Dowemo

( one ) configuration: To explain multiple associations by employer and employee

Multiple associations defined by the @ manytomany annotation. Also, you need to pass. Annotation @ jointable describes association tables and associated conditions If you're, one of the segments must be defined as owner, and the other end must be defined as inverse. ( this end will be ignored when updating the associated table ):


@Entity


public class Employer implements Serializable {


  @ManyToMany


 @JoinTable(


 name="EMPLOYER_EMPLOYEE",


 joinColumns=@JoinColumn(name="Employer_ID"),


 inverseJoinColumns=@JoinColumn(name="Employee_ID")


 )


 public Collection getEmployees() {


 return employees;


 }


. . .


}







@Entity


public class Employee implements Serializable {


  @ManyToMany(mappedBy ="employees")//参数值为Employer中的getEmployees中get后的值:employees


 public Collection getEmployers() {


 return employers;


 }


}






It isn't necessary to describe the physical mapping by the associated end: just a simple one. mappedByParameter, which contains the attribute name of the principal side, so that the relationship between the two sides.

( ii ) configuration based on xml:

The configuration of student. Hbm. Xml:

<? version ="1. 0"?>.
<! doctype hibernate-mapping public
"-//Hibernate/Hibernate mapping dtd 3.0//EN"
Http://hibernate. Sourceforge. Net/hibernate-mapping-3. 0. Dtd &>.
<hibernate-mapping>
<class name ="hibernate. Student">.
<id name ="id">
<generator class ="native"> </generator>
</id>

<property name ="name"> </property>
<set name ="teachers"table ="t_s">
<key column ="student_id"> </key>
<many-to-many class ="hibernate. Teacher"column ="teacher_id"/>.
</set>

</class>
</hibernate-mapping>

Configuration for teacher. Hbm. Xml:

<? version ="1. 0"?>.
<! doctype hibernate-mapping public
"-//Hibernate/Hibernate mapping dtd 3.0//EN"
Http://hibernate. Sourceforge. Net/hibernate-mapping-3. 0. Dtd &>.
<hibernate-mapping>
<class name ="com. Bjsxt. Hibernate. Teacher">.
<id name ="id">
<generator class ="native"> </generator>
</id>

<property name ="name"> </property>
<set name ="students"table ="t_s">
<key column ="teacher_id"> </key>
<many-to-many class ="com. Bjsxt. Hibernate. Student"column ="student_id"/>.
</set>

</class>

</hibernate-mapping>







Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs