Teachers表:
create table TEACHERS
(
ID NUMBER(
TEACHERNAME VARCHAR
)
alter table TEACHERS
add constraint DERE primary key (ID)
Students表
create table STUDENTS
(
ID NUMBER(
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);
Teacher
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) {
this
}
/**
* 得到教師名稱
* @return String 教師名稱
*/
public String getTeacherName() {
return teacherName;
}
/**
* 設置教師名稱
* @param teacherName String 教師名稱
*/
public void setTeacherName(String teacherName) {
this
}
/**
* 構造函數
* @param teacherName String
*/
public Teacher(String teacherName) {
this
}
}
Student
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) {
this
}
/**
* 得到學生名稱
* @return String 學生名稱
*/
public String getStudentName() {
return studentName;
}
/**
* 設置學生名稱
* @param studentName String 學生名稱
*/
public void setStudentName(String studentName) {
this
}
/**
* 得到教師對象
* @return Teacher 教師對象
*/
public Teacher getTeacher() {
return teacher;
}
/**
* 設置教師對象
* @param teacher Teacher 教師對象
*/
public void setTeacher(Teacher teacher) {
this
}
/**
* 構造函數
* @param string String
* @param teacher Teacher
*/
public Student(String studentName
this
this
}
}
## Oracle
hibernate
hibernate
nnection
nnection
nnection
nnection
Teacher
<?xml version=
<!DOCTYPE hibernate
PUBLIC
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<column name=
</property>
</class>
</hibernate
Student
<?xml version=
<!DOCTYPE hibernate
PUBLIC
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<column name=
</property>
<many
name=
column=
class=
cascade=
/>
</class>
</hibernate
BusinessService
package mypack;
import net
import net
import java
public class BusinessService{
//session工廠類
public static SessionFactory sessionFactory;
//實始化session工廠
static{
try{
//建立配置類
Configuration config = new Configuration();
config
//得到sessionFactory對象
sessionFactory = config
}catch(Exception e){e
}
/**
* 通過學生類
* @param student Student
* @throws Exception
* @return List
*/
public List findTeacherByStudent(Student student) throws Exception{
Session session = sessionFactory
Transaction tx = null;
try {
tx = session
List orders=(List)session
mit();
return orders;
}catch (Exception e) {
if (tx != null) {
tx
}
throw e;
} finally {
session
}
}
/**
* 查找指定id的學生類
* @param student_id long
* @throws Exception
* @return Student
*/
public Student findStudent(long student_id) throws Exception{
Session session = sessionFactory
Transaction tx = null;
try {
tx = session
Student student=(Student)session
mit();
return student;
}catch (Exception e) {
if (tx != null) {
//發生錯誤
tx
}
throw e;
} finally {
//沒有錯誤
session
}
}
/**
* 級連保存Teacher對象和Student對象
* @throws Exception
*/
public void saveTeacherAndStudentWithCascade() throws Exception{
Session session = sessionFactory
Transaction tx = null;
try {
tx = session
Teacher teacher=new Teacher(
Student student
Student student
session
session
mit();
}catch (Exception e) {
if (tx != null) {
//發生錯誤
tx
}
e
} finally {
// 沒有錯誤
session
}
}
/**
* 保存教師和學生對象
* @throws Exception
*/
public void saveTeacherAndStudent() throws Exception{
Session session = sessionFactory
Transaction tx = null;
try {
tx = session
Teacher teacher=new Teacher(
session
Student student
Student student
session
session
//提交事務
mit();
}catch (Exception e) {
if (tx != null) {
//發生錯誤
tx
}
throw e;
} finally {
// 沒有錯誤
session
}
}
/**
* 輸出學生對象集合
* @param students List
*/
public void printStudents(List students){
for (Iterator it = erator(); it
Student student=(Student)it
System
}
}
/**
* 測試方法
* @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()
sessionFactory
}
}
目錄結構示意
Classes
Hibernate
/mypack
Teacher
Student
BusinessService
Teacher
Student
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28579.html