JPA mapping annotations for Object type

时间秒杀一切 提交于 2019-12-25 02:33:31

问题


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?

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

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.


回答1:


You can not annotate Object field as @Lob. According to spec: "A Lob may be either a binary or character type."




回答2:


You should decide whether A, B and C are entities or state field values. If they are entities and they can be referenced, queried by their properties, create a common super class a go with the inheritance.

If they are just an object then using Lob and implementing Serializable should suffice. If the JPA provider requires you to replace Object with something else, as you have stated you can use Serializable interface, if that does not work then a common super class which implements Serializable.




回答3:


It seems to me you want to use it as "ValueObject" (as u mentioned serializable) instead of "Entity".

It is fine if you treat the field as ValueObject. Using Lob should be fine (and you can always find workaround by manually serializing it).

But if you want to treat them as "Entity ", it will never be reasonable to declare it as ref to "Object". One of the most obvious argument is Object is NOT an Entity, as it doesn't contains any entity identity.




回答4:


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.

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

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.



来源:https://stackoverflow.com/questions/11735501/jpa-mapping-annotations-for-object-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!