Rolejava
package commanymanybean;
import javautilSet;
public class Role {
private int id;
private String name;
private Set<User> users;
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
thisusers = users;
}
}
Userjava
package commanymanybean;
import javautilSet;
public class User {
private int id;
private String name;
private Set<Role> roles;
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
thisroles = roles;
}
}
映射文件
Rolehbmxml
<?xml version= encoding=UTF?>
<!DOCTYPE hibernatemapping PUBLIC
//Hibernate/Hibernate Mapping DTD //EN
hiber/hibernatemappingdtd>
<hibernatemapping package=commanymanybean>
<class name=Role table=roles catalog=Hibernate_ManyMany>
<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>
<manytomany column=user_id class=User/>
</set>
</class>
</hibernatemapping>
Userhbmxml
<?xml version= encoding=UTF?>
<!DOCTYPE hibernatemapping PUBLIC
//Hibernate/Hibernate Mapping DTD //EN
hiber/hibernatemappingdtd>
<hibernatemapping package=manymanybean>
<class name=User table=users catalog=Hibernate_ManyMany>
<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>
<manytomany column=role_id class=Role/>
</set>
</class>
</hibernatemapping>
导出的sql
create table Hibernate_ManyManyroles (
id integer not null auto_increment
name varchar()
primary key (id)
)
create table Hibernate_ManyManyusers (
id integer not null auto_increment
name varchar()
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 FKBFADABF (role_id)
add constraint FKBFADABF
foreign key (role_id)
references Hibernate_ManyManyroles (id)
alter table user_role
add index FKBFAFFDF (user_id)
add constraint FKBFAFFDF
foreign key (user_id)
references Hibernate_ManyManyusers (id)
测试代码
@Test
public void insert(){
Session session=HibernateSessionFactorygetSession();
Transaction transaction=sessionbeginTransaction();
try {
transactionbegin();
User user=new User();
usersetName(张三);
Role role=new Role();
rolesetName(管理员);
Set<Role> roles=new HashSet<Role>();
rolesadd(role);
usersetRoles(roles);
sessionpersist(user);
mit();
} catch (HibernateException e) {
eprintStackTrace();
transactionrollback();
}
}
@Test
public void select(){
Session session=HibernateSessionFactorygetSession();
User user=(User) sessionget(Userclass );
Systemoutprintln(usergetName());
Set<Role> roles=usergetRoles();
for (Iterator<Role> iterator = erator(); iteratorhasNext();) {
Role role = (Role) iteratornext();
Systemoutprintln(rolegetName());
}
}