JPA annotation 学习笔记(三)
Hibernate 集合组件Collections of components
描述实体集合组件的时候我还是用到了hibernate的annotation,JPA里面的我没有找到类似的annotation,如果有知道的朋友可以回复告诉我,先谢过了.
实体间的annotation
public void testSimpleCollectionSave() {Student stu = new Student();stu.setName("kongji");String[] strs = {"andy","superman","中国人","大傻","Thr rock"};stu.getAliasNames().addAll(Arrays.asList(strs));sdao.persist(stu);}public void testSimpleCollectionUpdate() {Student stu = sdao.find(9L);List<String> l = stu.getAliasNames();for(String s : l){if(s.endsWith("大傻")){l.remove(s);}}sdao.merge(stu);}public void testEmbeddableCollectionSave(){Friend[] f = {new Friend("机器猫",10),new Friend("小新",8),new Friend("奥托曼",100)};Student stu = new Student();stu.setName("kongji");stu.getFriends().addAll(Arrays.asList(f));sdao.persist(stu);}public void testEmbeddableCollectionUpdate(){Student stu = sdao.find(11L);List<Friend> l = stu.getFriends();for(Friend f : l){if(f.getName().equals("小新"))l.remove(f);}sdao.merge(stu);}1 楼 jelver 2008-12-17 哥们,写得不错,继续努力 2 楼 liuliliujian 2009-05-14 哥们,这里有个问题,不知道你解决了没,就是Student的friends如果设置一个什么属性都没设置的Friend对象时,保存成功,下次再读出该student对象,重新保存,会出错,hibernate会将friend视为新创建的对象,重新insert,此时就出现了STUDENT_ID + IDX唯一性约束violation,希望同你探讨,msn: debby_test@hotmail.com. 3 楼 kj2ff 2009-05-15 liuliliujian 写道
哥们,这里有个问题,不知道你解决了没,就是Student的friends如果设置一个什么属性都没设置的Friend对象时,保存成功,下次再读出该student对象,重新保存,会出错,hibernate会将friend视为新创建的对象,重新insert,此时就出现了STUDENT_ID + IDX唯一性约束violation,希望同你探讨,msn: debby_test@hotmail.com.
你的friend表是否是PRIMARY KEY (`STUDENT_ID`,`IDX`), 两个主键,hibernate在处理生命周期依赖于主对象的集合对象时候,是删除然后插入操作,应为是id+idx复合主键,所以不应该出现你说的重复问题,我觉得是你的数据库表约束没有设置好的问题。 4 楼 lilihongmm 2010-02-18 写得不错,继续努力