我有一个表格如下
人:
地址:
Hibernate Mapping:
<hibernate-mapping>
<class name="com.mypackage.Person" table="PERSON">
<id name="id">
<column name="ID"/>
<generator class="increment"></generator>
</id>
<property name="name"></property>
<bag name="address" table="ADDRESS" inverse="true" lazy="false" fetch="select" cascade="all">
<key>
<column name="PERSON_ID" not-null="false"/>
</key>
<one-to-many class="Address"/>
</bag>
</class>
<class name="Address" table="ADDRESS">
<id name="id">
<column name="ADD_ID"/>
<generator class="increment"></generator>
</id>
<property name="type" type="string" not-null="true" column="TYPE" length="1"/>
<many-to-one name="person" class="Person" lazy="false" fetch="select">
<column name="PERSON_ID" not-null="true"/>
</many-to-one>
</class>
</hibernate-mapping>
的POJO:
public class Person {
private int id;
private String name;
private List<Address> addresses;
....
// getters and setters defined
}
public class Address {
private int add_id;
private int person;
private String address;
....
// getters and setters defined
}
我需要检索2种不同内容的数据
答案 0 :(得分:0)
尝试以下代码 -
Person p = sess.createCriteria(Person.class, id);
Hibernate.initialize(p.getAddresses());