hibernate 一对多单向、双向关联
程序员文章站
2022-04-22 16:10:09
...
一个老师 -- 关联 多个学生
create table STUDENT (
ID NUMBER(16) not null,
STUDENT_NAME VARCHAR(20),
TEACHER_ID NUMBER(16),
constraint PK_STUDENT primary key (ID)
);
create table TEACHER (
ID NUMBER(16) not null,
TEACHER_NAME VARCHAR(22),
constraint PK_TEACHER primary key (ID)
);
---PO---
public class Teacher implements java.io.Serializable {
// Fields
private Long id;
private String teacherName;
public class Student implements java.io.Serializable {
// Fields
private Long id;
private String studentName;
private Long teacherId;
private Teacher teacher; // 老师
配置文件
<hibernate-mapping>
<class name="com.po.Teacher" table="TEACHER">
<id name="id" type="java.lang.Long">
<column name="ID" precision="16" scale="0" />
<generator class="sequence" />
</id>
<property name="teacherName" type="java.lang.String">
<column name="TEACHER_NAME" length="22" />
</property>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.po.Student" table="STUDENT">
<id name="id" type="java.lang.Long">
<column name="ID" precision="16" scale="0" />
<generator class="sequence" />
</id>
<property name="studentName" type="java.lang.String">
<column name="STUDENT_NAME" length="20" />
</property>
<property name="teacherId" type="java.lang.Long">
<column name="TEACHER_ID" precision="16" scale="0" />
</property>
<many-to-one name="teacher" class="com.po.Teacher" column="teacher_id" fetch="select" lazy="false" cascade="save-update"></many-to-one>
</class>
</hibernate-mapping>
测试代码
/**
* Test.java
* com.hib
*
* Function: TODO
*
* ver date author
* ──────────────────────────────────
* Mar 31, 2010 程仁银
*
* Copyright (c) 2010, All Rights Reserved.
*/
package com.hib;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.po.Address;
import com.po.Person;
import com.po.Student;
/**
* ClassName:Test
* Project:
* Company: LINKAGE
*
* @author 程仁银
* @version
* @since Ver 1.1
* @Date Mar 31, 2010 1:56:43 PM
*
* @see
*/
public class Test {
/**@author . 程仁银
* @email . [email protected]
* @createTime .Mar 31, 2010 1:56:44 PM
* @parameters .
* @description .
*/
public static void main(String[] args)
{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
// 业务逻辑测试
Query query=session.createQuery("from Student u");
List<Student> list = query.list();
for(Student s:list)
{
System.out.println(
s.getId()+" "+s.getStudentName()+
"======"+s.getTeacher().getTeacherName());
}
tx.commit();
}
}
配置下双向关联
在原有的基础上修改,主要是修改teacher
public class Teacher implements java.io.Serializable {
// Fields
private Long id;
private String teacherName;
private Set<Student> student = new HashSet<Student>();
<class name="com.po.Teacher" table="TEACHER">
<id name="id" type="java.lang.Long">
<column name="ID" precision="16" scale="0" />
<generator class="sequence" />
</id>
<property name="teacherName" type="java.lang.String">
<column name="TEACHER_NAME" length="22" />
</property>
<set name="student" inverse="true" fetch="select" cascade="save-update" lazy="true">
<key column="teacher_id"></key>
<one-to-many class="com.po.Student"/>
</set>
</class>