简体   繁体   English

Spring JPA:找不到带有@Any批注的对象的属性ID

[英]Spring JPA: no property id found for Object with `@Any` annotation

I wish to create an Attachment entity that may be related to entities of different types, so I am trying to use @Any annotation for that. 我希望创建一个可能与不同类型的实体相关的Attachment实体,因此我尝试为此使用@Any注释。 My code is like this: 我的代码是这样的:

@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;
}

My problem now is that when I start my app, DB initialization fails with: 现在的问题是,当我启动应用程序时,数据库初始化失败,并显示以下内容:
org.springframework.data.mapping.PropertyReferenceException: No property id found for type Object! Traversed path: Attachment.container

All the examples of using @Any I've found so far are exactly the same. 到目前为止,我发现的所有使用@Any的示例都完全相同。 So what is the right way of doing this? 那么正确的做法是什么?

The error says the for field: @JoinColumn(name = "container_id") private Object container; 错误显示为for字段:@JoinColumn(name =“ container_id”)私有对象容器;

You use JoinColumn , but JPA can not find the ID field in Object . 您使用JoinColumn ,但是JPA在Object找不到ID字段。 The meaning is, when you use JoinColumn , JPA needs to know the column name in current table, Attachment in this case, and the referenced column in the joined table, that is container . 意思是,当您使用JoinColumn ,JPA需要知道当前表中的列名(在这种情况下为Attachment ,以及联接表中的被引用列,即container

But as you said, you want to let this table related to entities of different types. 但是正如您所说,您希望让该表与不同类型的实体相关。 I think you can not do that. 我想你做不到。 In my understanding, JPA is a little like 'static' explanatory. 以我的理解,JPA有点像“静态”解释。 You can not make it 'dynamic'. 您不能使其“动态”。

So far I've managed to overcome this by providing an IAttachable interface with getId() method and replacing Object with IAttachable in my relation definition. 到目前为止,我已经设法通过提供带有getId()方法的IAttachable接口并在我的关系定义IAttachable Object替换为IAttachable来克服了这一问题。

Not sure if this is the best way, but it works so far. 不知道这是否是最好的方法,但是到目前为止它仍然有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM