SQL
create table Roles
(
id int not null auto_increment,
name varchar(20),
primary key (id)
);
create table Users
(
id int not null auto_increment,
name varchar(20),
primary key (id)
);
create table user_role
(
user_id int not null,
role_id int not null,
primary key (user_id, role_id)
);
alter table user_role add constraint FK_Reference_1 foreign key (user_id)
references Users (id) on delete restrict on update restrict;
alter table user_role add constraint FK_Reference_2 foreign key (role_id)
references Roles (id) on delete restrict on update restrict;
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.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="roles",catalog="JPA_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(cascade=CascadeType.PERSIST)
@JoinTable(
name="user_role",
joinColumns=@JoinColumn(name="role_id"),
inverseJoinColumns=@JoinColumn(name="user_id")
)
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;
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="users",catalog="JPA_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(mappedBy="users",cascade=CascadeType.REFRESH)
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
JPAUtil.java
package com.many2many.util;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JPAUtil {
private static EntityManager entityManager;
public static EntityManager getInstance(){
if(entityManager!=null){
return entityManager;
}else{
return makeInstance();
}
}
private static synchronized EntityManager makeInstance() {
if(entityManager==null){
EntityManagerFactory entityManagerFactory=Persistence.createEntityManagerFactory("JPA_Many2ManyPU");
return entityManagerFactory.createEntityManager();
}
return null;
}
}
测试代码
@Test
public void insert(){
EntityManager entityManager=JPAUtil.getInstance();
EntityTransaction entityTransaction=entityManager.getTransaction();
try {
entityTransaction.begin();
User user=new User();
user.setName("张三");
Role role=new Role();
role.setName("管理员");
Set<User> users=new HashSet<User>();
users.add(user);
role.setUsers(users);
entityManager.persist(role);
entityTransaction.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void select(){
EntityManager entityManager=JPAUtil.getInstance();
Role role=entityManager.find(Role.class, 1);
System.out.println(role.getName());
Set<User> set=role.getUsers();
for (Iterator<User> iterator = set.iterator(); iterator.hasNext();) {
User user = (User) iterator.next();
System.out.println(user.getName());
}
}