I am learning with bootspring.
findByDate(int date);
used to work until I've moved int Date
into the inner class.
Now I can save new entries but I can't retrive 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!
No, there is no difference between them, they will execute exactly the same query, the All part is ignored by Spring Data when deriving the query from the method name.
Annotation Type EmbeddedIdApplied to a persistent field or property of an entity class or mapped superclass to denote a composite primary key that is an embeddable class. The embeddable class must be annotated as Embeddable .
Their main functions are: CrudRepository mainly provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sorting records. JpaRepository provides some JPA-related methods such as flushing the persistence context and deleting records in a batch.
The use of @Id with a class marked as @Embeddable is the most natural approach. The @Embeddable tag can be used for non-primary key embeddable values anyway. It allows you to treat the compound primary key as a single property, and it permits the reuse of the @Embeddable class in other tables.
You should include name of embedded key class in repository instead of Long. Try this one (not tested):
public interface ExpirationDAO extends JpaRepository<ExpirationDTO, IdKey> { public List<ExpirationDTO> findByIdDate(int date); }
There after findBy
Id
is yours EmbeddedId
and Date
is attribute of embeddable class. And one more thing: if you use only part of embedded key, you can't expect only one result...
You should include name of embedded key class in repository and also add an underscore (_)
Tested below:
public interface ExpirationDAO extends JpaRepository<ExpirationDTO, IdKey> { public List<ExpirationDTO> findByIdKey_Date(Date date); }
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