2012-07-06 60 views
0

在我的應用程序中,我沒有在一個表中使用主鍵列。我將其視爲代理鍵並嘗試使用tag.but我得到了異常。如果Hibernate的表中沒有ID列,該怎麼辦?

**"org.hibernate.MappingException: Error reading resource:  
    com/mapping/FamilyDetails.hbm.xml"** 

**Caused by: org.xml.sax.SAXParseException: 
    The content of element type "class" must match " 
    (meta*,subselect?,cache?,synchronize*,comment?, 
    (id|composite-id),discriminator?,natural-id?,(version|timestamp)?, 
    (property|many-to-one|one-to-one|component|dynamic- 
    component|properties|any|map|set|list|bag|idbag|array| 
    primitive-array|query-list)*, 
    ((join*,subclass*)|joined-subclass*|union-subclass*),loader?, 
    sql-insert?,sql-update?,sql-delete?,filter*)".** 

映射文件:-FamilyDetails.hbm.xml

<class name="com.beans.FamillyDetails" 
     table="personal_fmly_dtls" proxy="com.aims.beans.FamillyDetails"> 


    <natural-id> 
     <property name="systemid" /> 
    </natural-id> 
    <property name="membername" 
       type="java.lang.String" column="membername" not-null="true" /> 
    <property name="relation" 
       type="java.lang.String" column="relation" not-null="true" /> 
    <property name="dateOfBirth" 
       type="java.util.Date" column="dateofbirth" not-null="true" /> 

    <many-to-one name="Personal" column="pid" class="com.aims.beans.Personal"/> 

</class> 



<?xml version="1.0" encoding='UTF-8'?> 
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 
<hibernate-mapping> 
<class name="com.aims.beans.Personal" 
     table="personalinfo" proxy="com.aims.beans.Personal"> 

<id name="pid" column="pid" type="java.lang.Integer"> 
    <generator class="sequence"> 
     <param name="sequence">personal_seq</param> 
    </generator> 
</id> 
<property name="name" 
      type="java.lang.String" column="name" not-null="true" /> 
<property name="deptno" 
      type="java.lang.Integer" column="DEPTNO" not-null="true" /> 

<component name="permentaddress" class="com.aims.beans.Address"> 
    <property name="homeAddress1" type="java.lang.String" column="pddress1"/> 
    <property name="homeAddress2" type="java.lang.String" column="pddress2"/> 
    <property name="homecity"  type="java.lang.String" column="pcity"/> 
    <property name="homeState" type="java.lang.String" column="pstate"/> 
</component> 
<set name="famildtlslst"> 
    <key column="pid" not-null="true"/> 
    <one-to-many class="com.aims.beans.FamillyDetails"/> 
</set> 
<join table="address">  
    <key column="pid" /> 
    <component name="Address" class="com.aims.beans.Address"> 
     <property name="homeAddress1" 
        type="java.lang.String" column="HomeAddress1"/> 
     <property name="homeAddress2" 
        type="java.lang.String" column="HomeAddress2"/> 
     <property name="homecity" 
        type="java.lang.String" column="homecity"/> 
     <property name="homeState" 
        type="java.lang.String" column="homestate"/> 
    </component> 
</join> 

請幫助我。

+0

java hibernate中xml-mapping過時了嗎?我的意思是,在編輯器和重構工具的幫助下,寫起來很容易。它從實體實現中分離映射。我也不確定你是否可以用註釋做所有你可以用xml做的事情。但我不熟悉java,我正在使用NHibernate和C#。 – 2012-07-06 12:49:10

回答

2

它可能是另一個組件。組件不需要主鍵:

<set name="famildtlslst" table="personal_fmly_dtls"> 
    <key column="pid" not-null="true"/> 
    <composite-element parent="Personal"> 
    <property name="systemid" /> 
    <property name="membername" not-null="true" /> 
    <property name="relation" not-null="true" /> 
    <property name="dateOfBirth" not-null="true" /> 
    </composite-element> 
</set> 

當然是另一種語義的,但如果你是幸福的一個組成部分,它更容易。

+0

因此,它不需要任何映射文件。它很好。但一般如果表沒有任何主鍵,什麼是替代方式?但我試圖使用natural-id.is正確的方式使用自然id。 – user1357722 2012-07-06 12:52:27

+0

沒有辦法映射沒有主鍵的實體。看看這個問題在這裏:http://stackoverflow.com/questions/767277/hibernate-and-no-pk – 2012-07-06 13:05:12

相關問題