具有瞬态属性的Hibernate SQLQuery [英] Hibernate SQLQuery with transient properties

查看:128
本文介绍了具有瞬态属性的Hibernate SQLQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用db2创建一个hibernate SQLQuery,并且这个查询返回一些计算出的字段,并且与数据库中的任何列没有关系。



目标将SQLQuery中这些sum()计算的值设置为已存在的Java对象中的三个新瞬态字段。



SQLQuery使用以下语法:

  SELECT id as {entityObject.id},
name as {entityObject.name},
order as {entityObject。订单},
SUM(CASE
当pv.value_id = 1
AND pv.value = 1 THEN 1 ELSE 0 END)AS {entityObject.someCount}

问题在于Hibernate抱怨说需要一个someCount列。它看起来并不是帮助将java字段声明为 transient ,甚至不使用javax.persistence的 @Transient 注释

如果我只在hbm.xml映射文件中声明:

 

< property name =idtype =java.lang.Integercolumn =someColumn/>
<! - 这里有更多字段 - >

<! - 重要之一 - >
< property name =someCounttype =java.lang.Integer/>

Java对象:

  public class EntityObject implements Serializable {

private static final long serialVersionUID = 1479579608940145961L;

私人整数ID;
私人字符串名称;
私人整数订单;

//这是给我地狱的那个。我用@Transient也尝试过
私有瞬态整数someCount;
$ b $ public Category(){
}

public Category(final String name,final Integer order){
this.name = name;
this.order = order;
}

public Integer getOrder(){
return this.order;
}

public void setOrder(final Integer order){
this.order = order;
}

public Integer getId(){
return this.id;
}

public String getName(){
return this.name;
}

public void setName(final String name){
this.name = name;
}


public Integer getSomeCount(){
return someCount;
}

public void setSomeCount(final Integer count){
this.someCount = count;
}


}

它问我对于一列,并且我尝试插入一个假列,它不起作用。问题是我希望这些'count'字段只能从SQLQuery中设置,并且当来自常规Hibernate查询时为空和空值。



我看过在文档和谷歌搜索,似乎你可以声明一个字段瞬变,只是没有在hibernate映射文件中声明它,但它并没有将它设置在as {entityObject.someCount}的对象上,即使当我请声明getters / setters。



请帮忙。
非常感谢。

解决方案

<1>创建一个POJO:

  public class SumValue {
private BigInteger myId;
私人字符串myName;
私人BigInteger myOrder;
私人BigInteger mySum;
....
getters and setters here
....
}

2查询中的细微变化

  SELECT id为myId,
名称作为myName,
命令为myOrder,
SUM(CASE
当pv.value_id = 1
AND pv.value = 1 THEN 1 ELSE 0 END) mySum



<3>执行本地sql

  List< SumValue> jobStateViewList =(List< SumValue>)getSessionFactory()。getCurrentSession()
.createSQLQuery(yourQuery)
.setResultTransformer(
New AliasToBeanResultTransformer(SumValue.class)
).list );


I need to make an hibernate SQLQuery with db2 and this query is returning me some fields which are calculated and have no relation with any columns in database.

The goal is setting the values of these sum() calculations from SQLQuery on three new transient fields in a Java Object which already existed.

The SQLQuery uses the syntax:

SELECT id as {entityObject.id},
           name as {entityObject.name},
           order as {entityObject.order},
           SUM(CASE
           WHEN pv.value_id = 1 
            AND pv.value=1 THEN 1 ELSE 0 END) AS {entityObject.someCount}

The problem is that Hibernate complains and says to need a column for someCount. It seems not to help declaring the java field as transient or even using the @Transient annotation from javax.persistence at the same time.

If I only declare in the hbm.xml mapping file:

<property name="id" type="java.lang.Integer" column="someColumn" />
<!-- Some more fields here -->

<!-- THE IMPORTANT ONE -->
<property name="someCount" type="java.lang.Integer"/>

Java Object:

public class EntityObject implements Serializable {

private static final long serialVersionUID = 1479579608940145961L;

private Integer id;
private String name;
private Integer order;

    // This is the one giving me hell. I've tried with @Transient also
private transient Integer someCount;

public Category() {
}

public Category(final String name, final Integer order) {
    this.name = name;
    this.order = order;
}

public Integer getOrder() {
    return this.order;
}

public void setOrder(final Integer order) {
    this.order = order;
}

public Integer getId() {
    return this.id;
}

public String getName() {
    return this.name;
}

public void setName(final String name) {
    this.name = name;
}


public Integer getSomeCount() {
    return someCount;
}

public void setSomeCount(final Integer count) {
    this.someCount = count;
}


}

It asks me for a column, and I have tried inserting a fake column and it does not work. The thing is that I want these 'count' fields only to be set from the SQLQuery and to be empty and null when coming from a regular Hibernate Query.

I have looked at the docs and googled, and it seems that you can declare a field transient by only not declaring it at the hibernate mapping file, but then it does not set it on the object with the "as {entityObject.someCount}" even when I have getters/setters declared.

Help please. Thanks very much in advance.

解决方案

1 Create a POJO:

public class SumValue{
   private BigInteger myId; 
   private String myName;
   private BigInteger myOrder;
   private BigInteger mySum;
   ....
   getters and setters here
   ....
}

2 Minor changes in your query

SELECT id as "myId",
       name as "myName",
       order as "myOrder",
       SUM(CASE
       WHEN pv.value_id = 1 
        AND pv.value=1 THEN 1 ELSE 0 END) AS "mySum"

3 Execute native sql

     List<SumValue> jobStateViewList = (List<SumValue>)getSessionFactory().getCurrentSession()
                                   .createSQLQuery(yourQuery)
                                   .setResultTransformer(
                                        new AliasToBeanResultTransformer(SumValue.class)
                                   ).list();

这篇关于具有瞬态属性的Hibernate SQLQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆