简体   繁体   English

使用 @IdClass 休眠实体的 @ManyToOne

[英]Hibernate @ManyToOne of an entity with @IdClass

I just added a new entity to my project and now it doesn't start.我刚刚在我的项目中添加了一个新实体,现在它没有启动。 I think it is related to having a @ManyToOne being part of a composite key but I'm not sure.我认为这与@ManyToOne成为复合键的一部分有关,但我不确定。 I reduced my problem to the following entities:我将我的问题简化为以下实体:

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: B.类:

@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: KeyA.class:

public class KeyA implements Serializable {
    
   private String aId;
   private String b;
}

KeyB.class KeyB.class

public class KeyB implements Serializable{
    
   public String bId;
   public String bId2;
}

Exception thrown:抛出异常:

        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]

I tried everything, including importing latest javassist.我尝试了一切,包括导入最新的 javassist。 I'm running Spring Boot 2.2.5.RELEASE on Java 1.8 (it also happens in 11)我在 Java 1.8 上运行 Spring Boot 2.2.5.RELEASE(它也在 11 中发生)

Your composite key of A你的A组合键

public class KeyA implements Serializable {

    private String aId;
    private String b;
}

is structured as if B would have only a single string ID (here referred to as b ) while in reality it has a composite key itself结构就像B只有一个字符串 ID(这里称为b ),而实际上它本身有一个复合键

public class KeyB implements Serializable{

    public String bId;
    public String bId2;
}

so you have to change KeyA to represent that relationship correctly:因此您必须更改KeyA以正确表示该关系:

public class KeyA implements Serializable {

    private String aId;
    private KeyB keyB;
}

You should use in your KeyA exactly the same fields name/types as marked in the owned entity as @Id .您应该在KeyA使用与拥有的实体中标记为@Id完全相同的字段名称/类型。

So, you should correct KeyA in the following way:因此,您应该通过以下方式更正KeyA

public class KeyA implements Serializable {

    private String aId;
    private B b;
}

See further explanation and examples in the documentation .请参阅文档中的进一步说明和示例。

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

相关问题 具有复合键的自引用实体中的 @ManyToOne - @IdClass、java、hibernate - @ManyToOne in self-referencing entity with composite key - @IdClass, java, hibernate 将@IdClass与Hibernate和Arquillian强制CascadeType.Insert一起使用在ManyToOne外键对象上,该对象是实体组合主键的一部分 - Using @IdClass with Hibernate and arquillian force CascadeType.Insert on a ManyToOne foreign key object that is part of entity composite primary key 通过@ManyToOne字段休眠@Filter实体 - Hibernate @Filter entity by @ManyToOne field Hibernate @ManyToOne @OneToMany为同一个实体 - Hibernate @ManyToOne @OneToMany for the same entity Hibernate对基本实体的manyToOne过滤器 - Hibernate manyToOne filter on base entity Hibernate @ManyToOne 引用了一个未知实体 - Hibernate @ManyToOne references an unknown entity Hibernate @ManyToOne“目标实体未定义” - Hibernate @ManyToOne “Target Entity is not defined” 休眠异常:无法在以@IdClass注释的实体中找到属性… - Hibernate Exception: Unable to find properties … in entity annotated with @IdClass 通过IdClass使用复合键的Hibernate JPA ManyToOne外键映射找不到列 - Hibernate JPA ManyToOne foreign key mapping with composite key via IdClass does not find columns Hibernate“ManyToOne ...引用未知实体”异常 - Hibernate “ManyToOne … references an unknown entity” exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM