Iam learning hibernate now. I came to know that when we need a composite primary key then we will put those variables inside one class which must implement serializable. when it comes to other than composite primary key i.e. single primary key of datatype primitives or Wrapper classes, these are directly or indirectly implementing serializable interfece. Upto here Iam ok,but my question is why we need to implement serializable interface?? what is purpose of implementing serializable interface in hibernate??
Asked
Active
Viewed 1,119 times
3
-
What is your level of understanding of serialization? Maybe read this: https://docs.oracle.com/javase/tutorial/jndi/objects/serial.html – JohannesB May 22 '19 at 15:37
-
@JohannesB I know if we implement serializable we can either store in file or send through the network.But what is the purpose of using it in hibernate?? – Vidyasagar.dharna May 23 '19 at 14:29
-
See: https://stackoverflow.com/questions/9271835/why-composite-id-class-must-implement-serializable – JohannesB May 23 '19 at 17:43
-
@JohannesB.Finally i came to a conclusion after reading all links that session must be serialziable in hibernate and will get or update entity object by using primary key only.i.e PK column should be serialzable .Is that correct?? or do u have any other reasons?? – Vidyasagar.dharna May 24 '19 at 04:28
-
That was my impression as well, probably because of second level cache APIs but doesn't it make a lot of sense that if you want to store Java objects frameworks rely on the Java standards like Serializable? – JohannesB May 25 '19 at 08:42
-
I think PK needs to be serializable because of just to make PK as a supertype of all. – Vidyasagar.dharna Jun 03 '19 at 07:12
1 Answers
1
You must implement the serializable interface with composite primary keys so you can uniquely identify an instance of the class that has the composite primary key.
The serializable interface allows hibernate to compare the object as a row in the database to all other rows in the database.
Consider the following kotlin entity class that represents the employee table:
@Entity
@Table(name = "Employee")
class Employee (
@EmbeddedId
var employeeKey: EmployeeKey
)
The EmployeeKey
is a composite key class which looks like this:
@Embeddable
class EmployeeKey(
@Column(name = "first_name")
var firstName: String,
@Column(name = "last_name")
var lastName: String,
@Column(name = "employee_id")
var employeeId: Long
) : Serializable {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as EmployeeKey
if (firstName != other.firstName) return false
if (lastName != other.lastName) return false
if (employeeId != employeeId) return false
return true
}
override fun hashCode(): Int {
var result = firstName.hashCode()
result = 31 * result + lastName.hashCode()
result = 31 * result + employeeId.hashCode()
return result
}
}
The overridden methods define contracts for whether or not employee keys are equal to one another, as well as generating hash codes to uniquely identify each employee key on the hibernate side.

LostAtSea
- 5,384
- 1
- 9
- 11