user屬於groupjava
User源碼:session
package com.dailei.hibernate.model; import java.util.Date; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.IdClass; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Transient; @Entity // 定義實體,和表對應 @Table(name = "t_user") public class User { private Group group; private int id; private String name; //若是兩個對象有關聯,從A能夠導向到B,默認狀況下對A的保存不會影響到B //須要先存B再存A,若想自動保存,設置cascade //若是從B能夠導向C //在B上加上cascade @ManyToOne(cascade={CascadeType.ALL}) public Group getGroup() { return group; } @Id // 定義主鍵 @GeneratedValue public int getId() { return id; } public String getName() { return name; } public void setGroup(Group group) { this.group = group; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } }
group源碼:app
package com.dailei.hibernate.model; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name="_group") public class Group { private int id; private String name; private Set<User> users = new HashSet<User>(); @Id @GeneratedValue 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; } @OneToMany(mappedBy="group",cascade={CascadeType.ALL}) public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } }
測試程序:測試
package test.com.dailei.hibernate.model; import java.util.HashSet; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import com.dailei.hibernate.model.Group; import com.dailei.hibernate.model.User; public class Test1 { private static SessionFactory sf = null; private static Session session = null; @BeforeClass public static void beforeClass() { //有時候配置出錯,有時候不給報錯 try { sf=new AnnotationConfiguration().configure().buildSessionFactory(); new SchemaExport(new AnnotationConfiguration().configure()).create(false,true); }catch(Exception e) { e.printStackTrace(); } } @Test public void testSchemaExport() { User user = new User(); user.setName("u1"); Group group = new Group(); group.setName("g1"); group.getUsers().add(user); session = sf.getCurrentSession(); session.beginTransaction(); session.save(group); user.setGroup(group); // session.save(user); session.getTransaction().commit(); } @AfterClass public static void afterClass() { sf.close(); } }