如何解决使用@IdClass的实体休眠@ManyToOne
我刚刚在项目中添加了一个新实体,但现在它没有启动。
我认为这与使@ManyToOne
成为复合键的一部分有关,但是我不确定。
我将问题归结为以下实体:
A.class:
@Table(name = "A")
@Entity
@IdClass(KeyA.class)
public class A {
@Id
private String aId;
@Id
@ManyToOne
private B b;
public String getaId() {
return aId;
}
public void setaId(String aId) {
this.aId = aId;
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
B.class:
@Table(name = "B")
@Entity
@IdClass(KeyB.class)
public class B {
@Id
String bId;
@Id
String bId2;
public String getbId() {
return bId;
}
public void setbId(String bId) {
this.bId = bId;
}
public String getbId2() {
return bId2;
}
public void setbId2(String bId2) {
this.bId2 = bId2;
}
}
KeyA.class:
public class KeyA implements Serializable {
private String aId;
private String b;
}
KeyB.class
public class KeyB implements Serializable{
public String bId;
public String bId2;
}
引发异常:
Error creating bean with name 'entityManagerFactory' defined in class path resource
[...]
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.component.PojoComponentTuplizer]
[...]
Caused by: java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_201]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_201]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_201]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_201]
at org.hibernate.tuple.component.ComponentTuplizerFactory.constructTuplizer(ComponentTuplizerFactory.java:104) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
... 43 common frames omitted
Caused by: org.hibernate.PropertyNotFoundException: Could not locate field name [bId] on class [java.lang.String]
at org.hibernate.internal.util.ReflectHelper.findField(ReflectHelper.java:371) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
at org.hibernate.property.access.internal.PropertyAccessFieldImpl.<init>(PropertyAccessFieldImpl.java:34) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
at org.hibernate.property.access.internal.PropertyAccessstrategyFieldImpl.buildPropertyAccess(PropertyAccessstrategyFieldImpl.java:26) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
at org.hibernate.mapping.Property.getGetter(Property.java:311) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
at org.hibernate.tuple.component.PojoComponentTuplizer.buildGetter(PojoComponentTuplizer.java:141) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
我尝试了一切,包括导入最新的javassist。 我正在Java 1.8上运行Spring Boot 2.2.5.RELEASE(也发生在11中)
解决方法
您的A
组合键
public class KeyA implements Serializable {
private String aId;
private String b;
}
的结构就好像B
仅具有一个字符串ID(在此称为b
),而实际上它本身具有复合键
public class KeyB implements Serializable{
public String bId;
public String bId2;
}
因此您必须更改KeyA
才能正确表示该关系:
public class KeyA implements Serializable {
private String aId;
private KeyB keyB;
}
,
您应在KeyA
中使用与拥有实体中标记为@Id
完全相同的字段名称/类型。
因此,您应该通过以下方式更正KeyA
:
public class KeyA implements Serializable {
private String aId;
private B b;
}
请参见documentation中的进一步说明和示例。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。