休眠联接中的多对一联接

时间:2018-12-12 14:14:44

标签: java mysql hibernate join criteria

我想在Entry实体和Project实体之间建立连接。我的目标是按分配的项目ID搜索条目。我输入项目ID,并希望获得分配了该项目的所有条目。

这是我在DAO类中的方法

@Override
    public PageWrapper<Entry> getListOfEntries(EntrySC sc) {
        public PageWrapper<Entry> getListOfEntries(EntrySC sc) {
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Entry> cq = cb.createQuery(Entry.class);
    Root<Entry> entry = cq.from(Entry.class);
    //Join<Entry,Project> join = cq.from(Entry.class).join(Entry_.project);

    cq.select(entry);

    PageWrapper<Entry> results = new PageWrapper<>();
    results.setResults(em.createQuery(cq).getResultList());
    results.setRows(em.createQuery(cq).getResultList().size());

    return results;
    }

这是我的Entry实体和Project实体

@Entity
@Table(name = "Entry")
@NamedQueries({
    @NamedQuery(name="Entry.findAll", query="SELECT e FROM Entry e ORDER BY e.date DESC"),
    @NamedQuery(name="Entry.findByID", query="SELECT e FROM Entry e WHERE e.id = :entryID"),
    @NamedQuery(name="Entry.findByUserID", query="SELECT e FROM Entry e WHERE e.user.id = :userID ORDER BY e.date DESC"),
    @NamedQuery(name="Entry.findByProjectID", query="SELECT e FROM Entry e WHERE e.project.id = :projectID")
})
public class Entry extends BaseEntityAudit {


    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "PROJECT_ID", nullable = false)
    @JsonIgnore
    private Project project;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "USER_ID", nullable = false)
    @JsonIgnore
    private User user;

    @Column(name = "COMMENT", nullable = true)
    private String comment;

    @Column(name = "HOURS", nullable = false)
    private BigDecimal hours;

    @Column(name = "DATE", nullable = false)
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date date;


    public Entry(){}

    //setters and getters

}

@Entity
@Table(name="Project")
@NamedQueries({
    @NamedQuery(name="Project.findAll", query="SELECT p FROM Project p"),
    @NamedQuery(name="Project.findByID", query="SELECT p FROM Project p WHERE p.id = :projectID")
})
public class Project extends BaseEntityAudit {

    @Column(name="NAME", nullable=false)
    private String name;

    @Column(name="START_DATE", nullable=false)
    @Temporal(TemporalType.DATE)
    private Date startDate;

    @Column(name="DUE_DATE")
    @Temporal(TemporalType.DATE)
    private Date dueDate;

    @Column(name="COMMENT", nullable=true)
    private String comment;


    public Project(){

    }

    //setters and getters
}

项目类不知道条目。也许这是问题所在?

///////////////////////////////////////////////// ///////////////////

编辑:

我根据文档创建了联接。但我不清楚。因为如果我取消注释,我将得到空指针异常。我还创建了元模型类

@StaticMetamodel(Entry.class)
public class Entry_ {

    public static volatile SingularAttribute<Entry,Project> project;

    public static volatile SingularAttribute<Entry,User> user;

    public static volatile SingularAttribute<Entry, String> comment;

    public static volatile SingularAttribute<Entry,BigDecimal> hours;

    public static volatile SingularAttribute<Entry,Date> date;

}

1 个答案:

答案 0 :(得分:0)

假设您已经定义了NamedQuery批注,我想您打算在查询时使用它们。 Here您可以找到所需的示例。否则,如果您更喜欢使用CriteriaQuery来构造查询,则可以采用these示例。