• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA using Hibernate - Entity Manager doubts

 
Tiago Melo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi dear friends,

I'm learning about JPA and I've wrote a fairy simple application based on the Oreilly's EJB 3.0 Book. First, let me show you my classes and after that I'll tell what's happening.

Cabin entity




TravelAgentRemote




TravelAgentBean



persistence.xml



And now, a fairy simple standalone client to test everything:

Client



Ok, so let's go to the problem. The JPA's spec says that once an entity bean is managed by the EntityManager (after calling EntityManager.persist() or calling EntityManager.find()), every change on the entity is tracked and is persisted automatically on the data base.

So, here I'm persisting an instance of the Cabin Entity:



This works fine. I've checked at the database and a record was successfully created at the CABIN table.

So, according to the spec, since I've called EntityManager.persist passin in the cabin entity instance, this instance is managed, therefore if I do this:



the name should be automatically changed from "Master suite" to "Basic suite", and since this instance is managed, this change should be reflected at the database. But it's not what's happening! It don't have any effect, the name "Master Suite" is kept at the database. Why this is happening?

These are the jars I used to compile:

hibernate-entitymanager.jar (from Hibernate)
ejb3-persistence.jar (from Hibernate)
jboss-ejb3x.jar (from JBoss)
jbossall-client.jar (from JBoss)

The spec says that the <provider> element at persistence.xml is optional. If it's not specified, a default value is assumed. But in my case, what's the value that's being assumed?

Thank you all!
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I see it correctly that the entity that you are modifying is on the
client and the EntityManager on the server?
 
Tiago Melo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the entity is at the server as well, inside de jar file along with the other classes
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The interfaces that the client uses are remote, which may be a bit too
complex for a first example.
The entity is detached; one good reason for this is that the persistence context here is, according to the specification, "container-managed transaction scoped", and for the stateless session bean, a transaction is one method call. When the bean method returns, the transaction ends, and with it the persistence context.
 
Tiago Melo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right... so how could I turn the Persistence Context extended in this example?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your code is in the client, and therefore isn't being run in the context of an EntityManager. The EntityManager is only on the server, and therefore only in the context of the createCabin method.

Also why are you using PortableRemoteObject.narrow()? That isn't needed anymore in EJB 3

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic