Java Hibernate 填充瞬态属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21148241/
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
Java Hibernate populate transient property
提问by C-Rad
I have several entities that have transient properties that I would like to populate from the sql query. I have tried several things but have not found the correct combination yet.
我有几个具有瞬态属性的实体,我想从 sql 查询中填充这些属性。我已经尝试了几件事,但还没有找到正确的组合。
here is an example entity
这是一个示例实体
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
long id;
@Size(max=1000)
String image_url;
@Transient
boolean liked;
@Transient
long numLikes;
**getters and setters** for persistent properties
public boolean isLiked() {
return liked;
}
public void setLiked(boolean liked) {
this.liked = liked;
}
public long getNumLikes() {
return numLikes;
}
public void setNumLikes(long numLikes) {
this.numLikes = numLikes;
}
I have tried using the @Basic anotation and setting it's fetch method to eager
我曾尝试使用 @Basic 注释并将其 fetch 方法设置为eager
@Basic(fetch = FetchType.EAGER)
on the getters but that didn't seem to do anything. I've seen it set to lazy as well.
在吸气剂上,但这似乎没有任何作用。我也看到它设置为懒惰。
Am I missing anything in here that would cause a column name not be mapped to one of these transient properties?
我是否在这里遗漏了任何会导致列名未映射到这些瞬态属性之一的内容?
here is my impl code.
这是我的 impl 代码。
SQLQuery query = getCurrentSession().createSQLQuery("" +
"SELECT * ,count(likes1.liked) as numLikes " +
"FROM aboutme " +
"left JOIN " +
"(" +
" select liked, likedObject_id " +
" from likes " +
" where liked = 1" +
") as likes1 ON aboutme.id = likes1.likedObject_id " +
"left join " +
"(" +
" select likedObject_id, liked " +
" from likes " +
" where liked = 1 and profile_id = :id" +
") as likes2 on likes2.likedObject_id = aboutme.id " +
"WHERE aboutme.profile_id = :id " +
"group by aboutme.id");
query.addEntity(AboutMe.class);
query.setParameter("id",id);
return query.list();
I've tried using the
我试过使用
SQLQuery query = ..."Select count(value that returns properly) as {object.numLikes}..."
query.addEntity("object",Object.class),
I get an error here that says it can't find the column name for property [property] for alias [alias]
我在这里收到一个错误,说它找不到别名 [alias] 的属性 [property] 的列名
What am I doing wrong?
我究竟做错了什么?
Transient properties are probably not the way to go for what I am trying to do. what I need to figure out is how to map these derived columns to an object that I can return to the front end. how do I set up an object that hibernate can map these aliased columns to properties?
瞬态属性可能不是我想要做的事情的方法。我需要弄清楚的是如何将这些派生列映射到我可以返回到前端的对象。如何设置休眠可以将这些别名列映射到属性的对象?
回答by shippi
Hibernate will never populate your transient fields and what is most important transient fields are not even part of the java serialization process. What I would try if you really need this - is the ResultTransformer.alliasToBean
which is "injecting" the values into the class with reflection.
Hibernate 永远不会填充您的瞬态字段,最重要的瞬态字段甚至不是 java 序列化过程的一部分。如果你真的需要这个,我会尝试 - 是ResultTransformer.alliasToBean
通过反射将值“注入”到类中。
setResultTransformer(Transformers.aliasToBean(Yourclass.class));
Just a quick idea, not sure whether it will work.
只是一个快速的想法,不确定它是否有效。
回答by C-Rad
I figured it out. You can set the transient property though it's kind of a pain.
我想到了。您可以设置瞬态属性,尽管这有点痛苦。
using the addScalar Method on the SQLQuery object you can set the transient properties. However if it is done this way you have to set ALL the properties in the object this way (Hibernate doesn't autofill any properties)
在 SQLQuery 对象上使用 addScalar 方法可以设置瞬态属性。但是,如果以这种方式完成,则必须以这种方式设置对象中的所有属性(Hibernate 不会自动填充任何属性)
Also you will have to use the setResultTransformer Method also on the SQLQuery object. So to finish off my previous example I would have to add the following code in my impl.
此外,您还必须在 SQLQuery 对象上使用 setResultTransformer 方法。所以为了完成我之前的例子,我必须在我的 impl.js 中添加以下代码。
// query.addEntity(AboutMe.class); <- this contradicts the setResultTransformer
query.setParameter("id",id);
query.addScalar("id",StandardBasicTypes.LONG);
query.addScalar("image_url",StandardBasicTypes.LONG);
query.addScalar("liked",StandardBasicTypes.LONG);
query.addScalar("numLikes",StandardBasicTypes.LONG);
query.setResultTransformer(Transformers.aliasToBean(Object.class));
where Object is the class that I am wanting to use that has all these properties declared.
其中 Object 是我要使用的类,它声明了所有这些属性。