package mypack;
public class Teacher {
private Long id;
private String teacherName;
public Teacher() {
}
public Teacher(String teacherName) {
this.teacherName = teacherName;
}
Student.java
package mypack;
public class Student {
private Long id;
private String studentName;
private Teacher teacher;
public Student() {
}
public Student(String studentName, Teacher teacher) {
this.studentName = studentName;
this.teacher = teacher;
}
3. hibernate.properties
## Oracle
hibernate.dialect net.sf.hibernate.dialect.Oracle9Dialect
hibernate.dialect net.sf.hibernate.dialect.OracleDialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.username jbcm
hibernate.connection.password jbcm
hibernate.connection.url jdbc:oracle:thin:@localhost:1521:wsy
Teacher.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping >
<class name="mypack.Teacher" table="teachers" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id>
<property name="teacherName" type="string" >
<column name="teacherName" length="15" />
</property>
</class>
</hibernate-mapping>
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping >
<class name="mypack.Student" table="students" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id>
<property name="studentName" type="string" >
<column name="studentName" length="15" />
</property>
<many-to-one
name="teacher"
column="teacher_id"
class="mypack.Teacher"
cascade="save-update"
/>
</class>
</hibernate-mapping>
联系客服