//Students类 外键是classId引用ClassName中的ClassId
package com.hibernate_oneTomany;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="t_stu")
public class Students {
private String id;
private String name;
private ClassName clazz;
@Id
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne
@JoinColumn(name="classId")
public ClassName getClazz() {
return clazz;
}
public void setClazz(ClassName clazz) {
this.clazz = clazz;
}
}
//ClassName是班级类,主键是classId且为Students中classId的外键
package com.hibernate_oneTomany;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="t_class")
public class ClassName {
private String classId;
private String className;
private Set<Students> sets = new HashSet<Students>();
@Id
@Column(name="classId")
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
@OneToMany(mappedBy="clazz")
public Set<Students> getSets() {
return sets;
}
public void setSets(Set<Students> sets) {
this.sets = sets;
}
}
//测试类
package com.hibernate_oneTomany;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class Test {
public static void main(String[] args) {
AnnotationConfiguration af = new AnnotationConfiguration();
SessionFactory sf = af.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Students stu = new Students();
ClassName cls = new ClassName();
cls.setClassId("2");
cls.setClassName("bb");
stu.setId("aaa");
stu.setName("wangwu");
stu.setClazz(cls);
session.save(cls);
session.save(stu);
session.beginTransaction().commit();
}
}
//hibernate.cfg.xml中的配置
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- <mapping resource="com/hibernate/po/User.hbm.xml"/>
<mapping class="com.hibernate_onetoonepk.Students"></mapping>
<mapping class="com.hibernate_onetoonepk.StuID"></mapping> -->
<mapping class="com.hibernate_oneTomany.Students"></mapping>
<mapping class="com.hibernate_oneTomany.ClassName"></mapping>
</session-factory>
</hibernate-configuration>