4

I have 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?

3
  • What exactly do you want? Why do you need to use new here, can't you just do SELECT a ...? Commented Dec 2, 2010 at 12:56
  • in this class is more fields and I don't want to select them. Commented Dec 2, 2010 at 13:01
  • witch "select new A(a.b.id) from A a" hibernate will generate select just for that field and not for other eager fields. What I want is to get object instead of int value. But in B also is some eager fields, that I don't need, therefore I don't want to get full B object. Commented Dec 2, 2010 at 13:03

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

this query just return list of whatever and id. I need object A list, but if A is big and have lot relation to other objects, selecting this A can take some times. Some times I just need list of A with only few fields from only few related objects. I can do that with "select new A(a.whatever, a.b) from A", but than it will select full B object, and I need only id from it, therefore best query would by "select new A(a.whatever, new B(a.b.id) from A" but hibernate not allow that. therefor I need create other classes with only field I need.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.