hibernate--リレーショナルマッピング

9133 ワード

              ,            ,    
Hibernate : 1、 N-1 2、 1-1 3、 1-N 4、 N-N 5、 1-N 6、 N-N 7、 1-1 : 、 POJO public class Person{ private int pid; private String name; private Address address; ...// getter、setter } ---------------------------------------- public class Address{ private int id; private String detail; ...// getter、setter } Person , N-1 ** <class name="Person"> <id name="id"> <generator class="native"/> </id> ...// <many-to-one name="address" column="addressId"/>// </class> hibernate (Person) addressId 、 1-1( ) <many-to-one> , unique=true, : <many-to-one name="address" column="addressId" unique="true"/> 、 1-N ** , , 1-N public class Classes { private int id; private String name; private Set students; ...// getter、setter } --------------------------------------------------------------------------- public class Student { private int id; private String name; ..// getter、setter } ** : , , ** : <class name="Classes" table="t_classes"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="students"> <key column="classesid"/>// ( ) <one-to-many class="xxx.Student"/>// </set> </class> 、 1-N 1-N , 1-N N-N, <one-to-many> , <many-to-many> , , unique="true" ** , 1-N <class name="xxx.Classes" table="t_classes"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="students"> <key column="classesid"/> <many-to-many class="xxx.Student" unique="true"/> </set> </class> 、 ** POJO public class User { private int id; private String name; private Set roles; ..// getter、setter } --------------------------------------------------------------------------- public class Role { private int id; private String name; ..// getter、setter } N-N , User Role, Role User N-N N-1 ** <class name="xxx.User" table="t_user"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="roles" table="t_user_role"> <key column="userid"/> <many-to-many class="xxx.Role" column="roleid"/> </set> </class> t_user t_user_role t_role id name < -------userid roleid ----- > id name 、 , N-N Role public class Role { private int id; private String name; privarte Set user ..// getter、setter } <class name="xxx.User" table="t_user"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="roles" table="t_user_role"> <key column="userid"/> <many-to-many class="xxx.Role" column="roleid"/> </set> </class> ------------------------------------------------------------------------------------------------ <class name="xxx.Role" table="t_role"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="users" table="t_user_role" order-by="userid"> <key column="roleid"/> <many-to-many class="xxx.User" column="userid"/> </set> </class> :1、 2、 Set table , 、 1-1 : 1、 1-1 1-1 , N-1 , <many-to-one> unique="true" 1-1 , ? : User identityId POJO public class Person { private int id; private String name; private IdentityId identityId; ..// getter、setter } public class IdentityId { private int id; private String cardNo; private Person person; ..// getter、setter } ** <class name="xxx.Person" table="t_person"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <many-to-one name="identityId" class="xxx.IdentityId" unique="true"/> </class> ----------------------------------------------------------------------------------- <class name="xxx.IdentityId" table="t_identity"> <id name="id"> <generator class="native"/> </id> <property name="cardNo"/> <one-to-one name="person" class="xxx.Person" property-ref="identityId"/> </class> : property-ref identity identity person . identity getIdentityId() Person : Person IdentityId person.getIdentityId().getPerson() 2、 1-1 POJO , <class name="xxx.Person" table="t_person"> <id name="id" column="ID"> <generator class="foreign"> <param name="property">identityId</param> </generator> </id> <property name="name"/> <one-to-one name="identity" clsss="xxx.IdentityId" constrained="true"/> </class> : constrained="true" Person ID IdentityId -------------------------------------------------------------------------------------- <class name="xxx.Identity" table="t_identity"> <id name="id"> <generator class="native"/> </id> <property name="cardNo"/> <one-to-one name="person" class="xxx.Person"/> </class> : Person ID IdentityId, foreign Hibernate Person IdentityId


                                         
                                   (   )    (   )            

             ,       ,     ,             ,            。
                  ,       ,                       ?       ,     。       ,      ,       。
                   。