Hibernate的那些事-manyToOne注解映射(List集合)

1:定义一个Group类,在映射关系中为一的一方:

package collections.list;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

@Entity
@Table(name = "t_list_group")
public class Group {
 private int id;
 private String name;
 private List users = new ArrayList();

 @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 })
 @OrderBy("name ASC")
 public List getUsers() {
  return users;
 }

 public void setUsers(List users) {
  this.users = users;
 }

}
2:定义一个User类,映射中多的一方:

package collections.list;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "t_list_user")
public class User {
 private int id;
 private String name;
 private Group group;

 @ManyToOne(cascade = CascadeType.ALL)
 public Group getGroup() {
  return group;
 }

 public void setGroup(Group group) {
  this.group = group;
 }

 @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;
 }

}
在hibernate.cfg.xml中添加对实体的映射:


  

这里我们使用的是注解映射,要特别与使用映射文件的映射公开来;

同时也要注意一个问题(在hibernate.cfg.xml文件中不能将两种混用,否则会出错)

 

3:使用Junit对这个映射进行测试:

package collections.list;

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;

public class TestListJunit {

 /**
  * @param args
  */

 private static SessionFactory sessionFactory;

 @BeforeClass
 public static void beforClass() {
  new SchemaExport(new AnnotationConfiguration().configure()).create(
    false, true);

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

 }

 @AfterClass
 public static void afterClass() {
  if (sessionFactory != null) {
   sessionFactory.close();
  }

 }

 @Test
 public void testSavaEntity() {
  Session session = sessionFactory.getCurrentSession();
  session.beginTransaction();

  Group group = new Group();
  group.setName("animal");
  User user = new User();
  user.setName("little dog");
  user.setGroup(group);
  User user1 = new User();
  user1.setName("little cat");
  user1.setGroup(group);

  group.getUsers().add(user);
  group.getUsers().add(user1);

  session.save(group);
  session.save(user);
  session.save(user1);
  session.getTransaction().commit();

 }

 public static void main(String[] args) {

  beforClass();
 }
}

 

你可能感兴趣的:(Hibernate的那些事-manyToOne注解映射(List集合))