Hibernate单表继承与自联接

时间:2014-01-16 16:48:00

标签: java hibernate

我有以下课程:

指南课:

public abstract class Guide
{
    private Long idGuide;
    private String name;

    private GuideContainer parent;
    /** GETTERS & SETTERS*/
}

GuideContainer:

public class GuideContainer extends Guide
{    
   private Guide children;
/** GETTER & SETTERS */
}

GuideFile:

public class GuideFile extends Guide
{
   private String uri;
/**GETTERS & SETTERS */
}

使用以下映射: 对于指南:

<hibernate-mapping>
    <class name="Guide" table="guides" abstract="true">
        <id name="idGuide" type="integer" column="idGuide">
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String" column="name" />
        <discriminator column="type" type="java.lang.String" />

        <many-to-one class="GuideContainer" fetch="join" name="parent">
            <column name="parent" />
        </many-to-one>
    </class>
</hibernate-mapping>

GuideFile:

<hibernate-mapping>
    <subclass extends="Guide" name="GuideFile" discriminator-value="file">
        <property name="uri" type="java.lang.String" column="uri" />
    </subclass>
</hibernate-mapping>

GuideContainer:

<hibernate-mapping>
    <subclass extends="Guide" name="GuideContainer" discriminator-value="container">
        <set fetch="select" inverse="true" lazy="true" name="children" sort="unsorted" table="children">
            <key>
                <column name="parent" not-null="false" />
            </key>
            <one-to-many class="Guide" />
        </set>
    </subclass>
</hibernate-mapping>

当我尝试获取给定父母的所有指南时

 Query query = getSession().createQuery("from Guide g where parent = :parent order by type").setParameter("parent", parent);
        List<Guide> guides= query.list();
        return guides;

我收到以下异常:

 IllegalArgumentException occurred calling getter of Guide.idGuide

之后:

java.lang.IllegalArgumentException: object is not an instance of declaring class

我做错了什么?

1 个答案:

答案 0 :(得分:0)

解决。查询应该是:

from Guide g where parent.idGuide = :parent order by type