Role.java
package com.many2many.bean;
import java.util.Set;
public class Role {
private int id;
private String name;
private Set<User> users;
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;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
User.java
package com.many2many.bean;
import java.util.Set;
public class User {
private int id;
private String name;
private Set<Role> roles;
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;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
映射文件
Role.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.many2many.bean">
<class name="Role" table="roles" catalog="Hibernate_Many2Many">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<set name="users" table="user_role" cascade="all">
<key>
<column name="role_id"/>
</key>
<many-to-many column="user_id" class="User"/>
</set>
</class>
</hibernate-mapping>
User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.many2many.bean">
<class name="User" table="users" catalog="Hibernate_Many2Many">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<set name="roles" table="user_role" cascade="all">
<key>
<column name="user_id"/>
</key>
<many-to-many column="role_id" class="Role"/>
</set>
</class>
</hibernate-mapping>
导出的sql
create table Hibernate_Many2Many.roles (
id integer not null auto_increment,
name varchar(255),
primary key (id)
)
create table Hibernate_Many2Many.users (
id integer not null auto_increment,
name varchar(255),
primary key (id)
)
create table user_role (
role_id integer not null,
user_id integer not null,
primary key (user_id, role_id)
)
alter table user_role
add index FK143BF46ADA6B394F (role_id),
add constraint FK143BF46ADA6B394F
foreign key (role_id)
references Hibernate_Many2Many.roles (id)
alter table user_role
add index FK143BF46A7F95FD2F (user_id),
add constraint FK143BF46A7F95FD2F
foreign key (user_id)
references Hibernate_Many2Many.users (id)
测试代码
@Test
public void insert(){
Session session=HibernateSessionFactory.getSession();
Transaction transaction=session.beginTransaction();
try {
transaction.begin();
User user=new User();
user.setName("张三");
Role role=new Role();
role.setName("管理员");
Set<Role> roles=new HashSet<Role>();
roles.add(role);
user.setRoles(roles);
session.persist(user);
transaction.commit();
} catch (HibernateException e) {
e.printStackTrace();
transaction.rollback();
}
}
@Test
public void select(){
Session session=HibernateSessionFactory.getSession();
User user=(User) session.get(User.class, 1);
System.out.println(user.getName());
Set<Role> roles=user.getRoles();
for (Iterator<Role> iterator = roles.iterator(); iterator.hasNext();) {
Role role = (Role) iterator.next();
System.out.println(role.getName());
}
}