如果没有显式声明Transformer,hibernate如何与HQL查询协同工作 [英] How hibernate works with HQL query if the Transformer is not explicit declared

查看:70
本文介绍了如果没有显式声明Transformer,hibernate如何与HQL查询协同工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道在哪里可以找到hibernate的实现机制。我有很多关于hibernate的问题,但是我们可以从这个开始:

如果有这样的HQL:

 来自B b 
其中bx =:x
和by =:y

查询代码如下:

$ p $ Query query = session.createQuery (hql.toString());

将所有字段设置为B的默认转换器是什么?我发现这甚至不需要setter或getter来设置值。



或者说,它和这个之间有什么不同:

  Query query = session.createQuery(hql.toString())。setResultTransformer(Transformers.aliasToBean(B.class)); 

感谢您阅读本文,并欢迎您提供任何建议。



  Query query = session.createQuery(hql.toString())。setResultTransformer(Transformers.aliasToBean(B.class)); 



B不是hibernate实体(没有映射到任何表,它的简单POJO没有任何hibernate特定的注释)



例如
有时候我们有一个类,我们想根据查询返回的数据填充数据。这个类是一个简单的POJO,而不是一个Hibernate实体,所以Hibernate不会识别这个类。
这可以通过使用变形金刚在休眠中完成。让我们来看一个简单的例子,展示如何使用变形金刚。首先,让我们看看一个名为UserActivityStat的简单POJO类。
这个类包含一些统计信息。我们希望直接通过运行Hibernate HQL来填充实例的统计信息。


  public static class UserActivityStat {
private int totalPhotos;
private int totalViews;
public UserActivityStat(){}
public int getTotalPhotos(){
return totalPhotos;
}
public void setTotalPhotos(int totalPhotos){
this.totalPhotos = totalPhotos;
}
public int getTotalViews(){
return totalViews;
}
public void setTotalViews(int totalViews){
this.totalViews = totalViews;


$ / code $ / pre

现在,我们来看看一个简单的方法,使用hibernate HQL和Transformers类来填充UserActivityStat实例和数据

$ public $ UserActivityStat getUserActivityStat(User user){
return(UserActivityStat)hibernateSession.createQuery(
select count(*)as totalPhotos,sum(p.views)as totalViews+
from Photo p+
where p .user =:user+
p.dateCreated< =:now)
.setParameter(user,user)
.setTimestamp(now,new Date() )
.setResultTransformer(Transformers.aliasToBean(UserActivityStat.class))
.uniqueResult();
}

请注意,每列都有一个别名。此别名必须是UserActivityStat类中属性的名称。还要注意沿着变形金刚类使用setResultTransformer。


I don't know where I can find the implementation mechanism of hibernate. I have a lot of questions about hibernate but we can start them from this one:

If there is a HQL like this:

from B b
where b.x =: x
and b.y =: y

And query code like this:

Query query = session.createQuery(hql.toString());

What is the default transformer to set all of the fields into B? I found this even doesn't need setter or getter to set values.

Or say, what is the differece between it and this one:

Query query = session.createQuery(hql.toString()).setResultTransformer(Transformers.aliasToBean(B.class));

Thank you for reading this and any ideas are welcome.

解决方案

When default Transformer used it expects the class to be hibernate entity meaning that it must be mapped with some table and in second case that is

Query query=session.createQuery(hql.toString()).setResultTransformer(Transformers.aliasToBean(B.class));

B is not hibernate entity( not mapped to any table its simple POJO without any hibernate specific annotations )

e.g There are times we have a class, we would like to fill with data according the data returned from a query. The class is a simple POJO and not an Hibernate entity, so Hibernate won’t recognize this class. This can be done in Hibernate by using Transformers. Let’s have a look on a simple example, showing how Transformers can be used. First, let’s have a look at a simple POJO class named: "UserActivityStat". This class contains some statistical information. We would like to fill the statistical information of an instance, directly from running an Hibernate HQL.

public static class UserActivityStat{
    private int totalPhotos;
    private int totalViews;
    public UserActivityStat() {   }
    public int getTotalPhotos() {
          return totalPhotos;
    }
    public void setTotalPhotos(int totalPhotos) {
         this.totalPhotos = totalPhotos;
    }
    public int getTotalViews() {
      return totalViews;
    }
    public void setTotalViews(int totalViews) {
        this.totalViews = totalViews;
    }
 }

Now, let’s have a look at a simple method, that uses hibernate HQL and the Transformers class to fill "UserActivityStat" instance with data

public UserActivityStat getUserActivityStat(User user) {
     return (UserActivityStat) hibernateSession.createQuery(
             "select count(*) as totalPhotos, sum(p.views) as totalViews " +
             "from Photo p " + 
             "where p.user = :user " +
             "p.dateCreated  <= :now")
         .setParameter("user", user)
         .setTimestamp("now", new Date())
         .setResultTransformer(Transformers.aliasToBean(UserActivityStat.class))
         .uniqueResult();
}

Note, that each of the 2 columns has an alias. This alias must be the name of the property on the "UserActivityStat" class. Also note for the use of the "setResultTransformer" along the "Transformers" class.

这篇关于如果没有显式声明Transformer,hibernate如何与HQL查询协同工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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