我有一个使用 JPA 映射到数据库表的 Java 类。在这个类中我有一个 List<Code>
我需要存储在数据库中。
所以Code
类未映射到 Hibernate。有没有办法序列化 List<Code>
不将 Code 类映射到 Hibernate 中?提前致谢。
错误消息:
org.hibernate.exception.SQLGrammarException: could not insert collection [com.app.Account.codes#2]
问题是当 Hibernate 尝试序列化我的列表时出现错误。
@Entity
@Table (name="account", catalog="database1")
public class Account{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column (name = "id")
private String id
@Column (name = "type")
private String type;
@CollectionOfElements
@Column (name = "codes")
List<Code> codes;
...
...
}
public class Code implements Serializable{
//This is a basic POJO that is not mapped into Hibernate. I just want this object
//to be stored in database.
}
最佳答案
您需要使用@Lob
来注释codes字段,而不是使用@CollectionOfElements
。
参见http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#d0e6099以及以下段落。
不过,这是一种非常脆弱的持久对象列表的方法,因为它使用二进制序列化,如果列表或 Code 类发生更改,它可能很快就会变得不向后兼容。也无法使用数据库工具进行查询或检查。
我宁愿将 Code 类映射为实体或可嵌入。
关于java - 使用 Hibernate 序列化 POJO 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8302831/