[英]Spring JPA Composite key: This class does not define an IdClass
在應用程序運行時出錯This class [class com.socnetw.socnetw.model.Relationship] does not define an IdClass
。 當我使用 EntityManager 時,一切都運行良好。 但是現在我切換到 Spring CrudRepository<T, T>
並收到此錯誤。 我知道問題是關於映射主鍵約束。 但我不知道我到底應該做什么。 有人可以幫忙處理嗎?
關系類
@Table(name = "RELATIONSHIP")
@Getter
@Setter
@Entity
@ToString
@EqualsAndHashCode
public class Relationship implements Serializable {
@Id
private Long userIdFrom;
@Id
private Long userIdTo;
@Enumerated(EnumType.STRING)
private RelationshipStatus status;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "YYYY-MM-DD HH:mm:ss")
private LocalDate friendsRequestDate;
}
RelationshipRepository.class 僅用於案例
public interface RelationshipRepository extends CrudRepository<Relationship, Long> {
@Query(value = "some query", nativeQuery = true)
Long findAmountOfFriends(@Param("userId") Long userId);
...other methods
}
數據初始化類
@Component
public class DataInit implements ApplicationListener<ContextRefreshedEvent> {
private UserRepository userRepository;
private PostRepository postRepository;
private RelationshipRepository relationshipRepository;
private MessageRepositorys messageRepository;
public DataInit(UserRepository userRepository, PostRepository postRepository, RelationshipRepository relationshipRepository, MessageRepositorys messageRepository) {
this.userRepository = userRepository;
this.postRepository = postRepository;
this.relationshipRepository = relationshipRepository;
this.messageRepository = messageRepository;
}
@Override
@Transactional
public void onApplicationEvent(ContextRefreshedEvent event) {
//here I create users and save them
...
...
...
userRepository.save(someUser);
relationshipRepository.save(relationship);
messageRepository.save(message);
}
}
錯誤
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataInit' defined in file [C:\Users\tpmylov\Desktop\learning\Projects\socnetw\target\classes\com\socnetw\socnetw\bootstrap\DataInit.class]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'relationshipRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: This class [class com.socnetw.socnetw.model.Relationship] does not define an IdClass
...
...
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'relationshipRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: This class [class com.socnetw.socnetw.model.Relationship] does not define an IdClass
你有一個復合鍵:
@Id
private Long userIdFrom;
@Id
private Long userIdTo;
為此,您必須創建一個 IdClass:
public class RelationshipId implements Serializable {
private Long userIdFrom;
private Long userIdTo;
// Getter and Setter
}
然后你可以在課堂上使用它
@IdClass(RelationshipId.class)
public class Relationship ....
在存儲庫上:
public interface RelationshipRepository
extends CrudRepository<Relationship, RelationshipId> {
@Query(value = "some query", nativeQuery = true)
Long findAmountOfFriends(@Param("userId") Long userId);
...other methods
}
在官方 Hibernate 文檔中閱讀有關復合鍵的更多信息:
您的實體Relationship
聲明假定使用復合主鍵。 因此,您應該通過以下方式更正您的實體:
@Table(name = "RELATIONSHIP")
@Getter
@Setter
@Entity
@ToString
@EqualsAndHashCode
@IdClass(RelationshipPK.class)
public class Relationship implements Serializable {
@Id
private Long userIdFrom;
@Id
private Long userIdTo;
@Enumerated(EnumType.STRING)
private RelationshipStatus status;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "YYYY-MM-DD HH:mm:ss")
private LocalDate friendsRequestDate;
}
然后RelationshipPK
:
public class RelationshipPK implements Serializable {
private Long userIdFrom;
private Long userIdTo;
public RelationshipPK(Long userIdFrom, Long userIdTo) {
this.userIdFrom = userIdFrom;
this.userIdTo = userIdTo;
}
public RelationshipPK() {
}
//Getters and setters are omitted for brevity
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
RelationshipPK pk = (RelationshipPK) o;
return Objects.equals( userIdFrom, pk.userIdFrom ) &&
Objects.equals( userIdTo, pk.userIdTo );
}
@Override
public int hashCode() {
return Objects.hash( userIdFrom, userIdTo );
}
}
並且您的CrudRepository
應具有以下視圖:
public interface RelationshipRepository extends CrudRepository<Relationship, RelationshipPK>
{
@Query(value = "some query", nativeQuery = true)
Long findAmountOfFriends(@Param("userId") Long userId);
...other methods
}
實際上,hibernate 允許聲明一個復合鍵而沒有這些東西。 這就是所謂的具有關聯的復合標識符。 但是對於以下CrudRepository方法是必要的:
public interface CrudRepository<T,ID> extends Repository<T,ID>
{
void deleteById(ID id);
boolean existsById(ID id);
Iterable<T> findAllById(Iterable<ID> ids);
Optional<T> findById(ID id);
// ...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.