为什么composite-id类必须实现Serializable?

时间:2012-02-14 04:44:45

标签: java hibernate

如果我创建一个复合id类,它不实现Serializable,如:

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

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

出现错误,如:

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。那么,为什么composite-id类必须实现Serializable?我看过this thread,但它没有给我足够的信息。

1 个答案:

答案 0 :(得分:53)

会话对象需要是可序列化的,因此它引用的所有对象也必须是可序列化的。 id用作索引会话中加载的对象的键。在CompositeId的情况下,类本身用作id。