• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Hibernate Entities - Serializable

 
Ranch Hand
Posts: 401
2
Java
  • Mark post as helpful
  • send pies
  • 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: 10307
217
  • Mark post as helpful
  • send pies
  • 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.
 
Bartender
Posts: 20833
125
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • 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: 10307
217
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 20833
125
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!