TEXHIK
TEXHIK

Reputation: 1398

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

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?

Upvotes: 1

Views: 478

Answers (1)

samji
samji

Reputation: 31

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

Upvotes: 1

Related Questions