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
Introduction to Composite Primary Keys and Primary Key Classes. The EJB 3.0 specification allows you to define a primary key class as a @Embeddable and use it as the primary key of your Entity bean. One or more properties can be used as members of the primary key for that particular table.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With