Spring Data JPA vs JDBC - keshavbaweja-git/guides GitHub Wiki

  • JPA - Java Persistence API (Java specification for persistence)
  • EclipseLink - reference implementation
  • Hibernate - most popular implementation

JPA keeps reference to an entity, updates to an entity within a unit of work/tx are automatically persisted to DB. This has a couple of side effects. JPA has to make sure that for each entity, it loads one single instance within a tx. Load->modify->compare to original state, this can't be done, as JPA has latest state in memory. JPA considers domain model as a graph. Annotations for eager/lazy loading of referenced domain objects. Lazy loading triggers queries an unknown times. JPA has a proxy for a domain object, dirty checking determines if an entity needs to be persisted.

Spring Data JDBC does things fundamentally different. Delete or not referenced domain objects DDD concepts - Repository, Aggregate

When deleting referenced entities, use case is to delete Aggregate When you don't want to delete referenced entity, referenced entity is a different Aggregate Aggregates should be consistent at all times, Cascade deletes by default Other Aggregates referenced by ids, are not deleted. Other Aggregates have to loaded explicitly, lazily by developer code. Aggregate/Repositories could be in different data stores. Integration testing - Aggregate does not depend on other Aggregates, Referenced Aggregates are defined in terms of ids. Deferred constraint checks - allows you to create rows with foreign key checks deferred

An Aggregate is transactionally consistent, locking is at Aggregate level.

Spring Data JDBC does not support Composite Keys.