如何在 Spring Data JPA 中保留 Generic 类中的某些字段?
我有一个实体类如下。我需要的是,我需要使用泛型,通过使用它,我需要传递一些扩展 SomeInfo 类的类,并在 SubClass 表中保留一个公共字段,并且在返回数据时我可以填充 T
参数并轻松发送。但我无法直接扩展 SomeInfo 类,因为它包含我不需要在 SubClass 表中创建的字段,甚至我不需要使用继承单表解决方案
。我尝试过使用 @AttributeOverride 但它引发了错误,我使用了 @Type 注释并且错误消失了,但字段 info_name 却没有t 创建。
@Entity
public class SubClass<T extends SomeInfo> extends ParentClass {
@Type(type = "com.package.SomeInfo")
@AttributeOverrides({
@AttributeOverride(name = "name", column = @Column(name = "info_name", nullable = false))
})
private T info;
}
@MappedSuperClass
public abstract class SomeInfo extends ParentClass {
// some fields
}
有办法实现我所需要的吗?提前致谢。
I have an entity class as follows. What I need is, I need to use generics, and by using it I need to pass some classes that extend the SomeInfo class and persist a common field inside the SubClass table and when returning data I can populate T
parameter and send it easily. But I can't extend SomeInfo class directly because it contains fields that I don't need to be created in the SubClass table and even I don't need to use the Inheritance Single Table solution
. I have tried using @AttributeOverride
but it threw an error and I used @Type
annotation and the error was gone, but the field info_name
isn't created.
@Entity
public class SubClass<T extends SomeInfo> extends ParentClass {
@Type(type = "com.package.SomeInfo")
@AttributeOverrides({
@AttributeOverride(name = "name", column = @Column(name = "info_name", nullable = false))
})
private T info;
}
@MappedSuperClass
public abstract class SomeInfo extends ParentClass {
// some fields
}
Is there a way to achieve what I need? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论