2

I'm new to persistence and I want to do the following:

1) Load a part of database into entities and store them in a file

2) Load these entities from file, put them in managed context (so that all the @JoinColumn and @JoinTable, especially those with lazy fetch type, would work normally) and make sure that I don't accidentally update database with them.

The point of all that is that every time I start the application I don't want to call database for the table that almost never change - so I want to store them locally, preferably in a file.

While part 1) was pretty easy, I have some concerns as to part 2). I figured that loadObject and em.persist() would work fine as long as I make sure not to call em.flush().

But at one point I'd like to check if the object in database hasn't been changed. If I call persist() on my de-serialized entity, and at the same time database changed, after flushing which version would remain?

Is there a better way to do this?

Perception
  • 79,279
  • 19
  • 185
  • 195
r3mbol
  • 261
  • 6
  • 15
  • After some more research I found that apparently em.refresh() answers the question 'which version would remain' (refresh overwrites any changes I made locally with ones made on DB). Still, I can't get the lazy-fetch relations to work after de-serialization. – r3mbol Jul 25 '11 at 13:00

1 Answers1

0

Load a "managed" copy of this object from database. Then update copy the properties over from the object you read from file.

You'll have a "managed" entity not yet serialized into database.

Depending on your flow of logic you can abort it before you merge it with database instance.

John S
  • 13
  • 5