• 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

JPA using Hibernate - Entity Manager doubts

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right... so how could I turn the Persistence Context extended in this example?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic