标签:hibernate一对多
多端 <many-to-one name="itrusUser" column="USER_ID2" class="ItrusUser" fetch="join" /> 一端 <set name="certs" cascade="delete" inverse="false" order-by="id"> <key column="USER_ID1" /> <one-to-many class="ItrusCert" /> </set>
测试代码
@Test public void addUser() { ItrusUser user = new ItrusUser(); user.setPin("1234"); user.setPinCreateTime(new Date()); user.setPinStatus("可用"); user.setUserAddress("北京某某某处"); user.setUserCreateTime(new Date()); user.setUserEmail("123@123.com"); user.setUserIdNo("1231*******4235"); user.setUserName("测试用户001"); user.setUserPhone("123123123"); user.setUserPostcode("100010"); user.setUserStatus("valid"); user.setUserSurName("没有"); ItrusCert itrus= new ItrusCert(); commonDAO.save(itrus); Set<ItrusCert> i = new HashSet<ItrusCert>(); i.add(itrus); user.setCerts(i); commonDAO.save(user); } @Test public void addUser1() { ItrusUser user = new ItrusUser(); user.setPin("1234"); user.setPinCreateTime(new Date()); user.setPinStatus("可用"); user.setUserAddress("北京某某某处"); user.setUserCreateTime(new Date()); user.setUserEmail("123@123.com"); user.setUserIdNo("1231*******4235"); user.setUserName("测试用户001"); user.setUserPhone("123123123"); user.setUserPostcode("100010"); user.setUserStatus("valid"); user.setUserSurName("没有"); ItrusCert itrus= new ItrusCert(); commonDAO.save(user); itrus.setItrusUser(user); commonDAO.save(itrus); }
说明 在多端配置的是user_id2 一端配置的是user_id1 adduser方法 是先保存多端数据然后保存一端数据,最后 使用的是 一端设置的外键值 user_id1 赋值, adduser1方法 是反之保存的,所以是最后保存的多端外键值user_id2 记载一下
标签:hibernate一对多
原文地址:http://yjm199.blog.51cto.com/4408395/1892384