Hibernate XML Mapping单向(无外键)

时间:2016-10-05 16:55:36

标签: hibernate hibernate-mapping

我一直致力于遗留应用程序(重要的是要声明,因为我无法更改数据结构)

我有一个查找表,其中包含两个表的ID,我试图使用单向一对一映射从两个表中获取描述

我还要提一下,尽管person表有Address_ID和Secondary_Address_id,但它们不是数据库中Address和Secondary_address_table的外键

当我运行查询时,我将addressDescription和secondaryAddressDescription视为null,同时尝试使用getHibernateTemplate.merge(person)插入表Person时

我得到错误Hibernate Mismatch Exception for class Address expecting java.lang .long但是得到了整数

下面列出的是Hibernate映射文件和带有它的POJO

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <class name="com.sample.Person" table="Person">
        <id name="personId" type="java.lang.Integer" column="PERSON_ID">
            <generator class="native" />
        </id>



        <property name="addressId" column="ADDRESS_ID" type="java.lang.Long" />
        <property name="secondaryAddressId" column="SECONDARY_ADDRESS" type="java.lang.Integer" />
        <property name="primary" column="PRIMARY" type="java.lang.String" />

        <one-to-one name="address" class="com.sample.Address" cascade="none" foreign-key="addressId" >

        </one-to-one>

        <one-to-one name="secondaryAddress" class="com.sample.SecondaryAddress" cascade="none" foreign-key="secondaryAddressId">

        </one-to-one>

    </class>

    <query name="Address.getAddressById">
        <![CDATA[
  Select per.personId as personId,per.addressId as addressId,
                       per.secondaryAddressId as secondaryAddressId,, ad.description as addressDescription, sad.description as secondaryAddressDescription
                        from Person per left join per.address ad left join per.secondaryAddress sad  where
                         per.personId=:personId
   ]]>
    </query>

</hibernate-mapping>

地址的Hibernate映射

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <class name="com.Address" table="ADDRESS">
        <id name="addressId" type="java.lang.Long" column="ADDRESS_ID">
            <generator class="native" />
        </id>

        <property name="description" column="DESCRIPTION" type="java.lang.String" not-null="true"/>

    </class>

</hibernate-mapping>

SecondaryAddress的Hibernate映射

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <class name="com.SecondaryAddress" table="SECONDARY_ADDRESS">
        <id name="secondaryAddressId" type="java.lang.Integer" column="SECONDARY_ADDRESS_ID">
            <generator class="native" />
        </id>

        <property name="description" column="DESCRIPTION" type="java.lang.String" not-null="true"/>

    </class>

</hibernate-mapping>

POJO for Person(不粘贴getter / setter以获得更少的代码)

public class Person implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -1186272503323568780L;



    private Integer personId;
    private Long addressId;
    private Integer secondaryAddressId;
    private String addressDescription;
    private String secondaryAddressDescription;
    private Integer address;
    private String secondaryAddress;

  }

0 个答案:

没有答案