We are in the beginning coding phase for project that we are using JPA with. We used the tools in Netbeans to generate our JPA entities based on our schema. It worked pretty well, but as always we have had to customize the entities a good bit since then.
We made a decent size change to our schema (added a table, and swapped around relationships between others) and as such needed to update our entities. We could not regenerate them, as we would have to go and reapply all the customizations we had, so we did it all by hand. Not a big deal, just more time consuming then I expected.
The customizations were Named Queries we added, Cascade Types, our own to String methods, equals and hash code methods. I thought about creating classes that extended the entities to add in the toString, equals, and hascode methods, that way if we regenerated them, they would not be lost. But I was not sure about the Cascade Types and named queries.
Is there a better way, or is this just wishf开发者_如何转开发ul thinking?
I believe this is just wishful thinking. Generating your entities and extending them just gives you a different set of problems. You generated entities will have to be annotated as @MappedSuperclass and won't have table names - plus your extensions may have to repeat some of the annotations, plus you'll have to tweak you named query customizations anyway sometimes....its all just a hassle.
In general, the generation of entities from a database schema is a one-time thing, to be forever maintained by hand.
Generation of a database schema from entities is however, possible to do over longer periods of time. However, even then, you'll have to manage the issues around schema migration carefully.
Bottom line -- synchronising persistent entities and database schemas takes careful, manual work.
精彩评论