[英]Spring JPA: no property id found for Object with `@Any` annotation
我希望创建一个可能与不同类型的实体相关的Attachment
实体,因此我尝试为此使用@Any
注释。 我的代码是这样的:
@Entity
public class Attachment{
@XmlElement
@Any(metaColumn = @Column(name = "containerType"), fetch = FetchType.LAZY)
@AnyMetaDef(idType = "long", metaType = "string",
metaValues = {
@MetaValue(targetEntity = TestApp.class, value = "TestApp")
})
@JoinColumn(name = "container_id")
private Object container;
@XmlElement
@Column(insertable = false, updatable = false) //this I added cause Hibernate said so
private String containerType;
}
现在的问题是,当我启动应用程序时,数据库初始化失败,并显示以下内容:
org.springframework.data.mapping.PropertyReferenceException: No property id found for type Object! Traversed path: Attachment.container
到目前为止,我发现的所有使用@Any
的示例都完全相同。 那么正确的做法是什么?
错误显示为for字段:@JoinColumn(name =“ container_id”)私有对象容器;
您使用JoinColumn
,但是JPA在Object
找不到ID字段。 意思是,当您使用JoinColumn
,JPA需要知道当前表中的列名(在这种情况下为Attachment
,以及联接表中的被引用列,即container
。
但是正如您所说,您希望让该表与不同类型的实体相关。 我想你做不到。 以我的理解,JPA有点像“静态”解释。 您不能使其“动态”。
到目前为止,我已经设法通过提供带有getId()
方法的IAttachable
接口并在我的关系定义IAttachable
Object
替换为IAttachable
来克服了这一问题。
不知道这是否是最好的方法,但是到目前为止它仍然有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.