【hibernate框架】一对多(多对一)双向CRUD-Fetch1

关于把数据从里面拿出来是什么样子。


User.java:
package cn.edu.hpu.one2many;


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="m_user")
public class User {
	private int id;
	private String name;
	private Group group;
	
	//只要有双向就要指定制定一个属性(mapedby)
	//不指定的话会有两个相同的字段产生
    @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;
	}
	
}



Group.java:
package cn.edu.hpu.one2many;


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.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;


@Entity
@Table(name="m_group")
public class Group {
	private int id;
	private String name;
	private Set<User> users=new HashSet<User>();
	//选择set的原因是因为,set互相之间不会有重复的
	//跟数据库模型比较匹配
	
	@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;
	}
	
}



测试代码:
@Test
public void testGetUser(){
	sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
	Session s=sessionFactory.openSession();
	s.beginTransaction();
     User u=(User)s.get(User.class,2);//当取user的时候,会把相应的group取出来
	System.out.println(u.getName());
    s.getTransaction().commit();
}


看看输出的sql语句:
Hibernate: 
    select
        user0_.id as id1_1_,
        user0_.group_id as group3_1_1_,
        user0_.name as name1_1_,
        group1_.id as id0_0_,
        group1_.name as name0_0_ 
    from
        m_user user0_ 
    left outer join
        m_group group1_ 
            on user0_.group_id=group1_.id 
    where
        user0_.id=?


取出了di为2的User,并且取出了相应的group


那么取1的时候会不会把多取出来呢?取一个组,会把组的user全取出来吗?
经过实验,不会。


实验:
@Test
public void testGetGroup(){
	sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
	Session s=sessionFactory.openSession();
	s.beginTransaction();
	Group g=(Group)s.get(Group.class,2);
	System.out.println(g.getName());
    s.getTransaction().commit();
}


输出的sql语句:
ibernate: 
    select
        group0_.id as id0_0_,
        group0_.name as name0_0_ 
    from
        m_group group0_ 
    where
        group0_.id=?


测试user是否取出:
 for (User u:g.getUsers()) {
    System.out.println(u.getName());
}


发现并没有取出所有在id=2的group的user。


cascade并不影响你的读取。读取使用另外一个参数设定的:Fetch。


Fetch管读,cascade管增删改。


fetch里面的属性:依然是enumeration(枚举),有两个参数:
EAGER:渴望的,着急的,立马想要得到的。
LAZY:懒的,缓慢的,不是立马要得到的。


OneToMany的默认的fetch是LAZY。


现在将fecth改成EAGER:
@OneToMany(mappedBy="group",
		cascade={CascadeType.ALL},
		fetch=FetchType.EAGER
	)
	public Set<User> getUsers() {
		return users;
	}
测试例子加:
 for (User u:g.getUsers()) {
    System.out.println(u.getName());
}


结果:
Hibernate: 
    select
        group0_.id as id0_1_,
        group0_.name as name0_1_,
        users1_.group_id as group3_0_3_,
        users1_.id as id3_,
        users1_.id as id1_0_,
        users1_.group_id as group3_1_0_,
        users1_.name as name1_0_ 
    from
        m_group group0_ 
    left outer join
        m_user users1_ 
            on group0_.id=users1_.group_id 
    where
        group0_.id=?
g1
u4
u5
u6
u3


将属于id=2的Group的User全部取出来了


注:显然User里面的ManyToOne里面的Fetch默认的是EAGER

转载请注明出处:http://blog.csdn.net/acmman

你可能感兴趣的:(Hibernate,框架,fetch,一对多多对一双向关联)