【hibernate框架】几种集合映射

1.Set
前面我们已经用过,就不再说。

2.List
Group.java:
package cn.edu.hpu.listMapping;


import java.util.ArrayList;
import java.util.List;


import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;


@Entity
@Table(name="m_group")
public class Group {
	private int id;
	private String name;
	private List<User> users=new ArrayList<User>();
	
	
	@OneToMany(mappedBy="group",
			cascade={CascadeType.ALL},
			fetch=FetchType.LAZY
		)
	public List<User> getUsers() {
		return users;
	}
	public void setUsers(List<User> users) {
		this.users = users;
	}
	@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;
	}
		
}

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


import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
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},fetch=FetchType.EAGER)
	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中配置:
<mapping class="cn.edu.hpu.listMapping.Group"/>
<mapping class="cn.edu.hpu.listMapping.User"/>


执行测试,输出的sql语句:
alter table m_user 
        drop 
        foreign key FKBF71E05DA3D1B4E0


    drop table if exists m_group


    drop table if exists m_user


    create table m_group (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )


    create table m_user (
        id integer not null auto_increment,
        name varchar(255),
        group_id integer,
        primary key (id)
    )


    alter table m_user 
        add index FKBF71E05DA3D1B4E0 (group_id), 
        add constraint FKBF71E05DA3D1B4E0 
        foreign key (group_id) 
        references m_group (id)


成功创建m_user与m_group表,并且在user里建立了group的外键关联关系。
这时候,我们在group中取user数据的时候,要用list来取。

原则上来说,用set和list的区别不大,但是,当我们对取出的数据进行排序的时候,用list比较方便(set不支持排序)。

排序可以设定任意属性进行排序,也可以使用联合属性进行排序。
这里我们可以通过设定Annotation注解来设定:
@OrderBy
public List<User> getUsers() {
		return users;
	}
//@OrderBy默认按主键id排序
//@OrderBy("X DESC")/@OrderBy("X ASC")指按照X属性的ASC(升序)或DESC(降序)排序

其实完全可以不这么做,因为HQL语句就可以实现从数据库取数据的时候进行orderBy排序了。

3.Map

User.java一样,Group中的users变成Map形式。Map的Key值设置为主键(因为Key值不能重复),value设为User。
package cn.edu.hpu.listMapping;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

@Entity
@Table(name="m_group")
public class Group {
	private int id;
	private String name;
        private Map<Integer,User> users=new HashMap<Integer,User>();
	
        @OneToMany(mappedBy="group")
	@MapKey
	public Map<Integer, User> getUsers() {
		return users;
	}
	public void setUsers(Map<Integer, User> users) {
		this.users = users;
	}
	@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;
	}
		
}
其中@MapKey(name="id")指定map的Key值是属性的哪一个值。


建表语句:
 alter table m_user 
        drop 
        foreign key FKBF71E05DA3D1B4E0


    drop table if exists m_group


    drop table if exists m_user


    create table m_group (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )


    create table m_user (
        id integer not null auto_increment,
        name varchar(255),
        group_id integer,
        primary key (id)
    )


    alter table m_user 
        add index FKBF71E05DA3D1B4E0 (group_id), 
        add constraint FKBF71E05DA3D1B4E0 
        foreign key (group_id) 
        references m_group (id)

这样,当我们取users的时候,它会帮我们装到map里。

取出测试(group-g1里面有u1、u2、u3三个user对象):

@Test
public void testMapGroup(){
	sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
	Session s=sessionFactory.openSession();
	s.beginTransaction();
	Group g=(Group)s.load(Group.class,1);
	for(Map.Entry<Integer, User> entry:g.getUsers().entrySet()){
		System.out.println(entry.getValue().getName());
	}
	s.getTransaction().commit();
}

输出SQL语句和结果:
Hibernate: 
    select
        group0_.id as id0_0_,
        group0_.name as name0_0_ 
    from
        m_group group0_ 
    where
        group0_.id=?
Hibernate: 
    select
        users0_.group_id as group3_0_1_,
        users0_.id as id1_,
        users0_.id as formula0_1_,
        users0_.id as id1_0_,
        users0_.group_id as group3_1_0_,
        users0_.name as name1_0_ 
    from
        m_user users0_ 
    where
        users0_.group_id=?
u1
u2

u3

尊重开源精神,尊重劳动成果,转载请注明出处: http://blog.csdn.net/acmman/article/details/43907125

你可能感兴趣的:(Hibernate,list,map,set,hibernate集合映射)