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 query approach you’ve composed within the TropicalRepository should function accurately, assuming the data in your database aligns with the stipulated conditions. Nevertheless, there’s a minor typographical error in your query method parameter name. You’ve used retro instead of isRetro. Java conventions typically employ the "is" prefix for boolean attributes, so your method should be:
Tropical findByHouseIsAndSignAndIsRetroAndLang
(Integer house, String sign, Boolean isRetro, String lang);
Should your query still not yield the desired outcome, here are a few factors you can examine:
-
Data Correspondence: Ensure that the data within your database coincides with the criteria you’re aiming to retrieve. Verify if the values of
house,sign,isRetro, andlangprecisely match what you’re inputting into the query method. -
Database Configuration: Validate that your database is properly configured and linked to your Spring Boot application.
-
Logging: Activate Spring Data JPA query logging to view the concrete SQL query being formulated and executed. You can append the following property to your
application.propertiesorapplication.ymlfile:logging.level.org.springframework.data.jpa=DEBUGThis will display the SQL queries in the application logs, aiding in the identification of any issues.
-
Naming Convention: Reconfirm the field names in your
Tropicalentity class. The letter case of the field names must match the case used in the query method. Java is case-sensitive. -
Data Types: Ensure that the data types of the parameters in the query method correspond to the data types of the corresponding fields in the entity class.
-
NULL Values: If any of the fields (
house,sign,isRetro,lang) have the potential to benull, you might need to handle this in your query method. For instance, ifisRetroisnull, the query won’t match any records.