以下是多对多的基础配置
-- 多对多
-- 用户
CREATE TABLE users(
uid int PRIMARY KEY auto_increment, -- 用户id
uname VARCHAR(50) not null -- 用户名
);
-- 角色
CREATE TABLE roles(
rid int PRIMARY KEY auto_increment, -- 角色id
rname VARCHAR(50) not null -- 角色名
);
-- 用户角色关系表
CREATE TABLE relation(
uid int not null, -- 用户id
rid int not null -- 角色id
);
Roles.hbm.xml
<hibernate-mapping>
<class name="club.lygangdai.pojo.Roles" table="roles">
<id name="rid" type="int">
<column name="rid" />
<generator class="native" />
</id>
<property name="rname" type="java.lang.String">
<column name="rname" />
</property>
<set name="users" table="relation" >
<key column="rid"></key>
<many-to-many class="club.lygangdai.pojo.Users" column="uid" />
</set>
</class>
</hibernate-mapping>
Roles.java
package club.lygangdai.pojo;
import java.util.HashSet;
import java.util.Set;
/**
* @ClassName: Roles
* @Description: TODO(角色表)
* @author Uncle
* @date 2018年8月9日 下午7:11:39
*
*/
public class Roles {
private int rid;
private String rname;
private Set<Users> users = new HashSet<Users>();
/**
* @Description: TODO(这里用一句话描述这个类的作用)
* @param rid
* @param rname
*/
public Roles(int rid, String rname) {
super();
this.rid = rid;
this.rname = rname;
}
/**
* @Description: TODO(这里用一句话描述这个类的作用)
*/
public Roles() {
super();
}
/**
* @return rid
*/
public int getRid() {
return rid;
}
/**
* @param rid 要设置的 rid
*/
public void setRid(int rid) {
this.rid = rid;
}
/**
* @return rname
*/
public String getRname() {
return rname;
}
/**
* @param rname 要设置的 rname
*/
public void setRname(String rname) {
this.rname = rname;
}
/**
* @return users
*/
public Set<Users> getUsers() {
return users;
}
/**
* @param users 要设置的 users
*/
public void setUsers(Set<Users> users) {
this.users = users;
}
/* (非 Javadoc)
* @Title: toString
* @Description: TODO(这里用一句话描述这个方法的作用)
* @return
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Roles [rid=" + rid + ", rname=" + rname + "]";
}
}
Users.hbm.xml
<hibernate-mapping>
<class name="club.lygangdai.pojo.Users" table="users">
<id name="uid" type="int">
<column name="uid" />
<generator class="native" />
</id>
<property name="uname" type="java.lang.String">
<column name="uname" />
</property>
<set name="roles" table="relation" cascade="all-delete-orphan" inverse="true" >
<key>
<column name="uid" />
</key>
<many-to-many class="club.lygangdai.pojo.Roles" column="rid"/>
</set>
</class>
</hibernate-mapping>
Users.java
package club.lygangdai.pojo;
import java.util.HashSet;
import java.util.Set;
/**
* @ClassName: Users
* @Description: TODO(用户表)
* @author Uncle
* @date 2018年8月9日 下午7:10:23
*
*/
public class Users {
private int uid;
private String uname;
private Set<Roles> roles = new HashSet<Roles>();
/**
* @Description: TODO(这里用一句话描述这个类的作用)
* @param uid
* @param uname
*/
public Users(int uid, String uname) {
super();
this.uid = uid;
this.uname = uname;
}
/**
* @Description: TODO(这里用一句话描述这个类的作用)
*/
public Users() {
super();
}
/**
* @return uid
*/
public int getUid() {
return uid;
}
/**
* @param uid 要设置的 uid
*/
public void setUid(int uid) {
this.uid = uid;
}
/**
* @return uname
*/
public String getUname() {
return uname;
}
/**
* @param uname 要设置的 uname
*/
public void setUname(String uname) {
this.uname = uname;
}
/**
* @return roles
*/
public Set<Roles> getRoles() {
return roles;
}
/**
* @param roles 要设置的 roles
*/
public void setRoles(Set<Roles> roles) {
this.roles = roles;
}
/* (非 Javadoc)
* @Title: toString
* @Description: TODO(这里用一句话描述这个方法的作用)
* @return
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Users [uid=" + uid + ", uname=" + uname + "]";
}
}
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="club/lygangdai/pojo/Roles.hbm.xml"/>
<mapping resource="club/lygangdai/pojo/Users.hbm.xml"/>
</session-factory>
</hibernate-configuration>
评论 (0)