wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes EJB3/JPA: problem reloading object with one-to-many Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "EJB3/JPA: problem reloading object with one-to-many" Watch "EJB3/JPA: problem reloading object with one-to-many" New topic
Author

EJB3/JPA: problem reloading object with one-to-many

Bear Giles
Greenhorn

Joined: Mar 16, 2006
Posts: 25
I'm learning EJB3 and JPA and this might be an embarassingly stupid question. I'm following the conventions of injecting an entity manager factory into by stateless bean and creating an entity manager on initialization and closing it upon destruction.

This means that my one-to-many relationship crosses concurrent entity managers since it doesn't make sense to put both User (one) and Auction (many) operations into the same EJB bean.

The problem is that I can save a one-to-many relationship in the Auction bean but I don't see it via user.find() unless I immediately follow it with em.refresh(user).

Is there a way around this, e.g., having a shared entity manager cache? Calling refresh() hits the database so I would prefer to avoid it if possible.


SCJP 5 & 1.4, SCWCD 1.4, SCBCD 1.3; Security+
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
If you are using a stateless SessionBean you should inject an EntityManager, not an EntityManagerFactory. The container will then ensure the correct EntityManager is used for the transaction.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Bear Giles
Greenhorn

Joined: Mar 16, 2006
Posts: 25
When I tried that earlier it looked like it required JTA. That's a little heavy when you're just running stuff as MyEclipse unit tests or a very simple webapp under tomcat. Did I miss something?

(I also have jboss installed but this is an older system so I try to avoid the memory load.)
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
I'm not sure how you are using EJB SessionBeans without JTA, as they are dependent on JTA. Also not sure how you are using SessionBeans on Tomcat, as it is not a JEE container.

For webapps not using SessionBeans, you don't need to use JTA, you normally create an EntityManager per server request. If you have a stateful architecture you may have an EntityManager per http session. I don't think having cached EntityManager per DAO or per service would be a good idea.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB3/JPA: problem reloading object with one-to-many
 
Similar Threads
How to map to multiple entities from a single field
using JPA and JDBC together
Isolation of EARs (and EJB3)
Cade component diagram and DAO
JEE5 still pushes for anemic domain model ?