JPA / Hibernate 中具有继承类的复合键
我在我的类结构中定义了一个复合 id,如下所示。不幸的是,我总是收到一个休眠错误,抱怨未找到“第 2 部分”:
“在实体 MoreClass 中找不到 @IdClass 的属性:第 2 部分”
任何人都可以帮助我解决问题吗? (或者至少给我指出一个有用的 jpa/hibernate 文档?)
@IdClass(ClassKey.class)
@Entity
public class MoreClass extends LessClass implements Serializable
{
@Id
String part1;
}
@MappedSuperclass
public class LessClass implements Serializable
{
@Id
String part2;
}
public class ClassKey implements Serializable
{
String part1;
String part2;
}
i have a composite id defined on my class structure as below. Unfortunatly i always get a hibernate error that complains on the not found "part2":
"Property of @IdClass not found in entity MoreClass : part2"
Can anybody help me on fixing the problem? (or at least point me on a helpful jpa/hibernate doc?)
@IdClass(ClassKey.class)
@Entity
public class MoreClass extends LessClass implements Serializable
{
@Id
String part1;
}
@MappedSuperclass
public class LessClass implements Serializable
{
@Id
String part2;
}
public class ClassKey implements Serializable
{
String part1;
String part2;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
上述提到的 Michael 的 HHH-9114 bug 的解决方法 ,例如在我的例子中,添加到
TwitterListedCount
:(请注意,必须添加@Id
和@Type
对于用户类型仍然有效)顺便说一句,该解决方法有一个令人讨厌的副作用 HHH-9350 当与模式生成一起使用时,它会生成重复的复合列:
我尝试根本不使用
@MappedSuperclass
,但错误的模式生成仍然发生。顺便说一句,我正在使用DefaultComponentSafeNamingStrategy
这可能是错误所在。这可能是一个不同的错误,在 Hibernate find with composite key 中询问。列名无效异常正确的解决方法包括手动添加
@Column(name=)
,这与架构生成配合良好:仅供参考,与 Spring Data JPA,需要删除
@IdMappedSuperclass
的 > 和@Type
注释。如果不删除这些,将会出现以下错误。顺便说一句,它不会改变这个 Hibernate bug 的性质。The mentioned workaround for HHH-9114 bug by Michael works, e.g. in my case by adding to
TwitterListedCount
: (note that both@Id
and@Type
must be added for user types to still work)BTW, the workaround has a nasty side-effect HHH-9350 when used with schema generation, it generates duplicate composite columns:
I tried to not use
@MappedSuperclass
at all, but the wrong schema generation still happens. BTW I'm usingDefaultComponentSafeNamingStrategy
which may be where the bug lies. This is probably a different bug, asked in Hibernate find with composite key. Invalid column name ExceptionThe proper workaround involves adding
@Column(name=)
manually, which works well with schema generation:FYI, when used together with Spring Data JPA, it's required to remove the
@Id
and@Type
annotations from theMappedSuperclass
. If these are not removed, there will be errors bellow. It doesn't change the nature of this Hibernate bug, BTW.实际上遇到了
同样的问题
。作为:
似乎确实有效,我认为这是一个错误。请参阅https://hibernate.atlassian.net/browse/HHH-9114。
Actually bumped into the
same problem
.As:
Does seem to work, I would deem it a bug. See https://hibernate.atlassian.net/browse/HHH-9114.
来自 JPA 规范:
因此,根据 JPA,您无法重新定义
@Id
。我不会称这是一个错误。尽管此处作为答案给出的解决方法可能有效,但对于其他 JPA 框架,它可能不起作用。
From the JPA spec:
So according to JPA you can't redefine the
@Id
. I wouldn't call this a bug.Although the workaround given as answer here might work, it may happen that for other JPA frameworks it doesn't work.