• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object oriented Data access vs. QL in JPA

 
Alain Dickson
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • 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
 
John Bengler
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic