68

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.

Community
  • 1
  • 1
Sanghyun Lee
  • 21,644
  • 19
  • 100
  • 126

1 Answers1

77

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.

Firo
  • 30,626
  • 4
  • 55
  • 94
  • 12
    Then why single-id class doesn't need to be Serializable? Could you elaborate on that? – Sanghyun Lee Feb 14 '12 at 12:11
  • 38
    because the id is a primitive type which is by default serializable – Firo Feb 14 '12 at 13:18
  • 4
    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. – OndroMih Sep 30 '15 at 13:14
  • 10
    My apology, I found the answer why the compound key must be serializable - it is simply required by the specification, as explain [here](http://stackoverflow.com/questions/3585034/how-to-map-a-composite-key-with-hibernate#answer-3588400). It is just not stated clearly in most examples on the internet. – OndroMih Sep 30 '15 at 13:25
  • 2
    sessions might be serialized in the session cache of a web application. Thats why it has to be serializable – Firo Sep 30 '15 at 13:37
  • It would have been so simple to just wrap all `@Id` annotated attributes in a tuple or whatever wrapper that is serialisable... – Lukas Eder Jun 21 '19 at 13:37
  • @LukasEder then you would lose the equals, hashcode and serialization code in the original class and might get different results – Firo Jul 15 '19 at 09:09