转载地址:http://www.blogjava.net/sxyx2008/archive/2010/10/18/335490.html
Role.java
package
com.many2many.bean;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = " roles " ,catalog = " Hibernate_Many2Many " )
public class Role {
private int id;
private String name;
private Set < User > users;
@Id
@Column(name = " id " )
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
public void setId( int id) {
this .id = id;
}
@Column(name = " name " )
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
@ManyToMany(mappedBy = " roles " ,cascade = CascadeType.ALL)
public Set < User > getUsers() {
return users;
}
public void setUsers(Set < User > users) {
this .users = users;
}
}
User.java
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = " roles " ,catalog = " Hibernate_Many2Many " )
public class Role {
private int id;
private String name;
private Set < User > users;
@Id
@Column(name = " id " )
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
public void setId( int id) {
this .id = id;
}
@Column(name = " name " )
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
@ManyToMany(mappedBy = " roles " ,cascade = CascadeType.ALL)
public Set < User > getUsers() {
return users;
}
public void setUsers(Set < User > users) {
this .users = users;
}
}
package
com.many2many.bean;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = " users " ,catalog = " Hibernate_Many2Many " )
public class User {
private int id;
private String name;
private Set < Role > roles;
@Id
@Column(name = " id " )
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
public void setId( int id) {
this .id = id;
}
@Column(name = " name " )
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = " user_role " ,
joinColumns = @JoinColumn(name = " user_id " ),
inverseJoinColumns = @JoinColumn(name = " role_id " )
)
public Set < Role > getRoles() {
return roles;
}
public void setRoles(Set < Role > roles) {
this .roles = roles;
}
}
导出的sql
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = " users " ,catalog = " Hibernate_Many2Many " )
public class User {
private int id;
private String name;
private Set < Role > roles;
@Id
@Column(name = " id " )
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
public void setId( int id) {
this .id = id;
}
@Column(name = " name " )
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = " user_role " ,
joinColumns = @JoinColumn(name = " user_id " ),
inverseJoinColumns = @JoinColumn(name = " role_id " )
)
public Set < Role > getRoles() {
return roles;
}
public void setRoles(Set < Role > roles) {
this .roles = roles;
}
}
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 (
user_id integer not null ,
role_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)
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 (
user_id integer not null ,
role_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());
}
}
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());
}
}