Search code examples
mysqlspringspring-data-jpaspring-data

Spring JPA - Query Methods


I have this table:

@Entity
@Table(name = "t_tropical",
        uniqueConstraints =
        @UniqueConstraint(columnNames = {
                "name",
                "sign",
                "isRetro",
                "house",
                "lang"}))
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
public class Tropical {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String sign;
    private Boolean isRetro;
    private Integer house;
    private String lang;
    private Date updateDate;
    
}

and this method:

@Repository
public interface TropicalRepository extends JpaRepository<Tropical, Long> {

    Tropical findByHouseIsAndSignAndIsRetroAndLangIs
            (Integer house, String sign, Boolean retro, String lang);

}

but does not seems to work. I found for existing house / sign / isRetro and land and return null


Solution

  • The correct query should look as follows using the JPA naming conventions:

    @Repository
    public interface TropicalRepository extends JpaRepository<Tropical, Long> {
    
        Tropical findByHouseAndSignAndIsRetroAndLang
                (Integer house, String sign, Boolean retro, String lang);
    
    }
    

    In your query there seemed to be an Is appended to House and to Lang. These cannot be resolved based on the attributes in your entity.