hibernate 一对多单向、双向关联
一个老师 -- 关联 多个学生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 {// Fieldsprivate Long id;private String teacherName;public class Student implements java.io.Serializable {// Fieldsprivate 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 /> </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 /> </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" 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, 20101:56:43 PM * * @see */public class Test {/**@author . 程仁银 * @email . 13813375172@139.com *her_id"></key> <one-to-many class="com.po.Student"/> </set> </class>