hibernate级联更新外键问题
使用级联更新时,通过子表更新始终取不到主表的主键值,导致子表外键值始终是空的,而通过主表更新,子表能够取到所有值,小弟不解到底是怎么回事,请编程达人指教,代码如下:/*添...
使用级联更新时,通过子表更新始终取不到主表的主键值,导致子表外键值始终是空的,而通过主表更新,子表能够取到所有值,小弟不解到底是怎么回事,请编程达人指教,代码如下:
/*添加订单到数据库*/
Order_ order=new Order_();
User user=(User)session.get(Constants.LOGIN_USER);
order.setUserId(user.getUserId());
order=this.orderService.saveOrder(order);
/*添加订单项到数据库*/
List orderitems=(List)session.get(Constants.CART);
//一对多级联更新
Set set=new HashSet(orderitems);
order.setOrderitems(set);
this.orderService.updateOrder(order);
Orderitem orderitem=new Orderitem();
Hotel hotel=new Hotel();
for(int i=0;i < orderitems.size();i++){
orderitem=(Orderitem)orderitems.get(i);
// orderitem.setOrder(order);使用多对一更新的时候始终取不到外键值,而一对多可行
// this.orderitemService.saveOrderitem(orderitem);
/*预定后更新对应酒店的预定次数*/
hotel=this.hotelService.findByHotelId(String.valueOf(orderitem.getHotelId()));
hotel.setTimes(hotel.getTimes()+1);
this.hotelService.updateHotel(hotel);
}
hbm.xml文件
<hibernate-mapping>
<class name="com.myssh.po.Order_" table="order_">
<id name="orderId" type="integer">
<column name="oid" />
<generator class="native" />
</id>
<!-- cascade="all"级联保存,更新,删除附属物。inverse="true"设置关联关系由对方维护。为set属性 -->
<set name="orderitems" cascade="all" inverse="false" lazy="false">
<!-- 相关联主键 -->
<key column="oid"/>
<!-- 设置一对多关联类 -->
<one-to-many class="com.myssh.po.Orderitem"/>
</set>
<property name="userId" type="integer">
<column name="uid" />
</property>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.myssh.po.Orderitem" table="orderitem">
<id name="orderitemId" type="integer">
<column name="oiid"/>
<generator class="native"/>
</id>
<many-to-one name="order"
column="oid"
class="com.myssh.po.Order_"
cascade="none"
update="false"
insert="false"
not-null="true"
/>
<property name="orderId" type="integer">
<column name="oid"/>
</property>
<property name="hotelId" type="integer">
<column name="hid"/>
</property>
<property name="beginDate" type="date">
<!-- date是否应该配置格式? -->
<column name="begin"/>
</property>
<property name="days" type="integer">
<column name="days"/>
</property>
<property name="type" type="integer">
<column name="type"/>
</property>
</class>
</hibernate-mapping> 展开
/*添加订单到数据库*/
Order_ order=new Order_();
User user=(User)session.get(Constants.LOGIN_USER);
order.setUserId(user.getUserId());
order=this.orderService.saveOrder(order);
/*添加订单项到数据库*/
List orderitems=(List)session.get(Constants.CART);
//一对多级联更新
Set set=new HashSet(orderitems);
order.setOrderitems(set);
this.orderService.updateOrder(order);
Orderitem orderitem=new Orderitem();
Hotel hotel=new Hotel();
for(int i=0;i < orderitems.size();i++){
orderitem=(Orderitem)orderitems.get(i);
// orderitem.setOrder(order);使用多对一更新的时候始终取不到外键值,而一对多可行
// this.orderitemService.saveOrderitem(orderitem);
/*预定后更新对应酒店的预定次数*/
hotel=this.hotelService.findByHotelId(String.valueOf(orderitem.getHotelId()));
hotel.setTimes(hotel.getTimes()+1);
this.hotelService.updateHotel(hotel);
}
hbm.xml文件
<hibernate-mapping>
<class name="com.myssh.po.Order_" table="order_">
<id name="orderId" type="integer">
<column name="oid" />
<generator class="native" />
</id>
<!-- cascade="all"级联保存,更新,删除附属物。inverse="true"设置关联关系由对方维护。为set属性 -->
<set name="orderitems" cascade="all" inverse="false" lazy="false">
<!-- 相关联主键 -->
<key column="oid"/>
<!-- 设置一对多关联类 -->
<one-to-many class="com.myssh.po.Orderitem"/>
</set>
<property name="userId" type="integer">
<column name="uid" />
</property>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.myssh.po.Orderitem" table="orderitem">
<id name="orderitemId" type="integer">
<column name="oiid"/>
<generator class="native"/>
</id>
<many-to-one name="order"
column="oid"
class="com.myssh.po.Order_"
cascade="none"
update="false"
insert="false"
not-null="true"
/>
<property name="orderId" type="integer">
<column name="oid"/>
</property>
<property name="hotelId" type="integer">
<column name="hid"/>
</property>
<property name="beginDate" type="date">
<!-- date是否应该配置格式? -->
<column name="begin"/>
</property>
<property name="days" type="integer">
<column name="days"/>
</property>
<property name="type" type="integer">
<column name="type"/>
</property>
</class>
</hibernate-mapping> 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询