Why composite-id class must implement Serializable?

JavaHibernate

Java Problem Overview


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)

https://stackoverflow.com/questions/2726300/do-hibernate-table-classes-need-to-be-serializable">Hibernate entity classes doesn't need to be Serializable. Then, why does composite-id class must implement Serializable? I read https://forum.hibernate.org/viewtopic.php?f=1&t=959637">this thread, but it didn't give me enough information.

Java Solutions


Solution 1 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSanghyun LeeView Question on Stackoverflow
Solution 1 - JavaFiroView Answer on Stackoverflow