Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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! 
like image 641
dmorar Avatar asked Jan 28 '16 06:01

dmorar


People also ask

What is the difference between Findby and findAll in JPA?

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.

What is @EmbeddedId in JPA?

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 .

What is the difference between CrudRepository and JpaRepository?

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.

Which of the following is the correct use case for @EmbeddedId annotation?

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.


2 Answers

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

like image 109
Javasick Avatar answered Sep 29 '22 13:09

Javasick


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); } 
like image 37
Alifia Avatar answered Sep 29 '22 13:09

Alifia