多对多关联映射需要新增加一张表才完成基本映射,他的实例场景如下:
用户与他的角色(一个用户拥有多个角色,一个角色还可以属于多个用户)
多对多关联映射,需要一个中间表,两个表中的主键放到第三张做一个关联,用第三张表来解决可能会造成数据冗余的问题,多对多的关联映射,在实体类中,跟一对多一样,也是用集合来表示的。
单向关联映射则是只能由A端去操作B端,B端不能操作A端的数据。而双向关联映射则是A,B两端都可以操作另一端的数据。
(1)XML版
SQL文件为:
CREATE TABLE `user` (
`id` varchar(255) NOT NULL,
`uname` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `role` (
`id` varchar(255) NOT NULL,
`rname` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `user_role` (
`userid` varchar(255) DEFAULT NULL,
`roleid` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Role实体类:
package Hibernate_demo1.Demo9.Entity;
import java.util.Set;
public class Role {
private String id;
private String rname;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRname() {
return rname;
}
public void setRname(String rname) {
this.rname = rname;
}
}
package Hibernate_demo1.Demo9.Entity;
import java.util.Set;
public class User {
private String id;
private String uname;
private Set roles; //角色集合,一个用户可以有多个角色
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public Set getRoles() {
return roles;
}
public void setRoles(Set roles) {
this.roles = roles;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return uname;
}
public void setName(String name) {
this.uname = name;
}
}
测试类如下:
package Hibernate_demo1.Demo9;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import Hibernate_demo1.Demo9.Entity.Role;
import Hibernate_demo1.Demo9.Entity.User;
import Hibernate_demo1.Demo9.Util.HibernateUtils;
public class App
{
public static void main( String[] args )
{
Session session = null;
try{
session = HibernateUtils.getSession();
session.beginTransaction();
//创建三个角色
Role ro1=new Role();
ro1.setRname("程序员");
//保存角色
session.save(ro1);
Role ro2=new Role();
ro2.setRname("产品经理");
//保存角色
session.save(ro2);
Role ro3=new Role();
ro3.setRname("CEO");
//保存角色
session.save(ro3);
//创建两个用户
User use1=new User();
use1.setName("小明");
User use2=new User();
use2.setName("小黑");
Set su=new HashSet();
su.add(ro1);
su.add(ro2);
//给小明设置角色为(程序员,产品经理)
use1.setRoles(su);
Set su1=new HashSet();
su1.add(ro3);
//给小黑设置角色为(CEO)
use2.setRoles(su1);
session.save(use1);
session.save(use2);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils.closeSession(session);
}
}
}
执行结果如下:
Hibernate:
insert
into
role
(rname, id)
values
(?, ?)
Hibernate:
insert
into
role
(rname, id)
values
(?, ?)
Hibernate:
insert
into
role
(rname, id)
values
(?, ?)
Hibernate:
insert
into
user
(uname, id)
values
(?, ?)
Hibernate:
insert
into
user
(uname, id)
values
(?, ?)
Hibernate:
insert
into
user_role
(userid, roleid)
values
(?, ?)
Hibernate:
insert
into
user_role
(userid, roleid)
values
(?, ?)
Hibernate:
insert
into
user_role
(userid, roleid)
values
(?, ?)
Role表
User表
user_role表
示例:http://download.csdn.net/detail/u011781521/9834640
(2)注解版
Role类
package Hibernate_demo1.Demo10.Entity;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="role")
public class Role {
@Id
@GenericGenerator(name="uuidGenerator", strategy="uuid")
@GeneratedValue(generator="uuidGenerator")
private String id;
@Column(name="rname")
private String rname;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRname() {
return rname;
}
public void setRname(String rname) {
this.rname = rname;
}
}
package Hibernate_demo1.Demo10.Entity;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="user")
public class User {
@Id
@GenericGenerator(name="uuidGenerator", strategy="uuid")
@GeneratedValue(generator="uuidGenerator")
private String id;
@Column(name="uname")
private String uname;
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(name = "user_role", joinColumns = {@JoinColumn(name ="userid" )}, inverseJoinColumns = { @JoinColumn(name = "roleid") })
private Set roles; //角色集合,一个用户可以有多个角色
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public Set getRoles() {
return roles;
}
public void setRoles(Set roles) {
this.roles = roles;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return uname;
}
public void setName(String name) {
this.uname = name;
}
}
package Hibernate_demo1.Demo10;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import Hibernate_demo1.Demo10.Entity.Role;
import Hibernate_demo1.Demo10.Entity.User;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
//创建三个角色
Role ro1=new Role();
ro1.setRname("程序员");
//保存角色
session.save(ro1);
Role ro2=new Role();
ro2.setRname("产品经理");
//保存角色
session.save(ro2);
Role ro3=new Role();
ro3.setRname("CEO");
//保存角色
session.save(ro3);
//创建两个用户
User use1=new User();
use1.setName("小明");
User use2=new User();
use2.setName("小黑");
Set su=new HashSet();
su.add(ro1);
su.add(ro2);
//给小明设置角色为(程序员,产品经理)
use1.setRoles(su);
Set su1=new HashSet();
su1.add(ro3);
//给小黑设置角色为(CEO)
use2.setRoles(su1);
session.save(use1);
session.save(use2);
tx.commit();
session.close();
}
}
Hibernate:
insert
into
role
(rname, id)
values
(?, ?)
Hibernate:
insert
into
role
(rname, id)
values
(?, ?)
Hibernate:
insert
into
role
(rname, id)
values
(?, ?)
Hibernate:
insert
into
user
(uname, id)
values
(?, ?)
Hibernate:
insert
into
user
(uname, id)
values
(?, ?)
Hibernate:
insert
into
user_role
(userid, roleid)
values
(?, ?)
Hibernate:
insert
into
user_role
(userid, roleid)
values
(?, ?)
Hibernate:
insert
into
user_role
(userid, roleid)
values
(?, ?)