1:一对多的一方:
package collections.map;
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_map_group")
public class Group {
private int id;
private String name;
private Map<Integer,User> users = new HashMap<Integer,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 })
@MapKey(name="id")
public Map<Integer, User> getUsers() {
return users;
}
public void setUsers(Map<Integer, User> users) {
this.users = users;
}
}
2:多对一的多方:
package collections.map;
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_map_user")
public class User {
private int id;
private String name;
private Group group;
@ManyToOne(cascade = CascadeType.ALL)
/*@ManyToOne就是要生成一个外键去关联它的关联类
* 如果没有下面这个注解也会将在User实体对应的数据表中产生一个外键字段,它是参照Group的主键*/
@JoinColumn(name="groupid")
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;
}
}
3:在映射文件的配置:
<mapping class="collections.map.Group" />
<mapping class="collections.map.User" />
4:使用Junit进行测试
package collections.map;
import java.util.Map;
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 TestMapJunit {
/**
* @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(){
Sessionsession =sessionFactory.getCurrentSession();
session.beginTransaction();
Group group=new Group();
group.setName("kewen");
User user =new User();
user.setName("myDog");
user.setGroup(group);
User user1 =new User();
user1.setName("myCat");
user1.setGroup(group);
group.getUsers().put(user.getId(), user);
group.getUsers().put(user1.getId(), user1);
session.save(group);
session.save(user);
session.save(user1);
session.getTransaction().commit();
}
@Test
public void loadGroupAndUser(){
sessionFactory = new AnnotationConfiguration ().configure()
.buildSessionFactory();
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
Group g=(Group)session.load(Group.class, 1);
for(Map.Entry<Integer,User> entry:g.getUsers().entrySet()){
System.out.println(entry.getValue().getId()+entry.getValue().getName());
}
session.getTransaction().commit();
}
public static void main(String [] args){
beforClass();
}
}