• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Searching row in database by a column which is not a primary key

 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have an entity:




How can I find objectId by a surname given by user? In EntityManager I can use find(...) only with a primary key..
The surname is unique.
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to use a query. For example:

select p from Person p where surname = :surname
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ralph, but unfortunately it doesn't work, and besides I'd like to do it with JPA 2 CriteriaBuilder and CriteriaQuery... and I still cannot figure out how to do that correctly.
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String jpql = "select p from Person p where surname = '" + givenSurname + "'";


it works - I forgot about ' marks...

Still I'm looking for JPA 2.0 sample witha CriteriaBuilder ansd so on which will do the same thing.
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find some examples for using the criteria API in the following blog. Hope this helps.
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've also found this blog. The problem is that the class QueryBuilder seem to not exist, at least you can't find in this Java EE 6 API:

http://technology-related.com/javaee/6/docs/api/

and I can't find it in the implementation I downloaded (although rest of these new classes exist, i.e. CriteriaBuilder).




 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that blog entry is obsolete, the API changed after it was written. Here's a good introduction: http://www.ibm.com/developerworks/java/library/j-typesafejpa/
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code below finds and returns a whole Person object with given surname.



Almost there...

If you know how to return only objectId of this Person please share your knowledge
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Yes, that blog entry is obsolete, the API changed after it was written. Here's a good introduction: http://www.ibm.com/developerworks/java/library/j-typesafejpa/


But still there's a QueryBuilder
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ismael Upright wrote:The code below finds and returns a whole Person object with given surname.



Almost there...

If you know how to return only objectId of this Person please share your knowledge


Will this not do it:

 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will, but I was rather thinking about not sending the whole Person from the database, but only it's Id.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ismael Upright wrote:It will, but I was rather thinking about not sending the whole Person from the database, but only it's Id.


OK. What problem are you trying to solve by doing that?
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem of optimization of traffic between application and database. I want only needed data to be sent by database.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given how ORMs work, your problem is probably not worth worrying about. If you need this level of optimization (i.e. the network between database and application is likely to be very poor) don't use an ORM. Have you profiled your applciation and checked this is a real problem, or are you just worrying about this as an early optimisation?
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock wrote:Given how ORMs work, your problem is probably not worth worrying about. If you need this level of optimization (i.e. the network between database and application is likely to be very poor) don't use an ORM. Have you profiled your applciation and checked this is a real problem, or are you just worrying about this as an early optimisation?


Just worrying and wondering if there's a dedicated solution to get only 1 field from database instead of a whole object
Thank you for pointing out the ORM concept Probably it's indeed not worth worrying about.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic