多对多注解配置
学生表
@Entity @Table(name ="T_STUDENT") @SequenceGenerator(name ="SEQ_STUDENT", sequenceName = "SEQ_STUDENT") public class Student implementsSerializable {
private static finallong serialVersionUID = 2524659555729848644L; private Longid; private Stringname; private Datebirthday; private intsex; private Stringaddress; privateSet<Teacher>teacherList; @Id @GeneratedValue(strategy =GenerationType.SEQUENCE, generator = "SEQ_STUDENT") @Column(name = "ID",nullable = false, precision = 22, scale = 0) public Long getId(){ returnid; } public voidsetId(Long id) { this.id =id; } @Column(name ="NAME") public StringgetName() { returnname; } public voidsetName(String name) { this.name =name; } @Temporal(TemporalType.DATE) @Column(name ="BIRTHDAY") public DategetBirthday() { returnbirthday; } public voidsetBirthday(Date birthday) { this.birthday= birthday; } @Column(name ="sex") public int getSex(){ returnsex; } public voidsetSex(int sex) { this.sex =sex; } @Column(name ="address") public StringgetAddress() { returnaddress; } public voidsetAddress(String address) { this.address =address; } @ManyToMany(cascade= CascadeType.ALL) @JoinTable(name ="T_TEACHER_STUDENT", joinColumns= @JoinColumn(name = "student_id"), inverseJoinColumns= @JoinColumn(name = "teacher_id")) publicSet<Teacher> getTeacherList(){ returnteacherList; } public voidsetTeacherList(Set<Teacher>teacherList) { this.teacherList= teacherList; } }
教师表
@Entity
@Table(name = "T_TEACHER")
@SequenceGenerator(name = "SEQ_TEACHER", sequenceName = "SEQ_TEACHER")
public class Teacher implements Serializable {
private static final long serialVersionUID = 2297316923535111793L;
private Long id;
private String name;
private int sex;
private Set<Student> studentList;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_TEACHER")
@Column(name = "ID", nullable = false, precision = 22, scale = 0)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "sex")
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
<span style="color:#FF0000;"> @ManyToMany(mappedBy = "teacherList", cascade = CascadeType.ALL)</span>
public Set<Student> getStudentList() {
return studentList;
}
public void setStudentList(Set<Student> studentList) {
this.studentList = studentList;
}
}
hibernate.cfg.xml配置2个class类 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration> <session-factory> <!-- properties --> <!-- 数据库URL --> <property name="hibernate.connection.url">jdbc:mysql://localhost/onetomany</property> <!-- 数据库JDBC驱动 --> <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property> <!-- 数据库用户名 --> <property name="hibernate.connection.username">root</property> <!-- 数据库密码 --> <property name="hibernate.connection.password">hello</property> <!-- 数据库方言 --> <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property> <!-- 是否日志调试 --> <property name="show_sql">true</property> <!-- 是否使用数据库外连接 --> <property name="use_outer_join">true</property> <!-- 事务管理 使用JDBC Transaction(使用JTA会报错) --> <property name="transaction.factory_class"> net.sf.hibernate.transaction.JDBCTransactionFactory </property> <!-- 指定hibernate每次提交的SQL数量 对批量操作的性能提升帮助很大!!!!!!!!!!!!! --> <property name="hibernate.jdbc.batch_size">25</property> <!-- 映射文件配置,配置文件名必须包含其相对于根的全路径 --><mappingclass="com.dvn.li.model.Student"/> <mappingclass="com.dvn.li.model.Teacher"/>
</session-factory> </hibernate-configuration>
多对多注解非注解
用户User<strong>:</strong> import java.util.HashSet; import java.util.Set; public class User { private String userid; private String username; private String password; private Set roles = new HashSet(); public User() { super(); } public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Set getRoles() { return roles; } public void setRoles(Set roles) { this.roles = roles; } }
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> <class name="cn.qeli.ums.entity.User" table="Users"> <id name="userid" column="userid" length="32" > <generator class="uuid.hex"></generator> </id> <property name="username" length="30" /> <property name="password" length="92" /> <set name="roles" table="user_role"> <key column="userid" /> <many-to-many column="roleid" class="cn.qeli.ums.entity.Role" /> </set> </class> </hibernate-mapping>
角色Role: package cn.qeli.ums.entity; import java.util.HashSet; import java.util.Set; public class Role { private String roleid; private String rolename; private Integer ordernum; private String description; private Set users = new HashSet(); public Role() { super(); } public String getRoleid() { return roleid; } public void setRoleid(String roleid) { this.roleid = roleid; } public String getRolename() { return rolename; } public void setRolename(String rolename) { this.rolename = rolename; } public Integer getOrdernum() { return ordernum; } public void setOrdernum(Integer ordernum) { this.ordernum = ordernum; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Set getUsers() { return users; } public void setUsers(Set users) { this.users = users; } }
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> <class name="cn.qeli.ums.entity.Role" table="Roles"> <id name="roleid" column="roleid" length="32" > <generator class="uuid.hex"></generator> </id> <property name="rolename" length="30" /> <property name="ordernum" /> <property name="description" length="200" /> <set name="users" table="user_role" inverse="true"> <key column="roleid"/> <many-to-many column="userid" class="cn.qeli.ums.entity.User" /> </set> </class> </hibernate-mapping>
hibernate.cfg.xml <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration> <session-factory> <!-- properties --> <!-- 数据库URL --> <property name="hibernate.connection.url">jdbc:mysql://localhost/onetomany</property> <!-- 数据库JDBC驱动 --> <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property> <!-- 数据库用户名 --> <property name="hibernate.connection.username">root</property> <!-- 数据库密码 --> <property name="hibernate.connection.password">hello</property> <!-- 数据库方言 --> <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property> <!-- 指定hibernate每次提交的SQL数量 对批量操作的性能提升帮助很大!!!!!!!!!!!!! --> <property name="hibernate.jdbc.batch_size">25</property> <!-- 映射文件配置,配置文件名必须包含其相对于根的全路径 --> <mapping resource="com/model/TUser.hbm.xml" /> <mapping resource="com/model/TAddress.hbm.xml" /> </session-factory> </hibernate-configuration>