首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

hibernate 一对多单向、双向联系关系

2012-10-08 
hibernate 一对多单向、双向关联一个老师 -- 关联 多个学生create table STUDENT(IDNUMBER(16)not null,STU

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>

热点排行