如附件图示:为账户(Account)和订单(Order)之间建立双向一对多关联关系。以前用Set在Account中来保存所有的Order。今天改成用List后,出现一个问题:得到Account实例后,调用getOrders()得到所有的订单,再访问订单实例的属性时报空指针问题(以前用Set时没有问题),请各位大大帮忙。
Account.hbm.xml
<class name="org.qiujy.domain.Account" table="account">
<id name="id" column="id" type="java.lang.Long">
<generator class="native"/></id>
<property name="userName" column="userName" type="string"/>
<list name="orders" table="orders" cascade="save-update" inverse="true">
<key column="accountId" not-null="true"/>
<list-index column="orderNumber" />
<one-to-many class="org.qiujy.domain.Order"/>
</list>
</class>
Order.hbm.xml
<class name="org.qiujy.domain.Order" table="orders">
<id name="id" column="id" type="long">
<generator class="native"/></id>
<property name="orderNumber" column="orderNumber" type="string"/>
<property name="createTime" column="createTime" type="timestamp"/>
<many-to-one name="account"
column="accountId"
class="org.qiujy.domain.Account"
not-null="true"/>
</class>
测试(数据库中已经有测试数据了)
Account account = (Account) session.load(Account.class, new Long(1));
System.out.println("============"+account.getUserName());
List list = account.getOrders();
int size = list == null ? 0 : list.size();
for(int i = 0; i < size; i++){
Order ord = (Order)list.get(i);
//报空指针异常
System.out.println( "No " + ord.getOrderNumber());
}
- 大小: 28.4 KB
分享到:
相关推荐
自己记录的学习笔记,内容很详细,有代码,包括:一对多双向关联关系、一对多双向自身关联关系、一对一关联关系(主键关联、外键关联)、一对多与一对一结合、多对多关联关系、Map、Set、List与Bag映射关系、查询...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,...
一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,...
一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...
7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
15.3.4 把多对多关联分解为两个一对多关联 15.4 小结 15.5 思考题 第16章 Hibernate的检索策略 16.1 Hibernate的检索策略简介 16.2 类级别的检索策略 16.2.1 立即检索 16.2.2 延迟检索 16.3 一对多...
15.3.4 把多对多关联分解为两个一对多关联 15.4 小结 15.5 思考题 第16章 Hibernate的检索策略 16.1 Hibernate的检索策略简介 16.2 类级别的检索策略 16.2.1 立即检索 16.2.2 延迟检索 16.3 一对多...
15.3.4 把多对多关联分解为两个一对多关联 15.4 小结 15.5 思考题 第16章 Hibernate的检索策略 16.1 Hibernate的检索策略简介 16.2 类级别的检索策略 16.2.1 立即检索 16.2.2 延迟检索 16.3 一对多...
15.3.4 把多对多关联分解为两个一对多关联 15.4 小结 15.5 思考题 第16章 Hibernate的检索策略 16.1 Hibernate的检索策略简介 16.2 类级别的检索策略 16.2.1 立即检索 16.2.2 延迟检索 16.3 一对多...
7.2.1 一对多关联 7.2.2 多对多关联 7.2.3 把列添加到联结表 7.2.4 映射map 7.3 多态关联 7.3.1 多态的多对一关联 7.3.2 多态集合 7.3.3 对联合的多态关联 7.3.4 每个具体类一张...