ManyToMany(多对多)
//领导类
@Entity
public class Leader{
private Set<Employee> employees = new HashSet<Employee>();
@ManyToMany(
targetEntity=Employee.class,
cascade={CascadeType.ALL}//想用主表控制次表,必须写all
)
@JoinTable( name="LEADER_EMPLOYEE", //中间表名
joinColumns=@JoinColumn(name="LEADER_ID"), // 列名
LEADER_ID
inverseJoinColumns=@JoinColumn(name="EMPEE_ID") // 列名 EMPEE_I
)
public Set<Employee> getEmployees(){
return employees;
}
Leader leader = new Leader();
leader.setLeaderName("张三");
leader.setPosition("主管");
Employee employee = new Employee();
employee.setEmpName("马仔");
leader.getEmployees().add(employee);
session.save(leader);
}
//以员工表为主表
public void
addLeaderAndEmployee (){
Employee employee = new Employee();
employee.setEmpName("马仔3");
Leader leader = new Leader();
leader.setLeaderName("张三3");
leader.setPosition("主管3");
leader.getEmployees().add(employee);
employee.getLeaders().add(leader);
session.save(employee); session.beginTransaction().commit();
}
}
@Entity
//员工类
public class Employee{
private Set<Leader> Leaders = new HashSet<Leader>();
@ManyToMany( cascade = {CascadeType.ALL},
mappedBy = "employees",//相等于inverse
//如果不加这一句,会多生成一个中间表
//如果不加这一句,会多生成一个中间表
targetEntity = Leader.class )
public Set<Leader> getLeaders() { return Leaders; }
}
//测试类
public class Test{
//以领导表为主表
public void
addLeaderAndEmployee (){
addLeaderAndEmployee (){
Leader leader = new Leader();
leader.setLeaderName("张三");
leader.setPosition("主管");
Employee employee = new Employee();
employee.setEmpName("马仔");
leader.getEmployees().add(employee);
//session.save(employee)//如果主表中cascade不是all,必须要加上这一句
session.save(leader);
}
//以员工表为主表
public void
addLeaderAndEmployee (){
Employee employee = new Employee();
employee.setEmpName("马仔3");
Leader leader = new Leader();
leader.setLeaderName("张三3");
leader.setPosition("主管3");
leader.getEmployees().add(employee);
employee.getLeaders().add(leader);
session.save(employee); session.beginTransaction().commit();
}
public static void main(String[] args){
}
}
---------------------------------------------------------bulid.xml
<!-- 生成数据库 -->
<!-- ================================================================ -->
<target name="gen data base" description="生成数据库">
<echo message="删除目录 ${webapp}/schema" />
<delete dir="${webapp}/schema" />
<echo message="创建目录 ${webapp}/schema" />
<mkdir dir="${webapp}/schema" />
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="ant.classpath" />
<hibernatetool destdir="./">
<annotationconfiguration configurationfile="${resource}/hibernate.cfg.xml" />
<hbm2ddl export="true" outputfilename="${webapp}/schema/sql.sql" />
</hibernatetool>
</target>
<!-- ================================================================ -->
<target name="gen data base" description="生成数据库">
<echo message="删除目录 ${webapp}/schema" />
<delete dir="${webapp}/schema" />
<echo message="创建目录 ${webapp}/schema" />
<mkdir dir="${webapp}/schema" />
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="ant.classpath" />
<hibernatetool destdir="./">
<annotationconfiguration configurationfile="${resource}/hibernate.cfg.xml" />
<hbm2ddl export="true" outputfilename="${webapp}/schema/sql.sql" />
</hibernatetool>
</target>