java 如果 Transformer 未显式声明,hibernate 如何与 HQL 查询一起工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13872920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How hibernate works with HQL query if the Transformer is not explicit declared
提问by Xiezi
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:
不知道哪里可以找到hibernate的实现机制。我有很多关于休眠的问题,但我们可以从这个开始:
If there is a HQL like this:
如果有这样的 HQL:
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.
将所有字段设置为 B 的默认转换器是什么?我发现这甚至不需要 setter 或 getter 来设置值。
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.
感谢您阅读本文,欢迎提出任何想法。
回答by Jitendra Vispute
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
当使用默认 Transformer 时,它期望类是休眠实体,这意味着它必须与某个表映射,在第二种情况下是
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 )
B 不是休眠实体(未映射到任何表,它的简单 POJO 没有任何休眠特定的注释)
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.
例如,有时我们有一个类,我们想根据查询返回的数据填充数据。该类是一个简单的 POJO 而不是 Hibernate 实体,因此 Hibernate 不会识别该类。这可以通过使用 Transformers 在 Hibernate 中完成。让我们看一个简单的例子,展示如何使用 Transformer。首先,让我们看一个名为“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;
}
}
Now, let's have a look at a simple method, that uses hibernate HQL and the Transformers class to fill “UserActivityStat” instance with data
现在,让我们看一个简单的方法,它使用休眠 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();
}
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.
请注意,两列中的每一列都有一个别名。此别名必须是“UserActivityStat”类上的属性名称。还要注意在“Transformers”类中使用“setResultTransformer”。