Why composite-id class must implement Serializable?
Asked Answered
M

1

70

If I make a composite-id class which doesn't implement Serializable like:

@Entity
@Table(name = "board")
public class Board {
    @Id
    @Column(name = "keyword_news_id")
    private int id;

    @Id
    @Column(name = "board_no")
    private int boardNo;
....

Errors occur like:

Caused by: org.hibernate.MappingException: composite-id class must implement Serializable: com.estinternet.news.domain.IssueNewsBoard
    at org.hibernate.mapping.RootClass.checkCompositeIdentifier(RootClass.java:263)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:244)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1362)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)

Hibernate entity classes doesn't need to be Serializable. Then, why does composite-id class must implement Serializable? I read this thread, but it didn't give me enough information.

Mutation answered 14/2, 2012 at 4:44 Comment(4)
https://mcmap.net/q/281429/-hibernate-composite-keyUncial
I read that but still cannot understand the root cause.Mutation
Where is your @IdClass ?Forefront
@BillyFrost This is code of the composite-id class. The @IdClass should be in the other class that I haven't added to here.Mutation
E
79

The session object needs to be serializable hence all objects referenced by it must be serializable as well. The id is used as a key to index loaded objects in the session. In case of CompositeId s the class itself is used as the id.

Electrocautery answered 14/2, 2012 at 11:44 Comment(7)
Then why single-id class doesn't need to be Serializable? Could you elaborate on that?Mutation
because the id is a primitive type which is by default serializableElectrocautery
Interesting, I did not see any requirement in JPA that composite id classes must be serializable. Serialization is not necessary to use an object as a key - for this the object must correctly implement hashcode() and equals() methods. Serialization is used when objects are to be stored in DB or on disk in plain text. This would be necessary only if complete object should be stored as text in single DB column. But this is not the case in JPA - each field of compound key is mapped in its respective column.Tortuous
My apology, I found the answer why the compound key must be serializable - it is simply required by the specification, as explain here. It is just not stated clearly in most examples on the internet.Tortuous
sessions might be serialized in the session cache of a web application. Thats why it has to be serializableElectrocautery
It would have been so simple to just wrap all @Id annotated attributes in a tuple or whatever wrapper that is serialisable...Balmuth
@LukasEder then you would lose the equals, hashcode and serialization code in the original class and might get different resultsElectrocautery

© 2022 - 2024 — McMap. All rights reserved.