• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA question

 
srav kandy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I create a new entity in Namedquery like this -

@NamedQuery(name="productfordisplay",query=" SELECT NEW com.ssg.article.Product(p.name, p.id) FROM Product p where p.id=:id")

Is the above query valid. I want to return Product entity with only 2 attributes rather than all the attributes for display purpose (keep it lightweight).


Please clarify. I am in middle of design.

Thanks!
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say 'no.'

When you bring back an object, you bring back all the properties for the object.

You can do just straight SQL, and in that case you just get back the database values in a multi-dimensional array.

Still, I might think this is far too low level for the SCEA exam. Am I wrong?

-Cameron McKenzie
 
Claudiu Dumitrescu
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reading from JPA specification:
A constructor may be used in the SELECT list to return one or more Java instances. The specified class
is not required to be an entity or to be mapped to the database. The constructor name must be fully qualified.


So I think it's OK.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic