Spring JPA Composite键:此类未定义IdClass [英] Spring JPA Composite key: This class does not define an IdClass

查看:116
本文介绍了Spring JPA Composite键:此类未定义IdClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用运行时出现错误此类[com.socnetw.socnetw.model.Relationship类]没有定义IdClass .当我使用EntityManager时,一切运行良好.但是现在我切换到Spring CrudRepository< T,T> 并得到此错误.我知道问题在于映射主键约束.但是我不知道该怎么办.有人可以帮忙吗?

Get an error on app run This 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?

Relationship.class

Relationship.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\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:

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:

https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#identifiers-composite

这篇关于Spring JPA Composite键:此类未定义IdClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆