数据库schema
Teachers表:
create table TEACHERS
(
ID NUMBER() not null
TEACHERNAME VARCHAR()
)
alter table TEACHERS
add constraint DERE primary key (ID)
Students表
create table STUDENTS
(
ID NUMBER() not null
STUDENTNAME VARCHAR()
TEACHER_ID NUMBER()
)
alter table STUDENTS
add constraint RERE primary key (ID)
alter table STUDENTS
add constraint FFF foreign key (TEACHER_ID)
references TEACHERS (ID);
Teacherjava和Studentjava
Teacherjava
package mypack;
public class Teacher {
//教师id
private Long id;
//教师名称
private String teacherName;
/**
* 缺省构造函数
*/
public Teacher() {
}
/**
* 得到教师id
* @return Long 教师id
*/
public Long getId() {
return id;
}
/**
* 设置教师id
* @param id Long 教师id
*/
public void setId(Long id) {
thisid = id;
}
/**
* 得到教师名称
* @return String 教师名称
*/
public String getTeacherName() {
return teacherName;
}
/**
* 设置教师名称
* @param teacherName String 教师名称
*/
public void setTeacherName(String teacherName) {
thisteacherName = teacherName;
}
/**
* 构造函数
* @param teacherName String
*/
public Teacher(String teacherName) {
thisteacherName = teacherName;
}
}
Studentjava
package mypack;
public class Student {
//学生id
private Long id;
//学生名称
private String studentName;
//教师类
private Teacher teacher;
/**
* 缺省构造函数
*/
public Student() {
}
/**
* 得到学生id
* @return Long 学生id
*/
public Long getId() {
return id;
}
/**
* 设置学生id
* @param id Long 学生id
*/
public void setId(Long id) {
thisid = id;
}
/**
* 得到学生名称
* @return String 学生名称
*/
public String getStudentName() {
return studentName;
}
/**
* 设置学生名称
* @param studentName String 学生名称
*/
public void setStudentName(String studentName) {
thisstudentName = studentName;
}
/**
* 得到教师对象
* @return Teacher 教师对象
*/
public Teacher getTeacher() {
return teacher;
}
/**
* 设置教师对象
* @param teacher Teacher 教师对象
*/
public void setTeacher(Teacher teacher) {
thisteacher = teacher;
}
/**
* 构造函数
* @param string String
* @param teacher Teacher
*/
public Student(String studentName Teacher teacher) {
thisstudentName = studentName;
thisteacher = teacher;
}
}
hibernateproperties
## Oracle
hibernatedialect netsfhibernatedialectOracleDialect
hibernatedialect netsfhibernatedialectOracleDialect
nnectiondriver_class oraclejdbcdriverOracleDriver
nnectionusername jbcm
nnectionpassword jbcm
nnectionurl jdbc:oracle:thin:@localhost::wsy
Teacherhbmxml和Studenthbmxml
Teacherhbmxml
<?xml version=?>
<!DOCTYPE hibernatemapping
PUBLIC //Hibernate/Hibernate Mapping DTD //EN
mappingdtd>
<hibernatemapping >
<class name=mypackTeacher table=teachers >
<id name=id type=long column=ID>
<generator class=increment/>
</id>
<property name=teacherName type=string >
<column name=teacherName length= />
</property>
</class>
</hibernatemapping>
Studenthbmxml
<?xml version=?>
<!DOCTYPE hibernatemapping
PUBLIC //Hibernate/Hibernate Mapping DTD //EN
mappingdtd>
<hibernatemapping >
<class name=mypackStudent table=students >
<id name=id type=long column=ID>
<generator class=increment/>
</id>
<property name=studentName type=string >
<column name=studentName length= />
</property>
<manytoone
name=teacher
column=teacher_id
class=mypackTeacher
cascade=saveupdate
/>
</class>
</hibernatemapping>
数据库操作类
BusinessServicejava
package mypack;
import netsfhibernate*;
import netsfhibernatecfgConfiguration;
import javautil*;
public class BusinessService{
//session工厂类
public static SessionFactory sessionFactory;
//实始化session工厂
static{
try{
//建立配置类添加Student类和Teacher类
Configuration config = new Configuration();
configaddClass(Studentclass)
addClass(Teacherclass);
//得到sessionFactory对象
sessionFactory = configbuildSessionFactory();
}catch(Exception e){eprintStackTrace();}
}
/**
* 通过学生类查找教师类
* @param student Student
* @throws Exception
* @return List
*/
public List findTeacherByStudent(Student student) throws Exception{
Session session = sessionFactoryopenSession();
Transaction tx = null;
try {
tx = sessionbeginTransaction();
List orders=(List)sessionfind(from Student as o where oteacherid=+studentgetId());
mit();
return orders;
}catch (Exception e) {
if (tx != null) {
txrollback();
}
throw e;
} finally {
sessionclose();
}
}
/**
* 查找指定id的学生类
* @param student_id long
* @throws Exception
* @return Student
*/
public Student findStudent(long student_id) throws Exception{
Session session = sessionFactoryopenSession();
Transaction tx = null;
try {
tx = sessionbeginTransaction();
Student student=(Student)sessionload(Studentclassnew Long(student_id));
mit();
return student;
}catch (Exception e) {
if (tx != null) {
//发生错误回滚
txrollback();
}
throw e;
} finally {
//没有错误关闭session
sessionclose();
}
}
/**
* 级连保存Teacher对象和Student对象
* @throws Exception
*/
public void saveTeacherAndStudentWithCascade() throws Exception{
Session session = sessionFactoryopenSession();
Transaction tx = null;
try {
tx = sessionbeginTransaction();
Teacher teacher=new Teacher(myTeacher);
Student student=new Student(studentteacher);
Student student=new Student(studentteacher);
sessionsave(student);
sessionsave(student);
mit();
}catch (Exception e) {
if (tx != null) {
//发生错误回滚
txrollback();
}
eprintStackTrace();
} finally {
// 没有错误关闭session
sessionclose();
}
}
/**
* 保存教师和学生对象
* @throws Exception
*/
public void saveTeacherAndStudent() throws Exception{
Session session = sessionFactoryopenSession();
Transaction tx = null;
try {
tx = sessionbeginTransaction();
Teacher teacher=new Teacher(teacher);
sessionsave(teacher);
Student student=new Student(studentteacher);
Student student=new Student(studentteacher);
sessionsave(student);
sessionsave(student);
//提交事务
mit();
}catch (Exception e) {
if (tx != null) {
//发生错误回滚
txrollback();
}
throw e;
} finally {
// 没有错误关闭session
sessionclose();
}
}
/**
* 输出学生对象集合
* @param students List
*/
public void printStudents(List students){
for (Iterator it = erator(); ithasNext();) {
Student student=(Student)itnext();
Systemoutprintln(OrderNumber of +studentgetTeacher()getTeacherName()+ :+studentgetStudentName());
}
}
/**
* 测试方法
* @throws Exception
*/
public void test() throws Exception{
saveTeacherAndStudent();
// saveTeacherAndStudentWithCascade();
// Student student=findStudent();
// List students=findTeacherByStudent(student);
// printStudents(students);
}
public static void main(String args[]) throws Exception {
new BusinessService()test();
sessionFactoryclose();
}
}
目录结构示意
Classes
Hibernateproperty
/mypack
Teacherjava
Studentjava
BusinessServicejava
Teacherhbmxml
Studenthbmxml