简体   繁体   English

具有嵌套在不同实体类中的ID的实体类

[英]Entity Class with an Id nested in a different Entity Class

I got a database table MyTable with those columns: int Id, String Type, String Value 我得到了带有这些列的数据库表MyTable:int Id,字符串类型,字符串值

and I got an entity Class with those fields 我得到了带有这些字段的实体类

@Entity
@Table(name = "MyTable")
class MyClass {
    @Id
    int Id;
    String Type;
    String Value;
}

Now what I want are Classes that look like that: 现在我想要的是看起来像这样的类:

@Entity
class TypeA {
    MyClass myClass;
    //some stuff
}

The Id of TypeA should be MyClass.Id. TypeA的ID应该是MyClass.Id。 Can this be done with Hibernate? Hibernate可以做到吗? MyClass has to be a variable of TypeA, I explicitly do not want any inheritance. MyClass必须是TypeA的变量,我明确地不希望任何继承。

The only option I can see and comes close to your requirement but with one caveat is: 我可以看到的唯一接近您要求的选项,但有一个警告:

Using annotations Embeddable on MyClass and Embedded on TypeA will solve your problem partially. 使用在MyClassEmbeddable注释和在TypeAEmbedded注释可以部分解决您的问题。

Partially because of the caveat, ie, you should move id field from MyClass to the TypeA class. 部分由于警告,您应该将id字段从MyClass移到TypeA类。 That is the close you can get without using inheritance, I think. 我认为,这就是不用继承就可以实现的结果。

Would be interested to see if there really exists any solution that meets your requirement exactly. 有兴趣查看是否确实存在完全满足您要求的解决方案。

Anyways, if inheritance is an option, that would allow you to have id in the super class and have this super class annotated as MappedSuperClass and let TypeA extend super class. 无论如何,如果继承是一个选项,那将允许您在超类中具有id并将该超类注释为MappedSuperClass并让TypeA扩展超类。

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

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