每个层次结构的表加载多对一关系的问题

时间:2017-11-20 06:15:45

标签: java hibernate

我已按层次结构创建表

员工在单个表中有两个实体 OfficeAddress HomeAddress

使用HomeAddress系统加载Employee1的实体时抛出参数类型不匹配异常,

执行以下查询系统时会抛出异常。

Query createQuery = session.createQuery("from com.example.Employee e join fetch e.homeAddress as h where e.empId = 1");

以下是班级结构

<hibernate-mapping>
    <class name="com.example.Employee" table="employee">

        <id name="empId" column="emp_id" type="int">
            <generator class="increment" ></generator>
        </id>
        <property name="name" column="name" type="java.lang.String"></property>

        <property name="addressFk" column="address_fk" type="java.lang.Integer" not-null="true"/>


        <many-to-one name="officeAddress" class="com.example.OfficeAddress" fetch="select" not-null="false"  insert="false" update="false" >
            <column name="address_fk" not-null="false" />
        </many-to-one>


        <many-to-one name="homeAddress" class="com.example.HomeAddress" fetch="select" not-null="false"  insert="false" update="false" >
            <column name="address_fk" not-null="false" />
        </many-to-one>


    </class>


    <class name="com.example.Address" table="address" discriminator-value="addr">
        <id name="addressId" column="addr_id" type="int">
            <generator class="increment"></generator>
        </id>
        <discriminator column="type" type="string"></discriminator>

        <subclass name="com.example.OfficeAddress" discriminator-value="office_emp">
            <property name="officeAddress" column="office_address" type="java.lang.String"></property>
        </subclass>

        <subclass name="com.example.HomeAddress" discriminator-value="home_emp">
            <property name="homeAddress" column="home_address" type="java.lang.String"></property>
        </subclass>

    </class>


</hibernate-mapping>

public class Employee {
    private int empId;
    private String name;
    private OfficeAddress officeAddress;
    private HomeAddress homeAddress;
    private int addressFk;
    getXXX();
    setXXX()
}

public class OfficeAddress extends Address {
    private String officeAddress;
    getXXX();
    setXXX()
}
public class HomeAddress extends Address {

    private String homeAddress;
    getXXX();
    setXXX()
}

下面是堆栈跟踪

Exception in thread "main" IllegalArgumentException occurred while calling setter for property [com.example.Employee.officeAddress (expected type = com.example.OfficeAddress)]; target = [com.example.Employee@7eecb5b8], property value = [com.example.HomeAddress@71687585]
    at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:123)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:713)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:362)
    at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:4718)
    at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:188)
    at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1114)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
    at org.hibernate.loader.Loader.doQuery(Loader.java:920)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
    at org.hibernate.loader.Loader.doList(Loader.java:2553)
    at org.hibernate.loader.Loader.doList(Loader.java:2539)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
    at org.hibernate.loader.Loader.list(Loader.java:2364)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
    at com.example.StoreData.select(StoreData.java:41)
    at com.example.StoreData.main(StoreData.java:22)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:68)
    ... 20 more

可在Git Repository

获取存储库

如果有人知道如何解决这个问题,请告诉我。

1 个答案:

答案 0 :(得分:0)

您应该为您的POJO类提供getter和setter方法,例如Employee,HomeAddress和Office Address。

Hibernate查找用于映射的setter和getter方法。