下面是我写的一个班级-学生的一对多案例
学生类
package tm.change.domain;
public class Student {
//主键
private String id;
//学生姓名
private String name;
//外键值
private Banji banji;
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;
}
public Banji getBanji() {
return banji;
}
public void setBanji(Banji banji) {
this.banji = banji;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", banji=" + banji + "]";
}
}
班级类
package tm.change.domain;
import java.util.List;
//班级
public class Banji {
//主键
private String id;
//班级名称
private String name;
//班级人数
private Integer num;
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;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
@Override
public String toString() {
return "Banji [id=" + id + ", name=" + name + ", num=" + num + "]";
}
public Banji() {
super();
// TODO Auto-generated constructor stub
}
}
学生类映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-2-19 15:44:43 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="tm.change.domain.Student" table="STUDENT">
<id name="id" type="java.lang.String">
<column name="ID" />
<generator class="uuid" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<!-- 在一对多关系中,只要在多的一方中配置就可以了。 -->
<many-to-one name="banji" class="tm.change.domain.Banji" column="banji_id" cascade="save-update"/>
</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">
<!-- Generated 2017-2-19 16:19:18 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<!-- name 类名(如果不在hibernate-mapping中写包名,就要像我这样把包名也写上。table 表名 -->
<class name="tm.change.domain.Banji" table="BANJI">
<id name="id" type="java.lang.String">
<column name="ID" />
<generator class="uuid" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="num" type="java.lang.Integer">
<column name="NUM" />
</property>
</class>
</hibernate-mapping>
hibernate映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/goods</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.show_sql">true</property>
<!-- 面试题:create update 表已经存在时对表进行更新-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 资源配置 -->
<mapping resource="tm/change/domain/Banji.hbm.xml"/>
<mapping resource="tm/change/domain/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>