hibernate:无法级联插入
工程是通过MyEclipse database brower 逆向工程生成PO,结果无法 正常完成数据的级联插入(客户与订单关系);
java代码如下 :
public void saveCustomerAndOrder(){ Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Customers c = new Customers("张三","济南",new HashSet()); Orders o = new Orders("zhansan2424"); o.setCustomers(c); c.getOrderses().add(o); session.save(c); tx.commit(); } catch(Exception e){ if(tx!=null){ tx.rollback(); } e.printStackTrace(); } finally { session.close(); } }
这代码 是没错的。。出错的原因在于:hibernate映射文件中:
<set name="inClothmns" inverse="true" cascade="all" lazy="false"> <key> <column name="intable_id" /> </key> <one-to-many class="net.itaem.po.InClothmn" /> </set>
<many-to-one name="customers" class="com.yehancheng.bean.Customers" fetch="select" cascade="all" > <column name="customerId" not-null="true" /> </many-to-one>
将上面xml文件 中的黄色字体 的属性添加 进去以后就可以正常了。