Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why composite-id class must implement Serializable?

Tags:

java

hibernate

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.

like image 245
Sanghyun Lee Avatar asked Feb 14 '12 04:02

Sanghyun Lee


People also ask

Why do we need to implement Serializable class?

Implement the Serializable interface when you want to be able to convert an instance of a class into a series of bytes or when you think that a Serializable object might reference an instance of your class. Serializable classes are useful when you want to persist instances of them or send them over a wire.

Is it necessary to implement Serializable?

If you want a class object to be serializable, all you need to do it implement the java. io. Serializable interface. Serializable in java is a marker interface and has no fields or methods to implement.

What will happen if we don't implement Serializable?

The Student would not be Serializable, and it will act like a normal class. Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link.

Which classes should implement Serializable?

The Serializable interface must be implemented by the class whose object needs to be persisted. The String class and all the wrapper classes implement the java. io. Serializable interface by default.


1 Answers

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.

like image 84
Firo Avatar answered Sep 23 '22 13:09

Firo