映射文件<hibernate-mapping> <class name="org.gecs.hibernate.test.AdDepartment" table="AD_DEPARTMENT" schema="BARCODE" dynamic-insert="true" dynamic-update="true"> <id name="adDepartmentId" type="long"> <column name="AD_DEPARTMENT_ID" precision="22" scale="0" /> <generator class="sequence"> <param name="sequence">AD_DEPARTMENT_SEQ</param> </generator> </id> <property name="departmentName" type="string"> <column name="DEPARTMENT_NAME" length="50" not-null="true" /> </property> <property name="active" type="java.lang.Character" insert="false" update="true"> <column name="ACTIVE" length="1" /> </property> <property name="createdTime" type="date" insert="false" update="false"> <column name="CREATED_TIME" length="7" /> </property> <property name="createdUser" type="string"> <column name="CREATED_USER" length="20" not-null="true" /> </property> <many-to-one name="parentDepartment" column="PARENT_DEPARTMENT_ID"/>
<set name="childDepartment" cascade="save-update" lazy="true" inverse="true">
<key column="PARENT_DEPARTMENT_ID"/>
<one-to-many class="org.gecs.hibernate.test.AdDepartment"/>
</set> </class></hibernate-mapping>表结构: 测试代码:public void testSave() {
AdDepartment department = new AdDepartment();
department.setDepartmentName("華南資訊處");
department.setCreatedUser("bob");
AdDepartment child_department = new AdDepartment();
child_department.setDepartmentName("PCBA應用系統部");
child_department.setCreatedUser("bob");
child_department.setParentDepartment(department);
//
department.getChildDepartment().add(child_department);
Transaction tran = session.beginTransaction();
session.save(department);
//session.save(child_department);
tran.commit();
}
效果:
posted on 2011-07-07 15:35
Ke 阅读(1383)
评论(1) 编辑 收藏 所属分类:
hibernate