jpa查询

lb3vh1jj  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(176)

这样做的目的是使用关键字进行搜索,并且应该选择或显示两个表。下面是在我的sql控制台上工作的示例查询。 SELECT * FROM Week_Cycle w JOIN Day d ON d.week_Cycle_ID = w.id WHERE w.logged_By LIKE 'samplename' OR d.user_ID LIKE 'samplename' 我可以选择数据,但当我在域中将其转换为namedquery,然后在应用程序上运行时,它会引发sql异常,如下所示:
javax.persistence.persistenceexception:异常[eclipseelink-4002](eclipse persistence services-2.7.6.v20200131-b7c997804f):org.eclipse.persistence.exceptions.databaseexception内部异常:java.sql.sqlexception:从池借用preparestament失败
以下是namedquery: SELECT w FROM WeekCycle w JOIN Day d ON d.weekCycle = w.id WHERE w.loggedBy LIKE :keyword OR d.userId LIKE :keyword 以下是weekcycle域:

@Entity
@Table(name = "WEEK_CYCLE")
@NamedQueries({
    @NamedQuery(name = WeekCycle.FIND_ALL_WEEK_CYCLES_BY_KEYWORD, query = "SELECT w FROM WeekCycle w JOIN Day d ON d.weekCycle = w.id WHERE w.loggedBy LIKE :keyword OR  d.userId LIKE :keyword")})
public class WeekCycle{

    public static final String FIND_ALL_WEEK_CYCLES_BY_KEYWORD = "FindAllWeekCyclesByKeyword";

    @Id
    @SequenceGenerator(name = "SEQ_WEEK_CYCLE", sequenceName = "SEQ_WEEK_CYCLE", allocationSize = 50, initialValue = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_WEEK_CYCLE")
    @Column(nullable = false)
    private long id;

    @Column(name = "NAME", length = 255)
    private String name;

    @Enumerated(EnumType.STRING)
    @Column(name = "STATUS", length = 40)
    private WeekCycleStatus weekCycleStatus;

    @Column(name = "LOGGED_BY", length = 128)
    private String loggedBy;

    @OrderBy("id ASC")
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "weekCycle", orphanRemoval = true, cascade = CascadeType.ALL)
    private List<Day> days = new ArrayList<>();

以下是day域:

@Entity
@Table(name = "DAY")
public class Day {

    @Id
    @SequenceGenerator(name = "SEQ_Day", sequenceName = "SEQ_Day", allocationSize = 50, initialValue = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_Day")
    @Column(nullable = false)
    private long id;

    @Basic
    @Column(name = "USER_ID", nullable = true)
    private String userId;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "START_TIME")
    private LocalDateTime startTime;

    @Basic
    @Column(name = "DATA_SOURCE", nullable = true)
    private String dataSource;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "WEEK_CYCLE_ID")
    private WeekCycle weekCycle;

道:

public List<WeekCycle> searchOnWeekCycles(String keyword) {

        TypedQuery<WeekCycle> query = createEntityManager()
            .createNamedQuery(WeekCycle.FIND_ALL_WEEK_CYCLES_BY_KEYWORD, WeekCycle.class);
        query.setParameter("keyword", "%" + keyword + "%");

        return query.getResultList();
    }

是否有与上述namedquery等价的条件查询??

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题