配置文件:
班级:
<hibernate-mapping>
<class name="bjca.testHibernate.entity.impl.ClassInfo" table="t_class">
<id name="claId" type="java.lang.Integer">
<column name="claId" />
<generator class="native" />
</id>
<property name="claName" type="java.lang.String">
<column name="claName" length="50" not-null="true" />
</property>
<set name="students" inverse="true">
<key column="classId"></key>
<one-to-many class="bjca.testHibernate.entity.impl.StudentInfo"/>
</set>
</class>
</hibernate-mapping>
学生
<hibernate-mapping>
<class name="bjca.testHibernate.entity.impl.StudentInfo" table="t_student">
<id name="stuId" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="stuName" type="java.lang.String">
<column name="stuName" length="50" not-null="true" />
</property>
<many-to-one name="classes" column="classId"
class="bjca.testHibernate.entity.impl.ClassInfo" cascade="all"/>
</class>
</hibernate-mapping>
插入一个班级:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into t_class (claName, claId) values (?, ?)
11111
插入一个学生:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into t_student (stuName, classId, id) values (?, ?, ?)
11111
修改一条学生信息:
11111
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select studentinf0_.id as id0_, studentinf0_.stuName as stuName0_,
studentinf0_.classId as classId0_ from t_student studentinf0_ where studentinf0_.id=10
Hibernate: select classinfo0_.claId as claId1_, classinfo0_.claName as claName1_ from t_class
classinfo0_ where classinfo0_.claId=11
Hibernate: select studentinf0_.id as id0_0_, studentinf0_.stuName as stuName0_0_,
studentinf0_.classId as classId0_0_ from t_student studentinf0_ where studentinf0_.id=?
Hibernate: update t_student set stuName=?, classId=? where id=?
Hibernate: update t_class set claName=? where claId=?
修改一条班级信息:
11111
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select classinfo0_.claId as claId1_, classinfo0_.claName as claName1_ from t_class
classinfo0_ where classinfo0_.claId=11
Hibernate: update t_class set claName='大象二班' where claId=11
班级信息级联 all
11111
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select studentinf0_.id as id0_, studentinf0_.stuName as stuName0_,
studentinf0_.classId as classId0_ from t_student studentinf0_ where studentinf0_.id=12
Hibernate: select classinfo0_.claId as claId1_, classinfo0_.claName as claName1_ from t_class
classinfo0_ where classinfo0_.claId=11
Hibernate: select studentinf0_.id as id0_0_, studentinf0_.stuName as stuName0_0_,
studentinf0_.classId as classId0_0_ from t_student studentinf0_ where studentinf0_.id=?
Hibernate: update t_class set claName=? where claId=?
结论:
1对多,多的那方 cascade="all"
**********************************
1方, 班级:
**********************************
实体类包含set对象,放置多方对象 名自取
配置文件中配置<set>标记,标记name取本方实体中的对方set对象的名字,即set的名字 ,
<set> 下的 <key> 指向多方主键
<set> 下的 <one-to-many> 指向多方对象
***********************************
多方 学生:
***********************************
实体类包含1方对象,名自取
配置文件中配置<many-to-one> 指向1方对象 name 属性取本方实体方法中的对方对象的名字。
posted on 2009-09-10 17:38
super_nini 阅读(377)
评论(0) 编辑 收藏