1

I'm using multiple hibernate named queries that maps to the same hibernate class.

The problem is because the different named queries return different attributes, and not all the attributes I'm getting an "invalid column" error. The way to resolve it is to make sure the queries map all the attributes defined by the hibernate class. (Hibernate native query : Invalid Column Name Error SQL-17006)

Here's an sample of what I have:

<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>

I have defined a temporary fix where the queries return empty values to make sure all hibernate class attributes are mapped:

    <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>

But that is not ideal, because for my real case, I have quite a few of these unmapped attributes, and more named queries that use a different combination of attributes in the hibernate class.

Is there another way to resolve it? Also I need it to return data to the same object 'ObjectA', because of it's integration with the rest of the code.

Community
  • 1
  • 1
JackDev
  • 11,003
  • 12
  • 51
  • 68
  • 1
    Why are you using SQL queries instead of JPQL? Why aren't you selecting entities instead of some columns. And above all: why do you make these methods return instances of entities, although what is actually returned is not the entity state at all. This is extremely confusing and dangerous: the caller will think that the prop1 of the entity is an empty string, although it's not. Use different objects if you really want to return different things. – JB Nizet Feb 28 '14 at 07:31
  • Hi JB. SQL is used because we have a massive database, and the SQL is more complicated than the sample above - with multiple table joins where conditions and columns for different tables. Not all the tables used in the SQL is mapped. I'm will put it into view and have different objects at a later stage, but at the moment time + costing + risk do not allow. Good comment never the less. – JackDev Mar 02 '14 at 23:51

0 Answers0