hibernate投射心得 原创

hibernate映射心得 原创

Hibernate映射

-------------------------------------单向关联----------------------------------

  一对多 (多对一就反过来)

                在一的一方加集合属性set在多的一方加外键(可选)

                例子:一个人对应多个苹果

Person实体

一:private int id;        

               private String name;

               private int age;

               private Set app=new HashSet();  get  set方法

 

              Apple实体

           多: private int id;

               private String name;

               private String colour;

              private Person per;  (可选)

           映射----------------------------

一的一方:<class name="only.one2many.Person一的一方实体"  table="person表名" catalog="Hibernate数据库名">

           <id name="id">

              <generator class="native自增策略"></generator>

      </id>

            <property name="name实体属性"></property>

           <property name="age实体属性"></property>

           <set name="app集合属性">

              <key column="per_id多的一方的外键关联(在表中显示)"></key>

              <one-to-many class="only.one2many.Apple多的一方实体" />

           </set>

           </class>

   

多的一方:<class name="only.one2many.Apple"  table="apple" catalog="Hibernate">

       <id name="id">

           <generator class="native"></generator>

       </id>

       <property name="name" ></property>

       <property name="colour" ></property>

       <many-to-one name="name" column="per_id多的一方的外键关联" />(可选)

      </class>

 

---------------------------------

一对一:

    在其中一方另一方的引用(实体)  在其中一方加另一方的外键引用()

例子:一夫一妻

    Husband实体

private int id;

    private String name;

    private int age;

    private Wife wf; 

   

    Wife实体

    private int id;       

    private String name;

    private int age;

映射----------------------------------------------

<class name="Husband"  table="husband" catalog="Hibernate">

        <id name="id" column="hus_id">

           <generator class="native"></generator>

       </id>

       <property name="name" ></property>

       <property name="age" column="age"></property>

<many-to-one name="wf引用实体类属性" class="Wife引用的类" unique="falsetrue表示一对一">

           <column name="wif_id数据库外键引用(在表中显示)"></column>

       </many-to-one>

    </class>

-----------------------------------

<class name="Wife"  table="wife" catalog="Hibernate">

       <id name="id" column="wif_id">

           <generator class="native"></generator>

       </id>

       <property name="name"></property>

       <property name="age"></property>

</class>

 

 

 

 

 

 

----------------------------双向关联---------------------

 

 

 

 

 

 

一对多

  在一的一方加集合属性set在多的一方加外键(可选)

                例子:一个人对应多个苹果

Person实体

一:private int id;       

               private String name;

               private int age;

               private Set app=new HashSet();  get  set方法

 

              Apple实体

           多: private int id;

               private String name;

               private String colour;

              private person per; 

           映射----------------------------

一的一方:<class name="only.one2many.Person一的一方实体"  table="person表名" catalog="Hibernate数据库名">

           <id name="id">

              <generator class="native自增策略"></generator>

      </id>

            <property name="name实体属性"></property>

           <property name="age实体属性"></property>

           <set name="app集合属性<span