This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Entity Objec previous state

 
Bruno Frascino
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

Let's suppose an entity class A
I retrieve A from database and populate my update screen.
I change some fields from A
Then I want to update it, but I need to do some validation based on the original values. So when I pass A to my update method, I get A id, and get A again from Database by ID. So now I have 2 entities, will they be different? I mean, will they have different values?(because had done some changes)

Thanks
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you didn't commit it to the database then the database will have the old data. If your object is not in the persistent context. Meaning the new object isn't being monitored by ORM (you haven't merged the object in the session yet, or called update)

So you should be able to load the new data, compare. If you like new stuff, merge it into the persistence context and then commit.
 
Bruno Frascino
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,

I retrieved this entity A from database so it means it is ATTACHED, in other words, it is being managed by persistence context.
So I thought detaching it, using:

em.clear() - Clear the persistence context, causing all managed entities to become detached.

But this entity manager method detaches ALL entities... I am just wondering, could I detach only one?

Thanks again!
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bruno Frascino:
Hi Mark,

I retrieved this entity A from database so it means it is ATTACHED, in other words, it is being managed by persistence context.
So I thought detaching it, using:

em.clear() - Clear the persistence context, causing all managed entities to become detached.

But this entity manager method detaches ALL entities... I am just wondering, could I detach only one?

Thanks again!


Is it truly attached. I thought you showed the Entity into a screen, when you do this and allow a user to change the data, do you still have the Session open? I would also assume you have an n-tier architecture where your UI and Hibernate code is not on the same machine, so when you send the Object down to the client, what the client has and what it sends back is no longer attached, it is a detached object.

I am just trying to get the whole scenario in perspective.

Yeah, unfortunately JPA doesn't have an evict method like Hibernate's Session has.

Mark
 
Bruno Frascino
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,

I don't know how to explain the architecture, but I believe it is truly attached because any changes in the object, even not calling em.merge();, JPA still update the object.
I tried using the em.clear() method before updating, and now I am getting an error: Transaction is not active
This update method I am implementing is big, when I just update without validating heaps of condition I got no error!

So now I trying to get rid of some useless code... this is a refactoring project..

Anyway, thanks again!
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
" This update method I am implementing is big"

Yeah the general consensus is to keep a session/entityManager open for as short of a period as possible.

So good luck, I think as you refactor and make the code clearer, it will be much easier to see what is really happening.

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