How to map a native query, that has more columns than a table?

424 Views Asked by At

I have a class with 4 fields, a table with 2 columns and a native query, that return 4 columns. let's say: a class:

class Foo{
    int id;
    String name;
    int stat;
    String statName;
}

a table:

foo
---------
id | name

and mapping:

<class name="Foo" table="foo">
    <id name=id/>
    <property name="name"/>
    <property name="stat"/>
    <property name="statName"/>
</class>
<sql-query name="getWithStat">
    <return class="Foo"/>
    <!--stat and statName calculated as aggregation and concatenation from other table-->
</sql-query>

But with this mapping, I can't use basic entity, because table hasn't columns for stat and statName. How shoul I map this extra fields from my query into my class?

1

There are 1 best solutions below

1
On
you can use Transient annotation of JPA to ignore property at time of persist.

class Foo{
    int id;
    String name;
    @Transient
    int stat;
    @Transient
    String statName;
}