你的分享就是我们的动力 ---﹥

Hibernate关联关系映射-----基于连接表的单向一对多映射

时间:2013-06-14 15:10来源:www.chengxuyuans.com 点击:

实体:

[java] view plaincopyprint?
  1. packageuni.many2one.jointable;
  2. publicclassParent{
  3. privateintid;
  4. privateStringname;
  5. privateChildchild;
  6. publicParent(){
  7. }
  8. publicParent(intid,Stringname,Childchild){
  9. super();
  10. this.id=id;
  11. this.name=name;
  12. this.child=child;
  13. }
  14. publicintgetId(){
  15. returnid;
  16. }
  17. publicvoidsetId(intid){
  18. this.id=id;
  19. }
  20. publicStringgetName(){
  21. returnname;
  22. }
  23. publicvoidsetName(Stringname){
  24. this.name=name;
  25. }
  26. publicChildgetChild(){
  27. returnchild;
  28. }
  29. publicvoidsetChild(Childchild){
  30. this.child=child;
  31. }
  32. }
  33. packageuni.many2one.jointable;
  34. publicclassChild{
  35. privateintid;
  36. privateStringname;
  37. publicChild(intid,Stringname){
  38. super();
  39. this.id=id;
  40. this.name=name;
  41. }
  42. publicChild(){
  43. }
  44. publicintgetId(){
  45. returnid;
  46. }
  47. publicvoidsetId(intid){
  48. this.id=id;
  49. }
  50. publicStringgetName(){
  51. returnname;
  52. }
  53. publicvoidsetName(Stringname){
  54. this.name=name;
  55. }
  56. }
package uni.many2one.jointable;public class Parent {private int id;private String name;private Child child;public Parent() {}public Parent(int id, String name, Child child) {super();this.id = id;this.name = name;this.child = child;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Child getChild() {return child;}public void setChild(Child child) {this.child = child;}}package uni.many2one.jointable;public class Child {private int id;private String name;public Child(int id, String name) {super();this.id = id;this.name = name;}public Child() {}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

hm配置文件:

[html] view plaincopyprint?
  1. <hibernate-mapping>
  2. <!--<classname="org.hibernate.wk.Student"dynamic-insert="true"dynamic-update="true">-->
  3. <classname="uni.many2one.jointable.Parent">
  4. <idname="id"column="did">
  5. <generatorclass="native"/>
  6. </id>
  7. <propertyname="name"type="string"column="name"></property>
  8. <jointable="parentchild">
  9. <keycolumn="did"></key>
  10. <many-to-onename="child"column="cid"not-null="true"></many-to-one>
  11. </join>
  12. </class>
  13. </hibernate-mapping>
  14. <hibernate-mapping>
  15. <classname="uni.many2one.jointable.Child">
  16. <idname="id"column="id">
  17. <generatorclass="native"/>
  18. </id>
  19. <propertyname="name"type="string"column="name"></property>
  20. </class>
  21. </hibernate-mapping>
<hibernate-mapping><!-- <class name="org.hibernate.wk.Student" dynamic-insert="true" dynamic-update="true"> --><class name="uni.many2one.jointable.Parent"><id name="id" column="did"><generator  /></id><property name="name" type="string" column="name"></property><join table="parentchild"><key column="did"></key><many-to-one name="child" column="cid" not-null="true"></many-to-one></join></class></hibernate-mapping><hibernate-mapping><class name="uni.many2one.jointable.Child"><id name="id" column="id"><generator  /></id><property name="name" type="string" column="name"></property></class></hibernate-mapping>

测试文件:

[java] view plaincopyprint?
  1. publicclassTest{
  2. @org.junit.Test
  3. publicvoidtestAdd(){
  4. SessionFactorysf=HibernateUtil.getSessionFactory();
  5. Sessionsession=sf.getCurrentSession();
  6. session.beginTransaction();
  7. Childc1=newChild();
  8. c1.setName("child1");
  9. Parentmother=newParent();
  10. mother.setName("Mother");
  11. mother.setChild(c1);
  12. Parentfather=newParent();
  13. father.setName("Daddy");
  14. father.setChild(c1);
  15. session.save(c1);
  16. session.save(mother);
  17. session.save(father);
  18. session.beginTransaction().commit();
  19. }
  20. }
public class Test {@org.junit.Testpublic void testAdd() {SessionFactory sf = HibernateUtil.getSessionFactory();Session session = sf.getCurrentSession();session.beginTransaction();Child c1 = new Child();c1.setName("child1");Parent mother = new Parent();mother.setName("Mother");mother.setChild(c1);Parent father = new Parent();father.setName("Daddy");father.setChild(c1);session.save(c1);session.save(mother);session.save(father);session.beginTransaction().commit();}}

测测试结果:

[java] view plaincopyprint?
  1. Hibernate:insertintoChild(name)values(?)
  2. Hibernate:insertintoParent(name)values(?)
  3. Hibernate:insertintoparentchild(cid,did)values(?,?)
  4. Hibernate:insertintoParent(name)values(?)
  5. Hibernate:insertintoparentchild(cid,did)values(?,?)

http://www.cnblogs.com/focusj/archive/2011/07/23/2136416.html

转载注明地址:http://www.chengxuyuans.com/software_engineering/64487.html