hibernate中的多对一连接

v1uwarro  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(402)

我想在条目实体和项目实体之间建立连接。我的目标是按分配的项目id搜索条目。我输入项目id并希望获得分配了此项目的所有条目。
这是我在道课上的方法

@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;
    }

这是我的进入实体和项目实体

@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
}

项目类不知道条目。也许这就是问题所在?
///////////////////////////////////////////////////////////////////
编辑:
我根据文档创建了join-base。但我不清楚。因为如果我取消注解这个,我会得到空指针异常。我还创建了元模型类

@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;

}
aydmsdu9

aydmsdu91#

前提是您已经定义了 NamedQuery 注解,我想您打算在查询时使用它们。在这里你可以找到你需要的例子。否则,如果您希望使用 CriteriaQuery 你可以采用这些例子。

相关问题