How to use "findBy" in Entity with Composite PK (Hibernate JPA)

I participate in bootspring.

findByDate(int date); used to work until I moved int Date to inner class.

Now I can save new records, but I can not restore them byDate

What do i need to change?

 @Transactional public interface ExpirationDAO extends JpaRepository<ExpirationDTO, Long> { public ExpirationDTO findByDate(int date); } 

and

 @Embeddable public static class IdKey implements Serializable{ @NotNull int date; @ManyToOne ProductDTO product; public IdKey(){ } //setters and getters } @EmbeddedId private IdKey id; @NotNull int units; public ExpirationDTO(){ } //setters and getters } 

throws this exception:

 org.springframework.data.mapping.PropertyReferenceException: No property date found for type ExpirationDTO! 
+6
source share
2 answers

Instead of Long, you must specify the name of the built-in key class in the repository. Try this (not verified):

 public interface ExpirationDAO extends JpaRepository<ExpirationDTO, IdKey> { public List<ExpirationDTO> findByIdDate(int date); } 

Here, after findBy Id your EmbeddedId and Date is an attribute of the nested class. And one more thing: if you use only part of the built-in key, you cannot expect only one result ...

+15
source

You must include the name of the built-in key class in the repository, and also add an underscore (_)

Tested below:

 public interface ExpirationDAO extends JpaRepository<ExpirationDTO, IdKey> { public List<ExpirationDTO> findByIdKey_Date(Date date); } 
0
source

All Articles