对象类型的 JPA 映射注释 [英] JPA mapping annotations for Object type

查看:29
本文介绍了对象类型的 JPA 映射注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将实现一个实体类,例如:

I will implement an entity class, something like:

@Entity
public class XXXEntity {
     @Id
     private Long id;

     private Object entity;
}

但我不确定如何映射对象类型字段?

But I am not sure how to map the Object type field?

实际上,在我的项目中,对象类型包含其他几个实体,例如 A、B、C.(所有 A、B 和 C 都实现了 Serilizable 接口)

Actually, in my project the object types contain several other entities, like A, B, C. (all A,B, and C implement Serilizable interface)

我想将其注释为@Lob,但我不确定它是否正确?因为 Object 没有实现 Serilizable 接口.

I want to annotate it as @Lob, but I am not sure it is correct? Because Object doesn't implement Serilizable interface.

对此有什么想法吗?谢谢.

Any ideas about that? Thanks.

推荐答案

不,用@Lob 注释是不正确的.您不能直接将任意对象注释为持久属性.JPA 提供者无法知道如何持久化它.

No, it is not correct to annotate it with @Lob. You cannot directly annotate arbitrary Object as a persistent attribute. JPA provider have no way to know how to persist it.

根据 JPA 2.0 规范,持久属性必须是以下之一:

According JPA 2.0 specification persistent attribute must be one of following:

Java 原始类型;java.lang.String;其他 Java 可序列化类型(包括原始类型的包装器,java.math.BigInteger,java.math.BigDecimal,java.util.Date,java.util.Calendar[5],java.sql.Date, java.sql.Time, java.sql.Timestamp, byte[], Byte[],char[]、Character[] 和实现可序列化接口);枚举;实体类型;实体集合类型;可嵌入的类;基本类型和可嵌入类型的集合.

Java primitive types; java.lang.String; other Java serializable types (including wrappers of the primitive types, java.math.BigInteger,java.math.BigDecimal,java.util.Date,java.util.Calendar[5], java.sql.Date, java.sql.Time, java.sql.Timestamp, byte[], Byte[], char[], Character[], and user-defined types that implement the Serializable interface); enums; entity types; collections of entity types; embeddable classes; collections of basic and embeddable types.

你应该重新设计模型,这样就不需要持久化基本无类型的数据.

You should redesign model such a way that there is no need to persist basically typeless data.

这篇关于对象类型的 JPA 映射注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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