Data Access Object - actually a wrong name.

  6 posts   Feedicon  
Replies: 5 - Last Post: June 25, 2009 19:27
by: abien
showing 1 - 6 of 6
 
Posted: June 24, 2009 11:48 by abien
DAO (EJB 3.1) project checked in. But Data Access Object is actually a wrong name for this pattern. The checked in project was tested with Netbeans 6.7rc3 and Glassfish v3 prelude. The code, however, was already used with Glassfish v2.1 in real world projects.
 
Posted: June 25, 2009 06:35 by Zdenek Tronicek
I will add some comments to support discussion (I am sure that you, Adam, are aware of it, so it is mainly for others).

The create method calls persist, flush, and refresh. As somebody asked on Adam's blog: why?

public T create(T t) {
this.em.persist(t);
this.em.flush();
this.em.refresh(t);
return t;
}

It is common that entity Id is generated. It can be generated either by JPA implementation or by database. If it is generated by the JPA implementation, the entity will have Id once the persist call returns. However, if it generated by database, the entity will not have Id until it is written to the database (which happens at the end of transaction). So, if you expect or need Id, you have to ask for writing (flush), then fetch the entity (refresh) and check Id. If you do not need Id before the end of transaction, you can omit the flush and refresh calls.

public T create(T t) {
this.em.persist(t);
return t;
}

 
Posted: June 25, 2009 09:08 by Simon Martinelli
You don't need the refresh call. The ID will be available after flush or commit.
 
Posted: June 25, 2009 18:11 by abien
Yes and no. The entity is managed / attached after the persist call, so refresh is actually not needed. BUT: the key could be computed directly in the database - outside the provider (is typical for the DB 2 databases). In that particular case even refresh wouldn't be enough, because the key could be computed between the refresh and the commit as well...

In the practice it works surprisingly well, without flushing or refreshing...

thanks for your feedback!
 
Posted: June 25, 2009 19:20 by Simon Martinelli
I use JPA with Oracle, MySQL and HSQLDB and had never to do a refresh to get the ID and I usually need it because I'm developing Client/Server applications. Flush or commit is enough because then the SQL statments will be fired.

With Oracle the JPA Provider does a SELECT <SEQUENCE>.NEXTVAL FROM DUAL. In that case the provider can simply assign the value of the sequence to the entities ID.

With MySQL the provider normaly gets the ID with SELECT LAST_INSERT_ID()
 
Posted: June 25, 2009 19:27 by abien
I had an issue with DB 2 and openJPA and IDENTITY strategy. However, you could easily skipped the refresh and flush if it works for you well. The problem is more likely in remote client-server setups...
Thanks!
showing 1 - 6 of 6
Replies: 5 - Last Post: June 25, 2009 19:27
by: abien
  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120518.3c65429)
 
 
Close
loading
Please Confirm
Close