在应用程序运行时出错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 文档中阅读有关复合键的更多信息:
https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#identifiers-composite
关于java - Spring JPA 复合键 : This class does not define an IdClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60414409/