Hibernate----@Annotation----一对多----级联

一对多,双向,"一"的一方

Java代码 收藏代码
  1. packagecom.pegasus.domain;
  2. importjava.util.HashSet;
  3. importjava.util.Set;
  4. importjavax.persistence.CascadeType;
  5. importjavax.persistence.Entity;
  6. importjavax.persistence.FetchType;
  7. importjavax.persistence.GeneratedValue;
  8. importjavax.persistence.Id;
  9. importjavax.persistence.JoinColumn;
  10. importjavax.persistence.OneToMany;
  11. importjavax.persistence.Table;
  12. //在实体类的类名前
  13. //标注是实体
  14. //对应的表名
  15. //相当于xml的
  16. //<classname="com.pegasus.domain.Group"table="t_group"></class>
  17. @Entity
  18. @Table(name="t_group")
  19. publicclassGroup{
  20. privateintid;
  21. privateStringname;
  22. privateSet<User>users=newHashSet<User>();
  23. //在主键的get方法前
  24. //标注主键,以及生成主键策略
  25. //相当于<idname="id"><generatorclass="native"></generator></id>
  26. @Id
  27. @GeneratedValue
  28. publicintgetId(){
  29. returnid;
  30. }
  31. publicvoidsetId(intid){
  32. this.id=id;
  33. }
  34. //在一般属性的get方法前
  35. //一般的列名前不需要标注
  36. //相当于<propertyname="name"></property>
  37. publicStringgetName(){
  38. returnname;
  39. }
  40. publicvoidsetName(Stringname){
  41. this.name=name;
  42. }
  43. //在一对多"一"的一方关联的集合前
  44. //使用@OneToMany标注,并标明"mappedBy",即关联的"多"的一方所对应的类的属性
  45. //cascade参数标明级联的方式,如果不需要级联可以省略
  46. //相当于<setname="users">
  47. //<keycolumn="groupId"></key>
  48. //<one-to-manyclass="com.pegasus.domain.User"/>
  49. //</set>
  50. @OneToMany(mappedBy="group",
  51. cascade={CascadeType.ALL}
  52. )
  53. publicSet<User>getUsers(){
  54. returnusers;
  55. }
  56. publicvoidsetUsers(Set<User>users){
  57. this.users=users;
  58. }
  59. }

一对多,双向,"多"的一方

Java代码 收藏代码
  1. packagecom.pegasus.domain;
  2. importjavax.persistence.CascadeType;
  3. importjavax.persistence.Entity;
  4. importjavax.persistence.FetchType;
  5. importjavax.persistence.GeneratedValue;
  6. importjavax.persistence.Id;
  7. importjavax.persistence.ManyToOne;
  8. importjavax.persistence.Table;
  9. //在实体类的类名前
  10. //标注是实体
  11. //对应的表名
  12. //相当于xml的
  13. //<classname="com.pegasus.domain.User"table="t_user"></class>
  14. @Entity
  15. @Table(name="t_user")
  16. publicclassUser{
  17. privateintid;
  18. privateStringname;
  19. privateGroupgroup;
  20. //在主键的get方法前
  21. //标注主键,以及生成主键策略
  22. //相当于<idname="id"><generatorclass="native"></generator></id>
  23. @Id
  24. @GeneratedValue
  25. publicintgetId(){
  26. returnid;
  27. }
  28. publicvoidsetId(intid){
  29. this.id=id;
  30. }
  31. //在一般属性的get方法前
  32. //一般的列名前不需要标注
  33. //相当于<propertyname="name"></property>
  34. publicStringgetName(){
  35. returnname;
  36. }
  37. publicvoidsetName(Stringname){
  38. this.name=name;
  39. }
  40. //在一对多"多"的一方关联的属性get方法前
  41. //使用@ManyToOne标注
  42. //cascade参数标明级联的方式,如果不需要级联可以省略
  43. //相当于<many-to-onename="group"column="groupId"></many-to-one>
  44. @ManyToOne(cascade={CascadeType.ALL})
  45. publicGroupgetGroup(){
  46. returngroup;
  47. }
  48. publicvoidsetGroup(Groupgroup){
  49. this.group=group;
  50. }
  51. }

单元测试类

Java代码 收藏代码
  1. packagecom.pegasus.domain;
  2. importorg.hibernate.Session;
  3. importorg.hibernate.SessionFactory;
  4. importorg.hibernate.cfg.AnnotationConfiguration;
  5. importorg.hibernate.tool.hbm2ddl.SchemaExport;
  6. importorg.junit.AfterClass;
  7. importorg.junit.BeforeClass;
  8. importorg.junit.Test;
  9. publicclassHibernateORMappingTest{
  10. privatestaticSessionFactorysessionFactory;
  11. @BeforeClass
  12. publicstaticvoidbeforeClass(){
  13. newSchemaExport(newAnnotationConfiguration().configure()).create(false,true);
  14. sessionFactory=newAnnotationConfiguration().configure().buildSessionFactory();
  15. }
  16. @AfterClass
  17. publicstaticvoidafterClass(){
  18. sessionFactory.close();
  19. }
  20. /**级联保存*/
  21. @Test
  22. publicvoidtestSaveUser(){
  23. System.out.print("begintotestSaveUser()");
  24. Useru=newUser();
  25. u.setName("u1");
  26. Groupg=newGroup();
  27. g.setName("g1");
  28. u.setGroup(g);
  29. Sessions=sessionFactory.getCurrentSession();
  30. s.beginTransaction();
  31. //s.save(g);
  32. s.save(u);
  33. s.getTransaction().commit();
  34. }
  35. //@Test
  36. //publicvoidtestSaveGroup(){
  37. //System.out.print("begintotestSaveGroup()");
  38. //Useru1=newUser();
  39. //u1.setName("u1");
  40. //Useru2=newUser();
  41. //u2.setName("u2");
  42. //Groupg=newGroup();
  43. //g.setName("g1");
  44. //g.getUsers().add(u1);
  45. //g.getUsers().add(u2);
  46. //u1.setGroup(g);
  47. //u2.setGroup(g);
  48. //Sessions=sessionFactory.getCurrentSession();
  49. //s.beginTransaction();
  50. //s.save(g);
  51. //s.getTransaction().commit();
  52. //}
  53. /**级联查询*/
  54. @Test
  55. publicvoidtestGetUser(){
  56. System.out.print("begintotestGetUser()");
  57. //testSaveGroup();
  58. Sessions=sessionFactory.getCurrentSession();
  59. s.beginTransaction();
  60. Useru=(User)s.get(User.class,1);
  61. s.getTransaction().commit();
  62. System.out.println(u.getGroup().getName());
  63. }
  64. //@Test
  65. //publicvoidtestDeleteUser(){
  66. //System.out.print("begintotestDeleteUser()");
  67. //testSaveGroup();
  68. //
  69. //Sessions=sessionFactory.getCurrentSession();
  70. //s.beginTransaction();
  71. ////Useru=(User)s.load(User.class,1);
  72. ////u.setGroup(null);
  73. ////s.delete(u);
  74. //s.createQuery("deletefromUseruwhereu.id=1").executeUpdate();
  75. //s.getTransaction().commit();
  76. //
  77. //}
  78. /**级联修改*/
  79. @Test
  80. publicvoidtestUpdateUser(){
  81. System.out.print("begintotestUpdateUser()");
  82. //testSaveGroup();
  83. Sessions=sessionFactory.getCurrentSession();
  84. s.beginTransaction();
  85. Useru=(User)s.get(User.class,1);
  86. s.getTransaction().commit();
  87. u.setName("user");
  88. u.getGroup().setName("group");
  89. Sessions2=sessionFactory.getCurrentSession();
  90. s2.beginTransaction();
  91. s2.update(u);
  92. s2.getTransaction().commit();
  93. }
  94. /**级联删除*/
  95. @Test
  96. publicvoidtestDeleteGroup(){
  97. System.out.print("begintotestDeleteGroup()");
  98. testSaveGroup();
  99. Sessions=sessionFactory.getCurrentSession();
  100. s.beginTransaction();
  101. //Useru=(User)s.load(User.class,1);
  102. //u.setGroup(null);
  103. //s.delete(u);
  104. Groupg=(Group)s.load(Group.class,1);
  105. s.delete(g);
  106. //s.createQuery("deletefromUseruwhereu.id=1").executeUpdate();
  107. s.getTransaction().commit();
  108. }
  109. //@Test
  110. //publicvoidtestSchemaExport(){
  111. //newSchemaExport(newAnnotationConfiguration().configure()).create(true,true);
  112. //}
  113. //publicstaticvoidmain(String[]args){
  114. //beforeClass();
  115. //}
  116. }

hibernate.cfg.xml

Xml代码 收藏代码
  1. <?xmlversion='1.0'encoding='utf-8'?>
  2. <!DOCTYPEhibernate-configurationPUBLIC
  3. "-//Hibernate/HibernateConfigurationDTD3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <propertyname="connection.driver_class">com.mysql.jdbc.Driver</property>
  8. <propertyname="connection.url">jdbc:mysql://localhost/hibtest</property>
  9. <propertyname="connection.username">root</property>
  10. <propertyname="connection.password">123</property>
  11. <propertyname="dialect">org.hibernate.dialect.MySQLDialect</property>
  12. <!--JDBCconnectionpool(usethebuilt-in)-->
  13. <propertyname="connection.pool_size">1</property>
  14. <!--EnableHibernate'sautomaticsessioncontextmanagement-->
  15. <propertyname="current_session_context_class">thread</property>
  16. <!--Disablethesecond-levelcache-->
  17. <propertyname="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
  18. <!--EchoallexecutedSQLtostdout-->
  19. <propertyname="show_sql">true</property>
  20. <!--<propertyname="format_sql">true</property>-->
  21. <!--Dropandre-createthedatabaseschemaonstartup
  22. <propertyname="hbm2ddl.auto">update</property>
  23. -->
  24. <!--
  25. <mappingresource="com/pegasus/domain/Group.hbm.xml"/>
  26. <mappingresource="com/pegasus/domain/User.hbm.xml"/>
  27. -->
  28. <mappingclass="com.pegasus.domain.Group"/>
  29. <mappingclass="com.pegasus.domain.User"/>
  30. </session-factory>
  31. </hibernate-configuration>

你可能感兴趣的:(annotation)