My for class Class1Item
is a reference to an entry in Class1
. Hibernate needs me to define an @IdClass
. Defining an own Class with the Id
of Class1
did not work for me.
Here is the error I am getting:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Unsatisfied dependency expressed through method 'requestMappingHandlerAdapter' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookItemRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: This class [class de.models.Class1Item] does not define an IdClass
Relevant Classes which are all in the same package:
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;
}
How do I fix this error when there's actually an IdClass defined?
You have an @IdClass defined, but not the key re-declared in your ClassItem @Entity. They must match.
You need to add them like so:
@Entity
@IdClass(Class1ItemId.class)
// others
public class Class1Item implements Serializable {
@Id
private String key; // from your Class1ItemId
// others
}