Hibernate Criteria:如何使用外键关系检索表数据

时间:2014-10-27 09:01:18

标签: java hibernate criteria criteria-api detachedcriteria

我有两个pojo类,它们被命名为Document和DocumentUser。 DocumentUser有一个属性documentId,它通过外键链接到Document的id。 所以我想创建一个条件查询,用它的DocumentUser检索文档,这个文档是通过forein key(“document_id”)链接自己的

pojo课程:

文档

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

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

通过这种方式,我如何通过使用条件查询获取Document Data数据取决于DocumentUser表的personelId?我不熟悉hibernate,我需要你的帮助。我尝试写一些代码,但没有用。

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();

    }

1 个答案:

答案 0 :(得分:1)

您的代码存在一些问题。首先,你说

  

2)DocumentUser是Document

的子类

从您的代码判断(这意味着DocumentUser extends Document),这不是真的,但您可能意味着他们在父母中 - &gt;孩子的关系。其次,在documentUserList映射中,有@OneToMany(mappedBy = "dosya", fetch = FetchType.EAGER),这意味着dosya中有一个名为DocumentUser的字段,并且没有。{而是将其替换为mappedBy = "document"。假设其他一切正常,查询以获取基于其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();
}