Custom queries with CrudRepository

后端 未结 3 1191
夕颜
夕颜 2021-02-04 12:19

I would like to custom my queries with CrudRepository :

This is my code:

@Repository
public interface CustomerRepository extends CrudRepository

        
3条回答
  •  灰色年华
    2021-02-04 12:38

    Also take in account that when you're creating custom queries you must use the Entity structure and not the final database structure. For example you can have a composed private key. Then you Entity would have an attribute which would be the entity with the attributes that conform the private key. Example:

    @Entity(name = "itemPrice")
    public class ItemPriceEntity {
    
        @EmbeddedId
        private ItemPriceComposedPK composedPK;
    
        // Rest of the fields
        ...
    
        // Getters and setters
        ...
    }
    
    @Embeddable
    public class ItemPriceComposedPK implements Serializable {
    
       @Column
       private String id;
    
       @Column
       private int iteration;
    
       // Getters and setters
       ...
    }
    

    If you want to use the field iteration, then you must code something like: i.composedPK.iteration. Example:

    @Modifying
    @Query("select i from itemPrice i where i.composedPK.iteration = :it")
    public List searchByIteration(@Param("it") String it);
    

    Of course CrudRepository offers a better way to make this query without using custom queries but, as an example, is ok.

    Regards!

提交回复
热议问题