为什么复合 ID 类必须实现可序列化? [英] Why composite-id class must implement Serializable?

查看:36
本文介绍了为什么复合 ID 类必须实现可序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我创建一个未实现可序列化的复合 id 类,例如:

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;
....

出现如下错误:

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 实体类不需要可序列化.那么,为什么composite-id类必须实现Serializable呢?我阅读了这个帖子,但它没有给我足够的信息信息.

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.

推荐答案

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

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.

这篇关于为什么复合 ID 类必须实现可序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆