JPA 1.0 Complaints
By Anthony Mattas
Recently at work, I began refactoring a project I built a while ago to reduce code. When I first worked on it, I was new to the JEE5 stack and didn’t know about lazy or eager loading of relationships. As a result, I ended up writing a lot of extra code that basically acted like eager loading.
While refactoring, I’ve come across a few things about JPA that bother me:
- If you remove child objects from a parent in a relationship and then update or merge, the detached entity doesn’t actually delete the removed objects from the database. Instead, they get merged back into the detached instance. I know this is technically how merge is supposed to work, but it’s often pretty inconvenient.
- Managing many-to-many relationships is a real headache. It’s actually easier to treat them as two one-to-many relationships. You might think using two relationships would be more complicated, but with how JPA works, removing a child doesn’t just remove the link. It tries to delete the other object the relationship points to.
- I often call
EntityManager.refresh()andEntityManager.flush()in my facade classes to keep data consistent. Really, the JPA provider should handle this for me.
Fortunately, JPA 2.0 is supposed to add a @RemoveOrphans annotation, which should fix the first issue. I haven’t heard anything about the other problems yet. What have your experiences been with JPA’s quirks?
Category: Development
Comments
Sign in to join the conversation
No comments yet. Be the first to share your thoughts!