1、实体的关系图
1.1基于Annotation的配置
Special.java
package org.zttc.itat.model; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import org.hibernate.annotations.LazyCollection; import org.hibernate.annotations.LazyCollectionOption; @Entity @Table(name="t_special") public class Special { private int id; private String name; private String type; private Set<Classroom> clas; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } @OneToMany(mappedBy="special") @LazyCollection(LazyCollectionOption.EXTRA) public Set<Classroom> getClas() { return clas; } public void setClas(Set<Classroom> clas) { this.clas = clas; } }
Classroom.java
package org.zttc.itat.model; import java.util.Set; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.LazyCollection; import org.hibernate.annotations.LazyCollectionOption; @Entity @Table(name="t_classroom") @BatchSize(size=20) public class Classroom { private int id; private String name; private int grade; private Set<Student> stus; private Special special; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } @OneToMany(mappedBy="classroom") @LazyCollection(LazyCollectionOption.EXTRA) @Fetch(FetchMode.SUBSELECT) public Set<Student> getStus() { return stus; } public void setStus(Set<Student> stus) { this.stus = stus; } @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="spe_id") public Special getSpecial() { return special; } public void setSpecial(Special special) { this.special = special; } }
Student.java
package org.zttc.itat.model; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Version; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; @Entity @Table(name="t_stu") //@Cache(usage=CacheConcurrencyStrategy.READ_ONLY) public class Student { private int id; private String name; private String sex; private Classroom classroom; private int version; @Version public int getVersion() { return version; } public void setVersion(int version) { this.version = version; } @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @ManyToOne(fetch=FetchType.LAZY)//LAZY就是XML中的select,EAGER就表示XML中的join @JoinColumn(name="cid") public Classroom getClassroom() { return classroom; } public void setClassroom(Classroom classroom) { this.classroom = classroom; } }
1.2基于XML的配置
Special.hbm.xml
<?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="org.zttc.itat.model"> <class name="Special" table="t_special"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <property name="type"/> <set name="clas" inverse="true" lazy="extra"> <key column="spe_id"/> <one-to-many class="Classroom"/> </set> </class> </hibernate-mapping>
Classroom.hbm.xml
<?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="org.zttc.itat.model"> <class name="Classroom" table="t_classroom"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <property name="grade"/> <many-to-one name="special" column="spe_id" fetch="join"/> <set name="stus" inverse="true" lazy="extra" fetch="subselect"> <key column="cid"/> <one-to-many class="Student"/> </set> </class> </hibernate-mapping>
Student.hbm.xml
<?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="org.zttc.itat.model"> <class name="Student" table="t_stu"> <!-- <cache usage="read-only"/> --> <id name="id"> <generator class="native"/> </id> <version name="version"/> <property name="name"/> <property name="sex"/> <many-to-one name="classroom" column="cid" fetch="join"/> </class> </hibernate-mapping>