Hibernate学习笔记(二)【标识符及基本级联配置】

标识符

increment 用于代理主键.由Hibernate自增方式生成标识符,每次增量为1
identity  同上。由底层数据库生成标识符.前提是要数据库支持自增
sequence  同上。由底层数据库生成标识符.前提是要数据库支持序列
hilo      同上。由Hibernate根据high/low算法生成标识符
native    同上。根据数据库的支持,选择identity,sequence或hilo
uuid.hex  同上。由Hibernate采用UUID算法生成字符串形式的标识符
assigned  用于自然主键。由Java代码通过setId()生成标识符。

数据库可使用的标识符

几种常用数据库可以使用的标识符生成器:
1.      MySQL:identity,increment,hilo,native
2.      MS SQL Server:identity,increment,hilo,native
3.      Oracle:sequence,seqhilo,hilo,increment,native
4.      跨平台:native

 

IdentityTester.hbm.xml

 

<hibernate-mapping>
    <class name="mypack.IdentityTester" table="IDENTITY_TESTER">
        <id name="id" type="long" column="ID">
            <generator class="identity"/>        
        </id>
               <property name="name" type="string" column="NAME"/>
    </class>
</hibernate-mapping>

 

IncrementTester.hbm.xml

 

<hibernate-mapping>
    <class name="mypack.IncrementTester" table="INCREMENT_TESTER">       
        <id name="id" type="long" column="ID">
          <meta attribute="scope-set">private</meta>
            <generator class="increment"/>
        </id>
        <property name="name" type="string" column="NAME"/>
    </class>
</hibernate-mapping>

 

NativeTester.hbm.xml

 

<hibernate-mapping>
    <class name="mypack.NativeTester" table="NATIVE_TESTER">
        <id name="id" type="long" column="ID">
            <generator class="native"/>
        </id>
        <property name="name" type="string" column="NAME"/>
    </class>
</hibernate-mapping>

 

HiloTester.hbm.xml 【101 201 301】

 

<hibernate-mapping>
  <class name="mypack.HiloTester"  table="HILO_TESTER">   
    <id name="id" type="long" column="ID">
    <generator class="hilo">
                <param name="table">hi_value</param>
                <param name="column">next_value</param>
                <param name="max_lo">100</param>
        </generator>
    </id>
    <property name="name" type="string" column="NAME" />   
  </class>
</hibernate-mapping>

 

Squence.hbm.xml

 

       <generator class="sequence”>
                <param name="squence”>test_id_seq</param>
        </generator>

 

基础级联 Monkey属于Team

Team.hbm.xml 【inverse="true" cascade="all-delete-orphan"】

 

<hibernate-mapping>
    <class name="mypack.Team" table="TEAMS">       
        <id name="id" type="long" column="ID">      
            <generator class="increment"/>
        </id>
        <property name="name" type="string" column="NAME"/>           
              <set name="monkeys"
                   cascade="all-delete-orphan"
                      inverse="true">
                      <key column="TEAM_ID"/>
                      <one-to-many class="mypack.Monkey"/>
              </set>          
    </class>
</hibernate-mapping>

 

Monkey.hbm.xml 【none delete all delete-orphan all-delete-orphan

 

<hibernate-mapping>
    <class name="mypack.Monkey" table="MONKEYS">
        <id name="id" type="long" column="ID">
            <generator class="increment"/>
        </id>
        <property name="name" type="string" column="NAME"/>       
        <!--mapping with cascade -->      
        <many-to-one
         name="team"
        column="TEAM_ID"
         class="mypack.Team"
         cascade="save-update" <!— none delete all delete-orphan all-delete-orphan  --> 
         lazy="false"  <!-- proxy -->    
        />              
    </class>
</hibernate-mapping>

操作Service类代码片段

 

 

		     tx = session.beginTransaction();
                     Team team = new Team("BULL", new HashSet<Monkey>());
                     Monkey monkey = new Monkey();
                     monkey.setName("Tom");
                     monkey.setTeam(team);
                     team.getMonkeys().add(monkey);
                     session.save(team);
                     tx.commit();
                     tx =session.beginTransaction();
                     Team team =(Team) session.load(Team.class, teamId);
                     Monkey monkey =(Monkey) team.getMonkeys().iterator().next();
                     // 解除team和Monkey的关联关系
                     team.getMonkeys().remove(monkey);
                     monkey.setTeam(null);
                     tx.commit();

 


 

你可能感兴趣的:(java,Hibernate,标识符,identity,基本级联配置)