gpt4 book ai didi

java - 为什么复合ID类必须实现Serializable?

转载 作者:IT老高 更新时间:2023-10-28 20:42:45 26 4
gpt4 key购买 nike

如果我创建了一个没有实现 Serializable 的复合 ID 类,例如:

@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 ,但它没有给我足够的信息。

最佳答案

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

关于java - 为什么复合ID类必须实现Serializable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9271835/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com