简体   繁体   中英

Spring JPA Composite key: This class does not define an IdClass

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

@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 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

@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);

    }
}

Error

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

You have a composite key:

@Id
private Long userIdFrom;
@Id
private Long userIdTo;

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 ....

And on the Repository:

public interface RelationshipRepository 
                 extends CrudRepository<Relationship, RelationshipId> {

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

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

Your entity Relationship declaration assumes usage of composite primary key . So, you should correct your entity in the following way:

@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;
}

and then 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 );
    }
}

and your CrudRepository should have the following view:

public interface RelationshipRepository extends CrudRepository<Relationship, RelationshipPK>
{

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

}

Actually, hibernate allows to declare a composite key and without this stuff. This is so called composite identifiers with associations . But it is necessary for the following CrudRepository methods:

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);

   // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM