Skip to content

Instantly share code, notes, and snippets.

@stillerr
Created January 21, 2016 12:28
Show Gist options
  • Save stillerr/813e4b9c4d447cc4ea01 to your computer and use it in GitHub Desktop.
Save stillerr/813e4b9c4d447cc4ea01 to your computer and use it in GitHub Desktop.
Hibernate / JPA + Spring notes

Hibernate / JPA + Spring notes

Relationships

###OneToOne (bidirectional)

Employee.java

@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ADDRESS_ID") //indicates that this entity is the owner of the relationship
private Address address;

Address.java

@OneToOne(fetch=FetchType.LAZY, mappedBy="address") //indicates that the entity in this side is the inverse of the relationship
private Employee owner;

###OneToMany / ManyToOne (bidirectional)

Group.java

@OneToMany(mappedBy = "group")
private Set<GroupDetail> details = new HashSet<>();

GroupDetail.java

@ManyToOne
@JoinColumn(name = "GROUP_ID", nullable = false)
private Group group;

###ManyToMany

@ManyToMany
@JoinTable(
	name = "USER_ROLE",
	joinColumns = { 
		@JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID") 
	},
	inverseJoinColumns = {
		@JoinColumn(name = "ROLE_ID", referencedColumnName = "ROLE_ID") 
	}
)
private Set<Role> roles = new HashSet<>();

CascadeTypes

Name Note
CascadeType.PERSIST means that save() or persist() operations cascade to related entities.
CascadeType.MERGE means that related entities are merged into managed state when the owning entity is merged.
CascadeType.REFRESH does the same thing for the refresh() operation.
CascadeType.REMOVE removes all related entities association with this setting when the owning entity is deleted.
CascadeType.DETACH detaches all related entities if a “manual detach” occurs.
CascadeType.ALL is shorthand for all of the above cascade operations.

FetchType defaults

Relationship Default
OneToOne Eager
OneToMany Lazy
ManyToOne Eager
ManyToMany Lazy

Spring transaction propagation

TransactionDefinition Note
ISOLATION_DEFAULT Use the default isolation level of the underlying datastore.
ISOLATION_READ_COMMITTED Indicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur.
ISOLATION_READ_UNCOMMITTED Indicates that dirty reads, non-repeatable reads and phantom reads can occur.
ISOLATION_REPEATABLE_READ Indicates that dirty reads and non-repeatable reads are prevented; phantom reads can occur.
ISOLATION_SERIALIZABLE Indicates that dirty reads, non-repeatable reads and phantom reads are prevented.
PROPAGATION_MANDATORY Support a current transaction; throw an exception if no current transaction exists.
PROPAGATION_NESTED Execute within a nested transaction if a current transaction exists, behave like PROPAGATION_REQUIRED else.
PROPAGATION_NEVER Do not support a current transaction; throw an exception if a current transaction exists.
PROPAGATION_NOT_SUPPORTED Do not support a current transaction; rather always execute non-transactionally.
PROPAGATION_REQUIRED Support a current transaction; create a new one if none exists.
PROPAGATION_REQUIRES_NEW Create a new transaction, suspending the current transaction if one exists.
PROPAGATION_SUPPORTS Support a current transaction; execute non-transactionally if none exists.
TIMEOUT_DEFAULT Use the default timeout of the underlying transaction system, or none if timeouts are not supported.

JPA lock modes

LockModeType Note
NONE No lock
READ Synonymous with OPTIMISTIC
WRITE Synonymous with OPTIMISTIC_FORCE_INCREMENT
OPTIMISTIC Always issues a version check upon transaction commit, therefore ensuring optimistic locking repeatable reads.
OPTIMISTIC_FORCE_INCREMENT Always increases the entity version (even when the entity doesn’t change) and issues a version check upon transaction commit, therefore ensuring optimistic locking repeatable reads.
PESSIMISTIC_READ A shared lock is acquired to prevent any other transaction from acquiring a PESSIMISTIC_WRITE lock.
PESSIMISTIC_WRITE An exclusive lock is acquired to prevent any other transaction from acquiring a PESSIMISTIC_READ or a PESSIMISTIC_WRITE lock.
PESSIMISTIC_FORCE_INCREMENT A database lock is acquired to prevent any other transaction from acquiring a PESSIMISTIC_READ or a PESSIMISTIC_WRITE lock and the entity version is incremented upon transaction commit.

Usage with EntityManager:

entityManager.find(User.class, 1L, LockModeType.PESSIMISTIC_READ)

Usage with EntityManager: Spring Data JPA

@Lock(LockModeType.PESSIMISTIC_READ)

JPQL polymorphism

###Type:

SELECT v FROM Vehicle v WHERE TYPE(v) = Plane

###Treat:

SELECT v FROM Vehicle v TREAT(v AS Plane).wings > 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment