标签:
<span style="font-size:18px;">package com.bjpowernode.hibernate; public class User { private int id; private String name; private Group group; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Group getGroup() { return group; }</span> public void setGroup(Group group) { this.group = group; } }
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.User" table="t_user"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <many-to-one name="group" column="groupid" cascade="save-update"/> </class> </hibernate-mapping></span>
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.Group" table="t_group"> <id name="id"> <generator class="native"/> </id> <property name="name"/> </class> </hibernate-mapping></span>
<span style="font-size:18px;">package com.bjpowernode.hibernate; public class Person { private int id; private String name; private IdCard idCard; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public IdCard getIdCard() { return idCard; } public void setIdCard(IdCard idCard) { this.idCard = idCard; } }
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.Person" table="t_person"> <id name="id"> <!-- 采用foreign生成策略,forgeign会取得关联对象的标识 --> <generator class="foreign"> <!-- property只关联对象 --> <param name="property">idCard</param> </generator> </id> <property name="name"/> <!--one-to-one指示hibernate如何加载其关联对象,默认根据主键加载 也就是拿到关系字段值,根据对端的主键来加载关联对象 constrained="true表示,当前主键(person的主键)还是一个外键 参照了对端的主键(IdCard的主键),也就是会生成外键约束语句 --> <one-to-one name="idCard" constrained="true"/> </class> </hibernate-mapping></span>
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.IdCard" table="t_idCard"> <id name="id"> <generator class="native"/> </id> <property name="cardNo"/> </class> </hibernate-mapping></span>
<span style="font-size:18px;">package com.bjpowernode.hibernate; public class IdCard { private int id; private String cardNo; private Person person; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCardNo() { return cardNo; } public void setCardNo(String cardNo) { this.cardNo = cardNo; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } }
<span style="font-size:18px;">package com.bjpowernode.hibernate; public class Person { private int id; private String name; private IdCard idCard; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public IdCard getIdCard() { return idCard; } public void setIdCard(IdCard idCard) { this.idCard = idCard; } }
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.IdCard" table="t_idCard"> <id name="id"> <generator class="native"/> </id> <property name="cardNo"/> <one-to-one name="person"/> </class> </hibernate-mapping></span>
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.Person" table="t_person"> <id name="id"> <!-- 采用foreign生成策略,forgeign会取得关联对象的标识 --> <generator class="foreign"> <!-- property只关联对象 --> <param name="property">idCard</param> </generator> </id> <property name="name"/> <!-- one-to-one指示hibernate如何加载其关联对象,默认根据主键加载 也就是拿到关系字段值,根据对端的主键来加载关联对象 constrained="true表示,当前主键(person的主键)还是一个外键 参照了对端的主键(IdCard的主键),也就是会生成外键约束语句 --> <one-to-one name="idCard" constrained="true"/> </class> </hibernate-mapping></span>
<span style="font-size:18px;">public class Person { private int id; private String name; private IdCard idCard; public int getId() { return id; } public void setId(int id) { this.id = id; }</span>
。。。
}
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.Person" table="t_person"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <many-to-one name="idCard" unique="true"/> </class> </hibernate-mapping></span>
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.IdCard" table="t_idCard"> <id name="id"> <generator class="native"/> </id> <property name="cardNo"/> </class> </hibernate-mapping></span>
<span style="font-size:18px;">package com.bjpowernode.hibernate; public class IdCard { private int id; private String cardNo; private Person person; public int getId() { return id; } public void setId(int id) { this.id = id; } 。。。 }
<span style="font-size:18px;">package com.bjpowernode.hibernate; public class Person { private int id; private String name; private IdCard idCard; public int getId() { return id; } public void setId(int id) { this.id = id; } 。。。 }
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.Person" table="t_person"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <many-to-one name="idCard" unique="true"/> </class> </hibernate-mapping></span>
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.IdCard" table="t_idCard"> <id name="id"> <generator class="native"/> </id> <property name="cardNo"/> <one-to-one name="person" property-ref="idCard"/> </class> </hibernate-mapping></span>
<span style="font-size:18px;">package com.bjpowernode.hibernate; import java.util.Set; public class Classes { private int id; private String name; private Set students; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set getStudents() { return students; } public void setStudents(Set students) { this.students = students; } }
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.Student" table="t_student"> <id name="id"> <generator class="native"/> </id> <property name="name"/> </class> </hibernate-mapping></span>
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.Classes" table="t_classes"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="students"> <!-- <key column="classesid" not-null="true"/> --> <key column="classesid"/> <one-to-many class="com.bjpowernode.hibernate.Student"/> </set> </class> </hibernate-mapping></span>
<span style="font-size:18px;">package com.bjpowernode.hibernate; public class Student { private int id; private String name; private Classes classes; public int getId() { return id; } public void setId(int id) { this.id = id; } 。。。 }
<span style="font-size:18px;">public class Classes { private int id; private String name; private Set students; public int getId() { return id; } public void setId(int id) { this.id = id; } 。。。 }
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.Student" table="t_student"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <many-to-one name="classes" column="classesid"/> </class> </hibernate-mapping></span>
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.Classes" table="t_classes"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="students" inverse="true"> <!-- <key column="classesid" not-null="true"/> --> <key column="classesid"/> <one-to-many class="com.bjpowernode.hibernate.Student"/> </set> </class> </hibernate-mapping></span>
* 多对一维护的关系是:多指向一的关系,有了此关系,在加载多的时候可以将一加载上来
* 一对多维护的关系是:一指向多的关系,有了此关系,在加载一的时候可以将多加载上来
<span style="font-size:18px;">public class User { private int id; private String name; private Set roles; public int getId() { return id; } public void setId(int id) { this.id = id; } 。。。 }
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.User" table="t_user"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="roles" table="t_user_role"> <key column="user_id"/> <many-to-many class="com.bjpowernode.hibernate.Role" column="role_id" /> </set> </class> </hibernate-mapping></span>
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.Role" table="t_role"> <id name="id"> <generator class="native"/> </id> <property name="name"/> </class> </hibernate-mapping>
<span style="font-size:18px;">public class Role { private int id; private String name; private Set users; public int getId() { return id; } public void setId(int id) { this.id = id; } 。。。 }
<span style="font-size:18px;">public class User { private int id; private String name; private Set roles; public int getId() { return id; } public void setId(int id) { this.id = id; } 。。。 }
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.Role" table="t_role"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="users" table="t_user_role"> <key column="role_id" not-null="true"/> <many-to-many class="com.bjpowernode.hibernate.User" column="user_id"/> </set> </class> </hibernate-mapping></span>
<span style="font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.hibernate.User" table="t_user"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="roles" table="t_user_role"> <key column="user_id" not-null="true"/> <many-to-many class="com.bjpowernode.hibernate.Role" column="role_id" /> </set> </class> </hibernate-mapping></span>
标签:
原文地址:http://blog.csdn.net/u010097777/article/details/51614953