【问题标题】:This class does not define an IdClass此类未定义 IdClass
【发布时间】:2020-03-20 13:01:26
【问题描述】:

My for class Class1Item 是对 Class1 中的条目的引用。 Hibernate 需要我定义一个@IdClass。用Class1Id 定义自己的类对我不起作用。

这是我得到的错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: 创建定义名称为“requestMappingHandlerAdapter”的 bean 时出错 在类路径资源中 [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: 通过方法表达的不满足的依赖关系 'requestMappingHandlerAdapter' 参数 1;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 使用在类路径中定义的名称“mvcConversionService”创建 bean 资源 [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: 通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化 [org.springframework.format.support.FormattingConversionService]: 工厂方法“mvcConversionService”抛出异常;嵌套的 例外是 org.springframework.beans.factory.BeanCreationException: 创建名为“bookItemRepository”的 bean 时出错:调用 init 方法失败;嵌套异常是 java.lang.IllegalArgumentException: 此类 [class de.models.Class1Item] 没有定义 IdClass

都在同一个包中的相关类:

import javax.persistence.*;
import javax.validation.constraints.NotNull;

import lombok.*;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.List;

@Entity
@Setter
@Getter
@NoArgsConstructor
@IdClass(Class1ItemId.class)
public class Class1Item implements Serializable {

    @Id
    @ManyToOne
    @JoinColumn(name="reference")
    private Class1 class1;

    @NotNull
    @GeneratedValue
    private Long number;

    @Column(name="date_one")
    private LocalDate date1;

    @Column(name="date_x")
    private LocalDate dateX;

    @ManyToOne
    @JoinColumn(name="userReference")
    private User userReference;

    @ManyToMany
    @JoinColumn(name="waitingUsers")
    private List<User> waitingUsers;

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(name="enumX")
    private EnumX enumX;

}

import javax.persistence.*;
import javax.validation.constraints.NotBlank;

import lombok.*;

import java.io.Serializable;

@Entity
@Setter
@Getter
@NoArgsConstructor
public class Class1 implements Serializable {

    @Id
    @NotBlank
    private String key;

    @NotBlank
    private String name;

    @NotBlank
    private String nameF;

    @NotBlank
    private String nameL;

    private String path;
}

import javax.persistence.*;
import javax.validation.constraints.NotNull;

import lombok.*;

import java.io.Serializable;
import java.util.Set;

@Entity
@Setter
@Getter
@NoArgsConstructor
public class User implements Serializable {

    @Id
    @Column(name="UserID")
    private Long id;

    @NotNull
    @Column(name="email",unique=true)
    private String email;

    @NotNull
    @Column(name="user_firstname")
    private String firstName;

    @NotNull
    @Column(name="user_lastname")
    private String lastName;

    @NotNull
    @Column(name="isAdmin")
    private Boolean isAdmin;

    @ManyToMany
    @Column(name="wantedItems")
    private Set<Class1Item> wantedItems;

    @OneToMany
    @Column(name="borrowedItems")
    private Set<Class1Item> itemsBorrowed;
}

import javax.persistence.Id;
import java.io.Serializable;

public class Class1ItemId implements Serializable {
    @Id
    private String key;
}

如果确实定义了 IdClass,我该如何解决这个错误?

【问题讨论】:

    标签: java spring hibernate jpa


    【解决方案1】:

    您定义了一个@IdClass,但没有在您的 ClassItem @Entity 中重新声明密钥。它们必须匹配。

    你需要像这样添加它们:

    @Entity
    @IdClass(Class1ItemId.class)
    // others
    public class Class1Item implements Serializable {
    
        @Id
        private String key; // from your Class1ItemId
    
      // others
    
    }
    

    【讨论】:

    • private Class1 class1;in Class1ItemId.java 该怎么办?
    • 对我来说,它看起来像一个 \@ManyToOne,而不是 \@Id 字段,因此您删除了 \@ID。一般来说,在这里给你的代码命名更有意义,因为在你的头脑中很难在 class1、class1item、class1itemid 之间进行映射:)
    • 在此示例中,您必须将 Class1 视为具有已定义规格的汽车模型,而 Class1Item 是实际汽车,它是数据库中的一个实例。假设 Car 的“实例”将是“VW Golf”,而 class1Item 将是实际的汽车,并定义了所有者等。我使用占位符名称进行进一步抽象。
    猜你喜欢
    • 1970-01-01
    • 2020-06-10
    • 2020-04-08
    • 1970-01-01
    • 2015-11-08
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多