• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate Entities - Serializable

 
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hibernate entities should implement the Serializable interface. What is the reason to do so? If we are using plain JDBC, then there is such a restriction?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To store objects in a relation database, queries must be executed. in JDBC, you write these queries yourself.

Hibernate writes the queries for you, but it must first know how to convert entities to data that it can store in a database. This is why it uses Java's serialization mechanism.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:To store objects in a relation database, queries must be executed. in JDBC, you write these queries yourself.

Hibernate writes the queries for you, but it must first know how to convert entities to data that it can store in a database. This is why it uses Java's serialization mechanism.



Actually, no. Hibernate/JPA does not use Serialization as part of the normal CRUD mechanism. But if you want to be able to transfer JPA Entities between JVMs, then they must be Serializable, just like any other transferrable object.

The JPA spec says:

JSR wrote:
If an entity instance is to be passed by value as a detached object (e.g., through a remote interface), the entity class must implement the Serializable interface.



It should be noted, however, that if you are intending to use JPA Entities as session-scoped objects in a web application, you must make them Serializable, because all session-scope objects must be serializable. And, I should add, that you should detach them from the EntityManager between URL requests. Failure to do so is the equivalent of JDBC trying to pass a Connection between requests, and Connections are not Serializable (Connection is an interface, not a class), and thus can result in very unpredictable behavior.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Actually, no. Hibernate/JPA does not use Serialization as part of the normal CRUD mechanism.


Then I suppose it is done through reflection.
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Tim Holloway wrote:Actually, no. Hibernate/JPA does not use Serialization as part of the normal CRUD mechanism.


Then I suppose it is done through reflection.



Indeed it does. In fact, I believe it's using the Apache BeanUtils.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic