Spring JPA Composite key: этот класс не определяет IdClass - PullRequest
1 голос
/ 26 февраля 2020

Получить ошибку при запуске приложения This class [class com.socnetw.socnetw.model.Relationship] does not define an IdClass. Когда я использовал EntityManager все работало хорошо. Но теперь я переключаюсь на Spring CrudRepository<T, T> и получаю эту ошибку. Я знаю, проблема заключается в отображении ограничения первичного ключа. Но что именно я должен делать, я не знаю. Может ли кто-нибудь помочь это исправить?

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 только для случая

public interface RelationshipRepository extends CrudRepository<Relationship, Long> {

    @Query(value = "some query", nativeQuery = true)
    Long findAmountOfFriends(@Param("userId") Long userId);  
    ...other methods

}

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

Ответы [ 2 ]

1 голос
/ 26 февраля 2020

Декларация вашей сущности 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);

   // ...
}
0 голосов
/ 26 февраля 2020

У вас есть составной ключ:

@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 -композит

...