JPA – Updating Collections

I’d previously blogged about my difficulting in updating collections as part of JPA. It turns out the solution which had worked in a MySQL environment didn’t work terribly well in a Oracle environment.

For clarity we’re using TopLink. The behaviour from the previous post was that the old records from the collection were kept, and the new named collections were also inserted. I would have been really interesting to see what the application truly thought the underlying database was telling it, but unfortunately I wasn’t in a position to check it.

I tried multiple solutions (the orphanRemoval=true annotation, altered the collection code, setting the backwards links to null to enforce the orphan status for the removed items (e.g. Owner = null), and countless others) all without success.

I’d also partially implemented calling a Update Query, but this seemed incredibly inefficient. It was only later whilst still reading around the subject that I came across an article saying that the Unique field in the PK class should be marked with @Id – note this Unique field only needed to be unique for the collection NOT the relational database.

Adding this annotation to the PK class, and then calling the Update Query resolved the issue, and was actually more efficient that doing a double loop around various collections – The unique Id in the earlier example would in the case be the Pet Name.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s