Hibernate – JPA

SessionFactory (jpa: EntityManagerFactory) – thread-safe (and immutable) representation of the mapping of the application domain model to a database
Session (jpa: EntityManager) – A single-threaded, short-lived object conceptually modeling a “Unit of Work”
StatelessSession – like JDBC
Transaction (jpa: EntityTransaction) – A single-threaded, short-lived object used by the application to demarcate individual physical transaction boundaries.
@PersistenceUnit injects an EntityManagerFactory
@PersistenceContext injects an EntityManager

Hibernate – hibernate.cfg.xml

JPA – persistence.xml

save or persist

save() – for transient save to db, return object(id), for detached create new one row
persist() – for transient save to db and return void, for detached object – PersistentObjectException. For transient doesn’t guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time
saveOrUpdate() – (only hibernate) doesn’t throw Exception on transient object.

merge or update

merge() – for transient and detached the same behavior: update a persistent entity instance with new field values from a detached entity instance, return new attached object.
update() – original hibernate method, return type is void, act on passed object (re-attach). Throws exception on transient entity.

Flush and Refresh

session.refresh() – reload object from database

Flush –

  1. before some query executions
  2. from org.hibernate.Transaction.commit()
  3. from Session.flush()

Flush ordering –

  1. all entity insertions in the same order the corresponding objects were saved using Session.save()
  2. all entity updates
  3. all collection deletions
  4. all collection element deletions, updates and insertions
  5. all collection insertions
  6. all entity deletions in the same order the corresponding objects were deleted using Session.delete()

Flush mode –

  1. only flush at commit time when the Hibernate Transaction API is used
  2. flush automatically using the explained routine,
  3. never flush unless flush() is called explicitly.

get or load

get(class, id) (jpa: find )- hit the database, return object or null if it not exists.
load(class, id) (jpa: getReference) – return proxy, hit the database when object requested, throws exception (jpa:EntityNotFoundException) when object not exists.

sort or ordered

sort – sorting in jvm with Comparator or native
order – use database order by

Inheritance mapping

MappedSuperclass – the parent classes, can’t be entities – super class doesn’t have own table. Superclass no longer has an @Entity annotation. @AttributeOverride – for renaming database column that different from super class column name.

Table-Per-Class – all the properties of a class, are in its table, so no join is required. The same as previous, super class also has a table.

Single Table – the entities from different classes with a common ancestor are placed in a single table. @DiscriminatorValue (@DiscriminatorFormula) –  for this specific entity class so that your persistence provider can map each database record to a concrete entity class.

Joined Table – each class has its table and querying a subclass entity requires joining the tables. @PrimaryKeyJoinColumn – the primary key of the Subclass entity also has a foreign key constraint to the primary key of its parent entity.


Native query

Named queries

FetchType: N+1 SELECT’s Problem

FetchType.SELECT – N+1 problem is where one query is executed to fetch N records and N queries to fetch some relational records
can be resolved by using fetch join or using a Criteria
but fetch join doesn’t work with pagination

Lazy Loading

@Basic, @OneToMany, @ManyToOne, @OneToOne, and @ManyToMany annotations have an optional parameter called fetch. If this parameter is set to FetchType.LAZY
Build-time bytecode instrumentation (jpa)
Run-time bytecode instrumentation (jpa)
Run-time proxies (hibernate)

Bytecode enhancement

Custom user types

public class MoneyType implements BasicType
public class MoneyType implements UserType
public class MoneyType implements CompositeUserType

Collection types (hibernate)

Debug mode



@Transient – not store to database
@Temporal – time, date or timestamp in datadase
@Table – name of mapped table
@Version – for optimistic locking
@ElementCollection (hibernate: @CollectionOfElements) like OneToMany but belongs to entity – Embeddable, cannot select, merge, update without main entity

JPA 2.1

Named Stored Procedures

Leave a comment

Your email address will not be published. Required fields are marked *