hibernate学习笔记13--级联设置


Hibernate的缺省设置是不支持级联操作的。在缺省行为时,对于两对象关联的属性,如果不做特殊处理(手动save),或者文件配置,hibernate是不予保存的,只会报错。
如:
public class Department {
 
 private int id;
 private String name;
 private Set<Employee>emps;
 报错如下:
Hibernate:
    insert
    into
        Department
        (name, id)
    values
        (?, ?)
Hibernate:
    update
        Employee
    set
        depart_id=?
    where
        id=?
2013-6-2 20:49:32 org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: cn.itcast.domain.Employee
 at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:249)
 at org.hibernate.type.EntityType.getIdentifier(EntityType.java:486)
 at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:151)
 at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:863)
 at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1253)
 at org.hibernate.persister.collection.OneToManyPersister.recreate(OneToManyPersister.java:183)
 at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
 at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:364)
 at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:356)
 at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:281)
 at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:328)
 at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
 at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234)
 at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404)
 at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
 at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
 at cn.itcast.RelationalMapping.One2many.add2(One2many.java:145)
 at cn.itcast.RelationalMapping.One2many.main(One2many.java:26)

 不能保存瞬时对象,就是说两个employee对象没有保存到数据库中
 
解决:
可以在对象映射文件中配置级联属性cascade="save-update"
在department.hbm.xml中配置

	  <!--cascade="save-update"级联属性  -->
		<set name="emps" cascade="save-update">
		<key column="depart_id"></key> <!--  告诉hibernate按照什么字段检索employee表数据,必须和employee.hbm.xnml中的外键定义一样 -->
		<one-to-many class="Employee"></one-to-many> <!--告诉hibernate集合set中装什么类型 -->
		</set>



解释:就是在保存/更新department时,同时把与employee相关的emps这个复杂属性保存/更新
cascade其他属性:
none就是缺省状态:
all:对我做什么操作,则相关联的类也做同样的操作
save-update:保存更新时做级联
delete-orphan(one-to-many):删除对象时,同时删除其子节点的对象。如:删除部门,则部门中的雇员也删除。
可以配置多了个属性值,属性值间用逗号分开。

一般对many-to-one many-to-many不设置级联,在one-to-one one-to-many设置级联
一般一对多时,在主对象内配置级联。


级联的设置一定要符合需求。

总结:


 

 

你可能感兴趣的:(hibernate学习笔记13--级联设置)