What is the difference between the Hibernate Session
class and the EntityManager
class? I know that EntityManager
implements the Java Persistence API, but I am not sure what its relation is with Session
. Are they related at all?
Session is a hibernate-specific API, EntityManager is a standardized API for JPA. You can think of the EntityManager as an adapter class that wraps Session (you can even get the Session object from an EntityManager object via the getDelegate() function).
Hibernate provides implementation of JPA interfaces EntityManagerFactory and EntityManager . EntityManagerFactory provides instances of EntityManager for connecting to same database. All the instances are configured to use the same setting as defined by the default implementation.
1)The first difference between save and persist is there return type. Similar to save method, persist also INSERT records into the database, but return type of persist is void while return type of save is Serializable Object.
SessionFactory is a factory class for Session objects. It is available for the whole application while a Session is only available for particular transaction. Session is short-lived while SessionFactory objects are long-lived. SessionFactory provides a second level cache and Session provides a first level cache.
Session
is a hibernate-specific API, EntityManager
is a standardized API for JPA. You can think of the EntityManager
as an adapter class that wraps Session
(you can even get the Session
object from an EntityManager
object via the getDelegate()
function).
This is not dissimilar to other Java APIs around (for example, JDBC is a standard API, each vendor adapts its product to the API via a driver that implements the standard functions).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With