Hibernate相关联系的CRUD和调集映射(annotation)ITeye - 千亿集团

Hibernate相关联系的CRUD和调集映射(annotation)ITeye

2019年02月26日11时40分48秒 | 作者: 念波 | 标签: 目标,双向,化时 | 浏览: 967

设定cascade能够设定在耐久化时关于相关目标的操作。cascade的特点指明在做什么操作的时分相关目标是绑定在一起的。

双向相关:

1.设定mappedBy

2.在程序中要设定双向相关(目标之间)

cascade管的是增修改,而fecth管的是读取

一对多联系中,一的一方,默许的fecth为LAZY,而多一方,默许的fecth为EARGE

LAZY只能取到自身目标,而EARGE能够取到相关目标

Group.java

package com.test.hiberenate.model;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "t_group")
public class Group {
 private int id;
 private String name;
 //private Set User users = new HashSet User 
 //private List User users = new ArrayList User 
 private Map Integer, User users = new HashMap Integer, User 
 @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;
/* @OneToMany(mappedBy = "group", cascade = CascadeType.ALL)
 @OrderBy("name desc")// 指定依照User中name的降序摆放
 public List User getUsers() {
 return users;
 public void setUsers(List User users) {
 this.users = users;
 @OneToMany(mappedBy = "group", cascade = CascadeType.ALL)
 @MapKey(name = "id")// 指定map中的key为User中的id
 public Map Integer, User getUsers() {
 return users;
 public void setUsers(Map Integer, User users) {
 this.users = users;

 

User.java

package com.test.hiberenate.model;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "t_user")
public class User {
 private int id;
 private String name;
 private Group group;
 @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;
 @ManyToOne(cascade = CascadeType.ALL)
 @JoinColumn(name = "group_id")
 public Group getGroup() {
 return group;
 public void setGroup(Group group) {
 this.group = group;

 

HibernateORMappingTest.java

package com.test.hibernate.model;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.test.hiberenate.model.Group;
import com.test.hiberenate.model.User;
public class HibernateORMappingTest {
 private static SessionFactory sf = null;
 @BeforeClass
 public static void beforeClass() {
 //new SchemaExport(new Configuration().configure()).create(false, true);
 try {
 if (sf  null) {
 sf = new Configuration().configure().buildSessionFactory();
 } catch (HibernateException e) {
 e.printStackTrace();
 @AfterClass
 public static void afterClass() {
 sf.close();
/* @Test
 public void testSaveGroup() {
 Group group = new Group();
 group.setName("人力资源部");
 User user1 = new User();
 user1.setName("张三");
 user1.setGroup(group);
 User user2 = new User();
 user2.setName("李四");
 user2.setGroup(group);
 User user3 = new User();
 user3.setName("王五");
 user3.setGroup(group);
 group.getUsers().add(user1);
 group.getUsers().add(user2);
 group.getUsers().add(user3);
 Session session = sf.getCurrentSession();
 Transaction t = session.beginTransaction();
 session.save(group);
 t.commit();
 @Test
 public void testGroupForList() {
 testSaveGroup();
 Session session = sf.getCurrentSession();
 Transaction t = session.beginTransaction();
 Group group = (Group) session.get(Group.class, 1);
 List User lstUser = group.getUsers();
 for (int i = 0; i lstUser.size(); i++) {
 System.out.println(lstUser.get(i).getName());
 t.commit();
 @Test
 public void testGroupForMap() {
 Session session = sf.getCurrentSession();
 Transaction t = session.beginTransaction();
 Group group = (Group) session.get(Group.class, 1);
 Map Integer, User mUser = group.getUsers();
 for (Map.Entry Integer, User entry : mUser.entrySet()) {
 System.out.println(entry.getKey() + ":" + entry.getValue().getName());
 t.commit();
 public static void main(String[] args) {
 beforeClass();

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表千亿集团立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章