- What is detached entity?
- What is the use of flush?
- What are the cache levels?
- What is the first-level cache?
- Hibernate inheritance?
- Common hibernate mistakes?
- Hibernate best practice?
- How to Define and Use a @NamedEntityGraph?
- Possible issues with defining equals/hashcode on JPA entities?
- How lazy loading works in hibernate?
- Optimal queries in Hibernate?
- Possible enum mapping solutions?
- In which cases LazyInitializationException can occur?
- There are three SQL statements in one transaction, during the first one exception occurred on DB level, what will happen with others if you catch the first one?
- A
new
instance of a persistent class which is not associated with aSession
, has no representation in the database and no identifier value is considered transient by Hibernate. - A persistent instance has a representation in the database, an identifier value and is associated with a
Session
. You can make a transient instance persistent by associating it with aSession
. - If we
close
the HibernateSession
, the persistent instance will become a detached instance: it isn't attached to aSession
anymore (but can still be modified and reattached to a newSession
later though).
Flushing the session forces Hibernate to synchronize the in-memory state of the Session with the database (i.e. to write changes to the database). By default, Hibernate will flush changes automatically for you:
- before some query executions
- when a transaction is committed
Allowing to explicitly flush the Session gives finer control that may be required in some circumstances (to get an ID assigned, to control the size of the Session,...).
- Session Cache. The session cache caches objects within the current session. It is enabled by default in Hibernate. Objects in the session cache reside in the same memory location.
- Second Level Cache. The second level cache is responsible for caching objects across sessions. When this is turned on, objects will first be searched in the cache and if they are not found, a database query will be fired. Second level cache will be used when the objects are loaded using their primary key. This includes fetching of associations. Second level cache objects are constructed and reside in different memory locations.
- Query Cache. Query Cache is used to cache the results of a query.
Session Cache. The session cache caches objects within the current session. It is enabled by default in Hibernate. Read more about Session Cache . Objects in the session cache reside in the same memory location.
- When you’re defining your associations, you should prefer FetchType.LAZY and map many-to-many associations to a java.util.Set.
- If your use case uses a lazily fetched association, you should initialize it within the query that loads the entity, e.g., with a JOIN FETCH expression.
- Cascading and updating or removing multiple entities require more SQL statements than you might expect. It’s often better to implement a bulk operation as a native, JPQL or Criteria query.
- https://stackoverflow.com/questions/4388360/should-i-write-equals-and-hashcode-methods-in-jpa-entities
- https://developer.jboss.org/wiki/EqualsandHashCode
There are three SQL statements in one transaction, during the first one exception occurred on DB level, what will happen with others if you catch the first one?
All Java code after Exception catching will be normally executed with other statements, but at the moment you want to try to close transaction, it won't commit any of statements, because transaction was marked as "to be rollbacked".