问题描述
在应用程序运行时出错这个类[class com.socnetw.socnetw.model.Relationship]没有定义IdClass
.当我使用 EntityManager 时一切正常.但是现在我切换到 Spring CrudRepository
并收到此错误.我知道问题是关于映射主键约束.但我不知道我到底应该做什么.有人能帮忙解决吗?
Get an error on app runThis class [class com.socnetw.socnetw.model.Relationship] does not define an IdClass
.When I used EntityManager all worked well. But now I switch to Spring CrudRepository<T, T>
and get this error. I know problem's about mapping primary key constraint. But what exactly I should to do I dont know. Could some one help hendle it?
关系.class
@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 仅用于案例
RelationshipRepository.class just for case
public interface RelationshipRepository extends CrudRepository<Relationship, Long> {
@Query(value = "some query", nativeQuery = true)
Long findAmountOfFriends(@Param("userId") Long userId);
...other methods
}
DataInit.class
DataInit.class
@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 pmylovDesktoplearningProjectssocnetw argetclassescomsocnetwsocnetwootstrapDataInit.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:
For this you have to create an IdClass:
public class RelationshipId implements Serializable {
private Long userIdFrom;
private Long userIdTo;
// Getter and Setter
}
然后就可以在课堂上使用了
Then you can use it on the class
@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 文档中阅读有关复合键的更多信息:
Read more about composite keys in the official Hibernate documentation:
这篇关于Spring JPA 复合键:此类未定义 IdClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!