Please note: although I'm talking about Java in this question, what I'm asking about here should really be language-agnostic.
I am dabbling in OR/M for the first time, and have cooked up the following "update strategy" for all of my entities:
- All entity POJOs extend an abstract base class
BaseEntitythat has a
Long idproperty, for that entity's unique id/primary key
- Applications are free to create instances of an entity, but all constructors/factory methods omit the setting of this
- Hence if
Person extends BaseEntity, then I can write
Person p = new Person(...)but
- Only when you call the entity's DAO method does the entity get saved to persistence and is given an
- My DAOs will only have a
save*creational method (such as
savePerson(Person)), and they will do a check for the
idbeing NULL or not to determine whether we should attempt to
INSERTa new entity record, or
UPDATEan existing entity record.
- My DAO
save*methods will always return an instance of the entity that was just created/updated, but will make sure that the
idis now properly set (if it wasn't before).
To me this is logical/sensible, but seems a bit complicated/convoluted. Am I going about this the wrong way, are there better "update patterns" for OR/M that take care of this for me? I plan on doing everything in raw JDBC at first, and then eventually migrating to Hibernate once I'm more comfortable.