Hibernate 对应关系配置

原文传送门:http://zackey.javaeye.com/blog/192440

 

配置实例一:

特殊配置:

<hibernate-mapping package="net.share_info.dxw.hibernate"> <class name="Smkhabitword" table="SMKHABITWORD" node=""> <id name="id" column="ID" type="java.lang.String" node=""> <generator class="uuid.hex" /> </id> <property name="sortid" column="SORTID" type="java.lang.String" /> <property name="addtime" column="ADDTIME" type="java.util.Date" /> <property name="msg" column="MSG" type="java.lang.String" /> <property name="sortname" formula="( select s.sortname from SMKHABITWORDSORT s where s.id = sortid )" type="java.lang.String"> </property> </class> </hibernate-mapping>

一对多配置:

<hibernate-mapping> <class name="com.shareinfo.model.Peruserinfo" table="Peruserinfo"> <!--hibernate为我们生成主键id--> <id name="userid" type="long"> <generator class="sequence"> <param name="sequence">peruserinfo_userid</param> </generator> </id> <property name="username"/> <property name="password"/> <set name="books" lazy="true" inverse="true" cascade="all" > <key column="peruserid"/> //这个是Book表的外键(book外的一个字段) <one-to-many class="com.shareinfo.model.Bookinfo"/> </set> </class> </hibernate-mapping>

多对多配置:

<set name="courses" table="Student_Course_Link" //中间表 lazy="false" inverse="false" cascade="all" sort="unsorted" > <key column="StudentId" // Student_Course_Link的StudentId /> <many-to-many class="com.hellking.study.hibernate.Course" column="CourseId" // Student_Course_Link中的CourseId(意思就是用这个CourseId关联Course的主键) outer-join="auto" />

一对多的配置:

<set name="books" lazy="false" inverse="true" cascade="all" > <key column="peruserid"/> // books表的peruserid <one-to-many class="com.shareinfo.model.Bookinfo"/> </set>

多对一的配置:(学生映射表可以这样配置)

<many-to-one name="classes" class="com.hellking.study.hibernate.Classes" cascade="none" outer-join="auto" update="true" insert="true" column="ClassesId" //当前表的ClassesId />

一对一的配置:

<one-to-one name="address" class="com.hellking.study.hibernate.Address" cascade="none" outer-join="auto" constrained="false" />

你可能感兴趣的:(Hibernate,table,Class,insert,generator,books)