2

我有两个具有一对一关系的复合 id 表。

表一:PK 列:column1、column2 表二:PK 列:column3、column4。

列具有不同的名称。我必须将 column1 映射到 column3 并将 column2 映射到 column4。表 1(父表)声明:

<class name="com.java.TABLE1" table="TABLE1">
        <composite-id name="TABLE1_pk" class="com.tc4.dal.TABLE1_PK">
            <key-property name="COLUMN1" column="COLUMN1" />
            <key-property name="COLUMN2" column="COLUMN3" />
        </composite-id>
...remaining properties declarations...

<one-to-one name="TABLE2" class="com.tc4.dal.TABLE2"
            cascade="save-update, delete">
             <key column="COLUMN3" property-ref="COLUMN1" />
            <key column="COLUMN4" property-ref="COLUMN2" />
</one-to-one> 
</class>

Table2(子表)映射

<class name="com.java.TABLE2" table="TABLE2">
        <composite-id name="TABLE2_pk" class="com.tc4.dal.TABLE2_PK">
            <key-property name="COLUMN3" column="COLUMN3" />
            <key-property name="COLUMN4" column="COLUMN4" />
        </composite-id>
</class>

启动应用程序时,出现以下异常

  Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/JAVA/TABLE1.hbm.xml
    Caused by: org.xml.sax.SAXParseException: The content of element type "one-to-one" must match "(meta*,formula*)".
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:196)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:132)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:390)

在这种情况下,如何在映射文件中给予一对一的关联。

问候,纳雷什。

4

2 回答 2

1

像在父表中一样重命名子表复合键列已经解决了这个问题。详细说明:- - 父表(TABLE1):

<class name="com.java.TABLE1" table="TABLE1">
            <composite-id name="TABLE1_pk" class="com.tc4.dal.TABLE1_PK">
                <key-property name="COLUMN1Prop" column="COLUMN1" />
                <key-property name="COLUMN2Prop" column="COLUMN3" />
    </composite-id>
    .... remaining properties
    </class>

// 由于TABLE1有composit key,需要为TABLE1创建主键POJO

public class TABLE1_PK {
    public String COLUMN1Prop;
    public String COLUMN2Prop;
  }

表 1 实体:

public class TABLE1{
  // declare PK
  private TABLE1_PK TABLE1_pk;
// getters and setters and remaining properties
}
  • 子表(TABLE2):

     <class name="com.java.TABLE2" table="TABLE2">
                         <composite-id name="TABLE2_pk" class="com.tc4.dal.TABLE2_PK">
                             <key-property name="COLUMN1Prop" column="COLUMN3" />
                             <key-property name="COLUMN2Prop" column="COLUMN4" />
                 </composite-id>
                 .... remaining properties
          </class>
    

子实体 PK

public class TABLE2_PK{
// here property names should be defined as parent Entity properties
        public String COLUMN1Prop;
        public String COLUMN2Prop;
.. getters and setters
}
于 2013-05-06T08:58:52.333 回答
1

使用注释你可以像这样实现它..在表1中

 @OneToOne(targetEntity = TABLE2.class, fetch = FetchType.LAZY)
   @JoinColumns(value = {
         @JoinColumn(name = "COLUMN1", referencedColumnName = "COLUMN3"),
         @JoinColumn(name = "COLUMN2", referencedColumnName = "COLUMN4") })
  TABLE2 table2

用 XMl 试试这个,让我知道

<class name="com.java.TABLE1" table="TABLE1">
        <composite-id name="TABLE1_pk" class="com.tc4.dal.TABLE1_PK">
            <key-property name="COLUMN1PROP" column="COLUMN1" />
            <key-property name="COLUMN2PROP" column="COLUMN3" />
        </composite-id>
...remaining properties declarations...

<one-to-one name="TABLE2" class="com.tc4.dal.TABLE2"
            cascade="save-update, delete">
             <key column="COLUMN1PROP" property-ref="COLUMN3PROP" />
            <key column="COLUMN2PROP" property-ref="COLUMN4PROP" />
</one-to-one> 
</class>

<class name="com.java.TABLE2" table="TABLE2">
        <composite-id name="TABLE2_pk" class="com.tc4.dal.TABLE2_PK">
            <key-property name="COLUMN3PROP" column="COLUMN3" />
            <key-property name="COLUMN4PROP" column="COLUMN4" />
        </composite-id>
</class>

或者

<class name="com.java.TABLE1" table="TABLE1">
        <composite-id name="TABLE1_pk" class="com.tc4.dal.TABLE1_PK">
            <key-property name="COLUMN1PROP" column="COLUMN1" />
            <key-property name="COLUMN2PROP" column="COLUMN3" />
        </composite-id>
...remaining properties declarations...

<one-to-one name="TABLE2" class="com.tc4.dal.TABLE2"
            cascade="save-update, delete">
             <key column="COLUMN1" property-ref="COLUMN3PROP" />
            <key column="COLUMN2" property-ref="COLUMN4PROP" />
</one-to-one> 
</class>

<class name="com.java.TABLE2" table="TABLE2">
        <composite-id name="TABLE2_pk" class="com.tc4.dal.TABLE2_PK">
            <key-property name="COLUMN3PROP" column="COLUMN3" />
            <key-property name="COLUMN4PROP" column="COLUMN4" />
        </composite-id>
</class>

让我知道哪个有效。:)

于 2013-04-29T12:43:28.567 回答