开发者

create object in hibernate select

I ha开发者_开发知识库ve 2 classes:

class A {
B b;

public A() {}
public A(B b) { setB(b); }
...
}

class B {
int id;
public B(int id) { setId(id); }
}

in hql I want to select like this:

select new A( new B(a.b.id) ) from A a

but I got error

org.hibernate.hql.PARSER - line 1:48: unexpected token: ,

Is it possible to create object in parameter, or select just field and create it inside constructor?


Not sure whether I exactly understood what you want to achieve. But you can create a HQL query (with projection) to only query for the columns you're intereset in, like:

select a.whatever, b.id from A a join a.b b

Afterwards you provide an implementation for the interface ResultTransformer and set it to your query object with query.setResultTransformer(yourTransformer)

Your implementation of the result transformer is responsible for creating instances for A and B


maybe look into writing your own function to build your queries and use of the StrinBuilder class - use hql.append instead of writing out the query !!!

import org.hibernate.Hibernate; import org.hibernate.search.FullTextQuery; import org.hibernate.search.FullTextSession; import org.hibernate.search.Search;

not sure if it helps, otherwise go and check out cyclos web app - it includes lots of queries and according java files. Its open source and uses comprhensive hibernate

regards

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜