Hibernate - 如何获取子集合?

时间:2016-12-16 10:51:47

标签: hibernate collections set hql

我希望我的HQL查询返回一个对象及其子集合。

hbm:

<class name="Object" table="OBJECT">

<id name="id" column="IDOBJECT" type="long" access="field">
<generator class="sequence">
    <param name="sequence">OBJECT_SEQ</param>
</generator>
</id>

<many-to-one name="parent" column="IDPARENT" class="Parent" embed-xml="true" />

<set name="children" inverse="true" cascade="all-delete-orphan">   
    <key column="IDOBJECT" />
    <one-to-many class="Child" />
</set>

查询:

SELECT o from Object o JOIN FETCH o.parent JOIN FETCH o.children

最后:

  • 提取o.parent

  • o.children为空

子女孩在数据库中并与返回的对象相关联。

1 个答案:

答案 0 :(得分:1)

我使用基于注释测试了您的数据库结构,一切都很好。在您身边测试并检查您错过了什么:

子:

@Entity
@Table(name = "child")
@XmlRootElement
public class Child implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Size(max = 255)
    @Column(name = "name")
    private String name;
    @JoinColumn(name = "idobject", referencedColumnName = "id")
    @ManyToOne(fetch = FetchType.LAZY)
    private entity.Object object;

    public Child() {
    }

    public Child(Integer id) {
        this.id = id;
    }

    //getter and setters
}

对象:

@Entity
@Table(name = "object")
@XmlRootElement
public class Object implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Size(max = 255)
    @Column(name = "name")
    private String name;
    @OneToMany(mappedBy = "object", fetch = FetchType.LAZY)
    private List<Child> children;
    @JoinColumn(name = "idparent", referencedColumnName = "id")
    @ManyToOne(fetch = FetchType.LAZY)
    private Parent parent;

    public Object() {
    }

    public Object(Integer id) {
        this.id = id;
    }
    //getter and setter
}

父:

@Entity
@Table(name = "parent")
@XmlRootElement
public class Parent implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Size(max = 255)
    @Column(name = "name")
    private String name;
    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
    private List<entity.Object> objects;

    public Parent() {
    }

    public Parent(Integer id) {
        this.id = id;
    }
//getter and setters
}

和我的查询:

   public class ObjectDao { 

    public static List<Object> getObjects() {
        EntityManager em = Persistence.createEntityManagerFactory("test").createEntityManager();
        try {
            Query query = em.createQuery("SELECT o from Object o JOIN FETCH o.parent JOIN FETCH o.children");
            List l =  query.getResultList();
            return l;
        } catch (Exception ex) {
            ex.printStackTrace();
            return new ArrayList<Object>();
        } finally {
            em.close();
        }
    }
}

我的servlet for debuging:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    List<entity.Object> objects = ObjectDao.getObjects();
    for (entity.Object obj : objects) {
        System.out.println("object name:" + obj.getName());
        System.out.println("parent name:" + obj.getParent().getName());
        List<Child> children = obj.getChildren();
        System.out.println("children:");
        for(Child c: children){
            System.out.println(c.getName());
        }
    }
}

结果是:

object name:Object 1
parent name:Parent 1
children:
Child Name