简体   繁体   English

休眠条件:如何检索具有外键关系的表数据

[英]Hibernate Criteria: How to retrieve table data with foreign key relationship

I have two pojo classes wihch are named Document and DocumentUser. 我有两个pojo类,分别名为Document和DocumentUser。 DocumentUser has an property documentId which linked to Document's id by foreign key. DocumentUser具有一个属性documentId,该属性通过外键链接到Document的ID。 So i want to create criteria query which retrieve Documents with its DocumentUser which is linked itself by forein key("document_id") 因此,我想创建条件查询,以通过其文档用户检索文档,该文档用户由forein键(“ document_id”)自身链接

pojo classes: pojo课程:

Document 文献

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "DYS_BYS_DOSYA")
@Audited
public class Document implements Serializable {

    private Long id;
    private String name;

    private List<DocumentUser> documentUserList = new ArrayList<DocumentUser>();

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID", nullable = false, precision = 15, scale = 0)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "AD", nullable = false, length = 500)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @OneToMany(mappedBy = "document", fetch = FetchType.EAGER)
    @Fetch(FetchMode.SUBSELECT)
    @Cascade(CascadeType.ALL)
    public List<DocumentUser> getDocumentUserList() {
        return documentUserList;
    }

    public void setDocumentUserList(List<DocumentUser> documentUserList) {
        this.documentUserList = documentUserList;
    }

    @Override
    public String toString() {
        return "tr.com.enlil.dys.server.servis.model.Document[id=" + id + "]";
    }
}

DocumentUser: DocumentUser:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "DYS_DOSYA_SAHIBI_USER")
@Audited
public class DocumentUser implements Serializable {


    /**
     * 
     */
    private static final long serialVersionUID = 6393919788296838129L;

    private Long id;
    private Long personelId;
    private Document document;

    private String personelName;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID", unique = true, nullable = false, precision = 15, scale = 0)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "OLUSTURUCU_PERSONEL_ID")
    public Long getPersonelId() {
        return personelId;
    }

    public void setPersonelId(Long personelId) {
        this.personelId = personelId;
    }

    @Column(name = "KULLANICI_AD")
    public String getPersonelName() {
        return personelName;
    }

    public void setPersonelName(String personelName) {
        this.personelName = personelName;
    }

    @ManyToOne
    @JoinColumn(name = "DOSYA_ID")
    public Document getDocument() {
        return document;
    }

    public void setDocument(Document document) {
        this.document = document;
    }
}

In this way, how can i get Document data depends on personelId of DocumentUser table by using criteria query? 这样,如何通过使用条件查询获取Document数据取决于DocumentUser表的personelId? I am not familiar with hibernate and i need your helps. 我对休眠不熟悉,需要您的帮助。 I try to write some codes but didn't work. 我尝试编写一些代码,但是没有用。

public List<Document> fetchRecordsByCriteriaLimitedList(String userId) throws Exception{
        Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(Dosya.class);

        DetachedCriteria dosyaSahibiCriteria = (DetachedCriteria) criteria.createCriteria("documentUserList");
        dosyaSahibiCriteria.add(Restrictions.eq("personelId", userId));
        dosyaSahibiCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

            return criteria.list();

    }

Several problems with your code. 您的代码有几个问题。 First of all, you said 首先,你说

2)DocumentUser is subclass of Document 2)DocumentUser是Document的子类

This isn't true, judging from your code (it would mean that DocumentUser extends Document ), but you probably meant they are in a parent -> child relation. 从您的代码来看,这是不正确的(这意味着DocumentUser extends Document ),但是您可能意味着它们处于父级->子级关系中。 Second, in documentUserList mapping, there is this @OneToMany(mappedBy = "dosya", fetch = FetchType.EAGER) , which means there is a field named dosya in DocumentUser , and there isn't. 其次,在documentUserList映射中,有@OneToMany(mappedBy = "dosya", fetch = FetchType.EAGER) ,这意味着DocumentUser有一个名为dosya的字段,而没有。 Instead, replace it with mappedBy = "document" . 而是将其替换为mappedBy = "document" Assuming everything else is ok, query to get all documents based on their DocumentUser's id would be 假设其他一切正常,则根据其DocumentUser的ID查询以获取所有文档

public List<Document> fetchRecordsByCriteriaLimitedList(String userId) throws Exception{
    Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(Document.class);
    criteria.createAlias("documentUserList", "users").add(Restrictions.eq("users.personelId", userId));
    return criteria.list();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM