Should I return managed object on JPA create/edit
I've come a开发者_如何学Pythoncross a situation where I persist an Entity object in JPA and subsequently have to see if another object .equals()
it.
However, the test fails because the first object is just a POJO where as the second is its managed equivalent.
Is it good practise to return the newly managed entity object as a result of a JPA create or edit operation?
Regards, Laurence
You can override the hashCode()
and equals(..)
methods using the fields forming the business key of the object. See this article
You've got two types? A POJO, effectively a DTO and an annotated POJO which is managed? Can you either code an equals method or craft a comparison method that gives unsurprising behaviour?
This gets into all sorts of interesting questions about what "equality" means. My insticnt here is to provide a hasTheSameValueAs()
method.
To answer your question directly, it does seem very convenient on occasions to do return the managed object.
精彩评论