Hibernate!!关联关系cascade

在写代码的时候,需要设置好关联,不然数据库中会没有相应的数据。

双向关系在程序中药设定双向关联,不然在数据库中会出问题

 

cascade翻译成中文叫:级联

cascade一般用在级联保存,级联更新,级联删除上

cascade可以取得值:

  • ALL 在所有情况下,在持久化操作的增删改查,都会级联到另一个对象
  • MERGE   合并的时候
  • PERSIST   存储的时候
  • REFRESH  刷新
  • REMOVE   删除

如果两个对象之间有关联,那么就需要设置cascade这个属性。

 

@ManyToOne注解的这端,是多端

1.在注释@ManyToOne(cascade=CascadeType.ALL,optional=true)中将属性optional设置为true,这可以使得即使外键为空时仍可以向表中添加数据。

注意:导入包的时候是:import javax.persistence.CascadeType;

 

例子:

 

 1 package com.hb.model;

 2 

 3 import javax.persistence.Entity;

 4 import javax.persistence.GeneratedValue;

 5 import javax.persistence.Id;

 6 import javax.persistence.Table;

 7 

 8 @Entity

 9 @Table(name="t_group")

10 public class Group {

11     private  int gid ;

12     private  String  gname ;

13     

14     @Id

15     @GeneratedValue

16     public int getGid() {

17         return gid;

18     }

19     public void setGid(int gid) {

20         this.gid = gid;

21     }

22     public String getGname() {

23         return gname;

24     }

25     public void setGname(String gname) {

26         this.gname = gname;

27     }

28     

29 

30 }

 

 1 package com.hb.model;

 2 

 3 

 4 import javax.persistence.CascadeType;

 5 import javax.persistence.Entity;

 6 import javax.persistence.GeneratedValue;

 7 import javax.persistence.Id;

 8 import javax.persistence.ManyToOne;

 9 import javax.persistence.Table;

10 

11 

12 @Entity

13 @Table(name="t_user")

14 public class User {

15     private  int  id ; 

16     private   String  name ;

17     private  Group  group ;

18     

19     @Id

20     @GeneratedValue

21     public int getId() {

22         return id;

23     }

24     public void setId(int id) {

25         this.id = id;

26     }

27     public String getName() {

28         return name;

29     }

30     public void setName(String name) {

31         this.name = name;

32     }

33     

34 

35     //cascade属性是一个数组,cascade是级联

36     @ManyToOne(cascade={CascadeType.ALL})

37     public Group getGroup() {

38         return group;

39     }

40     public void setGroup(Group group) {

41         this.group = group;

42     }

43     

44     

45 

46 }
 1 package com.hb.model;

 2 

 3 import static org.junit.Assert.*;

 4 

 5 import org.hibernate.Session;

 6 import org.hibernate.SessionFactory;

 7 import org.hibernate.cfg.AnnotationConfiguration;

 8 import org.hibernate.tool.hbm2ddl.SchemaExport;

 9 import org.junit.AfterClass;

10 import org.junit.BeforeClass;

11 import org.junit.Test;

12 

13 public class hibernateCRUDTest {

14     

15 private  static  SessionFactory  sessionFactory ;

16     

17     @BeforeClass

18     public  static  void  beforeClass(){

19         new  SchemaExport(new AnnotationConfiguration().configure()).create(true, true);

20         sessionFactory = new  AnnotationConfiguration().configure().buildSessionFactory();

21         

22     }

23     

24     @AfterClass

25     public  static  void  afterClass(){

26         sessionFactory.close();

27     }

28     

29 

30     @Test

31     public void testSaveUser() {

32         User  u = new User();

33         u.setName("u1");

34         Group  g = new  Group();

35         u.setGroup(g);

36         g.setGname("g1");

37         Session  session = sessionFactory.getCurrentSession();

38         session.beginTransaction();

39         //session.save(g);

40         session.save(u);

41         session.getTransaction().commit();

42         

43         

44         

45     }

46     

47     public static void main(String[] args) {

48         beforeClass();

49     }

50 

51 }

 

 

 

 1 <?xml version='1.0' encoding='UTF-8'?>

 2 <!DOCTYPE hibernate-configuration PUBLIC

 3           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

 4           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 5 

 6 <!-- Generated by MyEclipse Hibernate Tools.                   -->

 7 <hibernate-configuration>

 8 

 9 <session-factory>

10     <property name="dialect">

11         org.hibernate.dialect.MySQLDialect

12     </property>

13     <property name="connection.url">

14         jdbc:mysql://localhost:3306/hb

15     </property>

16     <property name="connection.username">root</property>

17     <property name="connection.password">root</property>

18     <property name="connection.driver_class">

19         com.mysql.jdbc.Driver

20     </property>

21     <property name="myeclipse.connection.profile">Mysql</property>

22     <property name="show_sql">true</property>

23     <property name="format_sql">true</property>

24 

25     <property name="current_session_context_class">thread</property>

26     

27     <mapping class="com.hb.model.Group" />

28     <mapping class="com.hb.model.User" />

29 </session-factory>

30 

31 </hibernate-configuration>

 

 在 public class hibernateCRUDTest 类中,测试保存Group

 

 1     @Test

 2     public void testSaveGroup() {

 3         User  u1= new User();

 4         u1.setName("u1");

 5         User  u2 = new User();

 6         u2.setName("u2");

 7         Group  g = new  Group();

 8         g.setGname("g1");

 9         

10         g.getUsers().add(u1);

11         g.getUsers().add(u2);

12         

13         u1.setGroup(g);

14         u2.setGroup(g);

15 

16         Session  session = sessionFactory.getCurrentSession();

17         session.beginTransaction();

18         session.save(g);

19         session.getTransaction().commit();

20     }

 

 

 

由上面的 g.getUsers().add(u1); g.getUsers().add(u2) 可知现在是双向关联,所以对应的Group类中需要加入@OneToMany属性

双向关系在程序中要设定双向关联,不然在数据库中会出问题

 

双向关系在程序中要设定mappedBy="group",不然在数据库中会出问题

代码如下:

 

 

 1 package com.hb.model;

 2 

 3 import java.util.HashSet;

 4 import java.util.Set;

 5 

 6 import javax.persistence.CascadeType;

 7 import javax.persistence.Entity;

 8 import javax.persistence.GeneratedValue;

 9 import javax.persistence.Id;

10 import javax.persistence.OneToMany;

11 import javax.persistence.Table;

12 

13 @Entity

14 @Table(name="t_group")

15 public class Group {

16     private  int gid ;

17     private  String  gname ;

18     private  Set<User>  users = new HashSet<User>();

19     

20     @OneToMany(mappedBy="group",cascade={CascadeType.ALL})

21     public Set<User> getUsers() {

22         return users;

23     }

24     public void setUsers(Set<User> users) {

25         this.users = users;

26     }

27     @Id

28     @GeneratedValue

29     public int getGid() {

30         return gid;

31     }

32     public void setGid(int gid) {

33         this.gid = gid;

34     }

35     public String getGname() {

36         return gname;

37     }

38     public void setGname(String gname) {

39         this.gname = gname;

40     }

41     

42 

43 }

 

 

 

 

 

 

你可能感兴趣的:(Hibernate)