2013-11-15 39 views
0
<hibernate-mapping> 
    <class name="Employee" table="EMPLOYEE"> 
     <meta attribute="class-description"> 
     This class contains the employee detail. 
     </meta> 
     <id name="id" type="int" column="id"> 
     <generator class="native"/> 
     </id> 
     <property name="firstName" column="first_name" type="string"/> 
     <property name="lastName" column="last_name" type="string"/> 
     <property name="salary" column="salary" type="int"/> 
    </class> 
</hibernate-mapping> 

正如你可以在上面映射文件ID,請參見Key自己,而不是自動生成的使用生成器類=「本地」有沒有一種方法,我可以將主要處於休眠

所以是自動生成的有一種方法可以根據我的java邏輯來設置這個ID。 我不希望它爲我自動生成,我想用我自己的邏輯來生成它,然後插入表中,也有可能是當我運行代碼插入表中的值,然後表中在數據庫中自動創建,如果它不存在那裏,我聽說有一種方法可以做到這一點。

回答

1

如果您沒有爲主鍵屬性指定任何生成器,hibernate會假設您使用assign策略。 assign策略告訴休眠你會自己設置id,所以hibernate不會幫你定義id值。 (Hibernate會只是拋出一個異常,如果您嘗試堅持實體與空/已有ID)

所以這樣定義你的映射:

<hibernate-mapping> 
<class name="Employee" table="EMPLOYEE"> 
    <meta attribute="class-description"> 
    This class contains the employee detail. 
    </meta> 
    <id name="id" type="int" column="id"/> 
    <property name="firstName" column="first_name" type="string"/> 
    <property name="lastName" column="last_name" type="string"/> 
    <property name="salary" column="salary" type="int"/> 
</class> 
</hibernate-mapping> 
+0

謝謝,upvoted .. –

+0

不客氣。如果您的問題解決了,請不要忘記接受其中一個答案。 – ben75

+0

完成............... –

相關問題