1

I use JPA2 and Hibernate 3.6 as provider.

I have some code like this:

final List<Object[]> result = entityManager.createQuery("SELECT qt, tt FROM ...").getResultList();

I have a converter who transform the list of Object[] to list of SimpleClass, which contains my qt and tt entity.

Is is possible to do something like this in JPA 2 ? (In C# with linq, it's possible)

final List<SimpleClass> result = entityManager.createQuery("SELECT new SimpleClass(qt, tt) FROM ...").getResultList();

Thanks.

La Chamelle
  • 2,927
  • 4
  • 36
  • 54
  • 1
    You can look at this related answer http://stackoverflow.com/questions/2355728/jpql-create-new-object-in-select-statement-avoid-or-embrace – landal79 Mar 14 '12 at 15:34
  • Hmm, interesting question. I first thought using @MappedSuperclass as a wrapper, but it looks like you cannot query against it. I think good old composition will work as a backdoor. – d1e Mar 14 '12 at 20:49
  • @landal79 this is exactly what i search, i will try and make a feedback. Thanks you. – La Chamelle Mar 19 '12 at 13:31
  • Ok this is the good answer but sadly i use some fetch in my query and jpa complains : `query specified join fetching, but the owner of the fetched association was not present in the select list` – La Chamelle Mar 19 '12 at 13:47

1 Answers1

0

If you have mapped your entities, you should be able to do:

List<SimpleClass> result = entityManager
    .createQuery("select s from SimpleClass s", SimpleClass.class)
    .getResultList();
beerbajay
  • 19,652
  • 6
  • 58
  • 75
  • SimpleClass is not an Entity, its just a wrapper who contains the two other entities. – La Chamelle Mar 14 '12 at 13:29
  • `.__.` Perhaps you should be running two `JPQL` queries and combining the results in `SimpleClass` instances. You'd get the OR mapping for free in that case. – beerbajay Mar 14 '12 at 14:07
  • This is a related question/answer : http://stackoverflow.com/questions/2355728/jpql-create-new-object-in-select-statement-avoid-or-embrace – La Chamelle Mar 19 '12 at 13:51