灵活的Hibernate命名查询映射

时间:2014-02-28 06:13:47

标签: java hibernate

我正在使用映射到同一个hibernate类的多个hibernate命名查询。

问题是因为不同的命名查询返回不同的属性,而不是所有属性我都收到“无效列”错误。解决它的方法是确保查询映射hibernate类定义的所有属性。 (Hibernate native query : Invalid Column Name Error SQL-17006

以下是我所拥有的样本:

<hibernate-mapping>

    <class name="com.company.ObjectA" mutable="false" >
        <id name="id" type="string"/>
        <property name="prop1" type="string"/>
        <property name="prop2" type="string"/>
    </class>

    <sql-query name="get1">
        <return alias="a" class="com.company.ObjectA"/>
        <![CDATA[
        select  
                id as {a.id},  
                prop1 as {a.prop1}
        from TABLE_A
        ]]>
    </sql-query>

    <sql-query name="get2">
        <return alias="a" class="com.company.ObjectA"/>
        <![CDATA[
        select  
                id as {a.id},  
                prop2 as {a.prop2}
        from TABLE_A
        ]]>
    </sql-query>

</hibernate-mapping>

我已经定义了一个临时修复,其中查询返回空值以确保映射所有hibernate类属性:

    <sql-query name="get2">
        <return alias="a" class="com.company.ObjectA"/>
        <![CDATA[
        select  
                id as {a.id},  
                '' as {a.prop1},  
                prop2 as {a.prop2}
        from TABLE_A
        ]]>
    </sql-query>

但这并不理想,因为对于我的实际案例,我有很多这些未映射的属性,以及在hibernate类中使用不同属性组合的更多命名查询。

还有其他方法可以解决吗?此外,我需要它将数据返回到同一个对象'ObjectA',因为它与其余代码集成。

0 个答案:

没有答案
相关问题