【问题标题】:How to map a native query, that has more columns than a table?如何映射列多于表的本机查询?
【发布时间】:2018-11-23 13:53:03
【问题描述】:

我有一个包含 4 个字段的类、一个包含 2 列的表和一个返回 4 列的本机查询。 比方说: 一个类:

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

一张桌子:

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

和映射:

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

但是通过这个映射,我不能使用基本实体,因为表没有statstatName 的列。我应该如何将查询中的这些额外字段映射到我的班级?

【问题讨论】:

  • stat 和 statName 字段是否来自其他实体类?
  • @PoojaAggarwal 不,它是在查询中计算的(来自其他表的字段的一些连接和聚合)。

标签: java hibernate xml-configuration


【解决方案1】:
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;
}

【讨论】:

  • 但它不是暂时的,我希望将查询的结果映射到那里。但是,我不希望在持久化或从表中获取实体时映射此属性(因为它们在那里存在)
猜你喜欢
  • 1970-01-01
  • 2016-05-02
  • 2020-08-29
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
  • 2019-04-05
  • 1970-01-01
  • 2015-06-08
相关资源
最近更新 更多