Should I modify JPA generated beans or is that bad practise?

148 views Asked by At

I did alot of work with XML schema and JAXB to generate java beans. I never touched the generated beans, only extended them in other beans, because otherwise I might loose code if beans are regenerated. I actually generate my beans newly automatically before any build is allowed to happen. This gurantees me to only always have one truth.

Is the same idea valid for JPA bean generation, or is JPA bean generating meant to be a one time thing?

Should you touch the generated JPA beans or are they untouchable? I can't find any style or best practise recommendation...

2

There are 2 answers

0
JB Nizet On BEST ANSWER

It's actually a best practice to put code (other than getters and setters) in JPA entities. Not doing so results in an anemic domain model.

Generating JPA entities from a schema can be useful to start you up, but once this is done, you should fine-tune the code and the design of these entities and add business logic in them. It's not hard at all to write entities from scratch, without any generation phase.

0
OwnYourCode On

Thick domain model eventually produces an single domain, and limited system - slow too. Keep them lean, and some optimization can be done. Make them thick, you'll eventually loose your control. Old and proven DTO pattern with good in-between API is still the Right way to go - you may even want to scrap ORM. Entity2Gui "pattern" fixes everything to both directions. Harmful and extremely expensive.