標籤:student row color htm 表名 ext size 自己的 data-
【Hibernate】之關於多對多單向關聯映射
老師和學生,最典型的多對多關聯,
Teacher和Student。所謂單向意思就是說。老師知道自己的教的是哪些學生而學生不知道是哪些老師教。
也能夠這麼說,在查詢的時候,通過老師能夠級聯查詢出學生,可是通過學生不能夠級聯查詢出老師。
而多對多最麻煩的是怎麼自己定義我們中間表的,表名和列名,這個是重要的!
Annotations配置
@Entity@Table(name="t_teacher")publicclass Teacher { private Integer id; private String name; private Set<Student> students=new HashSet<Student>();//set不同意反覆,最適合資料庫模型 @Id @GeneratedValue public Integer getId() { returnid; } publicvoid setId(Integerid) { this.id = id; } @Column(name="t_name") public String getName() { returnname; } publicvoid setName(Stringname) { this.name = name; } @ManyToMany @JoinTable(name="t_s_two",//自己定義表名 joinColumns={@JoinColumn(name="teacher_id")},//自己定義列名 inverseJoinColumns={@JoinColumn(name="student_id")})//反轉。和Teacher相應的那個表的列名,也是自己定義 public Set<Student>getStudents() { returnstudents; } publicvoidsetStudents(Set<Student> students) { this.students = students; }}
@Entity@Table(name="t_student")publicclass Student { private Integer id; private String name; private Integer age; @Id @GeneratedValue public Integer getId() { returnid; } publicvoid setId(Integerid) { this.id = id; } @Column(name="s_name") public String getName() { returnname; } publicvoid setName(Stringname) { this.name = name; } @Column(name="s_age") public Integer getAge() { returnage; } publicvoid setAge(Integerage) { this.age = age; }}
XML配置
publicclass Teacher { private Integer id; private String name; private Set<Student> students=newHashSet<Student>();//set不同意反覆,最適合資料庫模型 public Integer getId() { returnid; } publicvoid setId(Integerid) { this.id = id; } public String getName() { returnname; } publicvoid setName(Stringname) { this.name = name; } public Set<Student>getStudents() { returnstudents; } publicvoidsetStudents(Set<Student> students) { this.students = students; }}
publicclass Student { private Integer id; private String name; private Integer age; public Integer getId() { returnid; } publicvoid setId(Integerid) { this.id = id; } public String getName() { returnname; } publicvoid setName(Stringname) { this.name = name; } public Integer getAge() { returnage; } publicvoid setAge(Integerage) { this.age = age; }}
<?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 package="csg.hibernate.entity"> <class name="Teacher" table="t_teacher"> <id name="id"> <column name="id"/> <generator class="native" /> </id> <property name="name" /> <set name="students" table="t_s_two"><!—-自己定義表名--> <key column="teacher_id"/><!—自己定義列名--> <many-to-many class="csg.hibernate.entity.Student" column="student_id"/><!—自己定義列名--> </set> </class></hibernate-mapping>
<?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 package="csg.hibernate.entity"> <class name="Student" table="t_student"> <id name="id"> <column name="id"/> <generator class="native" /> </id> <property name="name" /> <property name="age" /> </class></hibernate-mapping>
以上就是XML的many-to-many(單向)的配置方法,ok!
Hibernate之關於多對多單向關聯映射