• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object oriented Data access vs. QL in JPA

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Please help!

I am new to ORM, I am designing access to existing data.

Example Problem: I want to get a particular customer by Id(007). I have two ways of doing it.

Assumption: I have a @Entity class Customer mapped to Database table.

1. (OO Way)
Customer cust = entityManager.find(Customer.class, Id);
String fName = cust.first_name
String lName = cust.last_name

2. (QL Way)
String querySt = "SELECT c FROM Customer c WHERE c.Id = '007'";
Query query = entityManager.createQuery(querySt);
Collection <Customers> cust = query.getResutList();
String fName = cust.first_name
String lName = cust.last_name

Q1. Am I using the correct steps in using both of them,
Q2. If I prefer to use the first one are there any disadvantages to it.

Many Thanks
Alain
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alain,

if I understood my EJB3 documentation the first approach has an advantage if you use an Entity Cache.

As far as I got this find() first looks in the entity cache and only accesses the DB if it can't find the entity there, where the query always accesses the DB, at least to fetch the PK.

Since DB access is very expensive (in terms of performance) the first solution will be better.


As I said before, this is what I understood...


Any other opinions?


John
 
reply
    Bookmark Topic Watch Topic
  • New Topic