Search code examples
mysqlspring-bootspring-repositories

Searching in MySQL


I'am Trying to make a function in my Repository of Spring Boot to search for the elements who's check the condition condition :

@Query(value = "select *from `trash_can_response` WHERE temperature=:'21' OR distance=:'24' OR humidity=:'82' ", nativeQuery = true)
    public TrashCanResponse notif(); 

this is my data example :

{
    "id": 42,
    "humidity": 82,
    "distance": 23,
    "temperature": 21
  },
  {
    "id": 43,
    "humidity": 82,
    "distance": 23,
    "temperature": 21
  },
  {
    "id": 44,
    "humidity": 82,
    "distance": 23,
    "temperature": 21
  },
  {
    "id": 45,
    "humidity": 82,
    "distance": 24,
    "temperature": 21
  },
  {
    "id": 46,
    "humidity": 82,
    "distance": 23,
    "temperature": 21
  },
  {
    "id": 47,
    "humidity": 82,
    "distance": 23,
    "temperature": 21
  },

  {
    "id": 50,
    "humidity": 82,
    "distance": 23,
    "temperature": 21
  },
  {
    "id": 51,
    "humidity": 82,
    "distance": 23,
    "temperature": 21
  },
 
  {
    "id": 54,
    "humidity": 82,
    "distance": 23,
    "temperature": 21
  },

i want to the function notif() to return who get temperature=21 OR humidity =21 OR distance =24 ;

i know my Query code is wrong , can you correct it for me ?


iam using JPA :

@Entity
@Table(name = "TrashCanResponse")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = { "createdAt", "updatedAt" }, allowGetters = true)

@EnableJpaAuditing
public class TrashCanResponse {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank
    private Integer humidity;
    
    @Column(unique=true)
    @NotBlank
    private Integer distance;

    @NotBlank
    private Integer temperature;

   

    @Column(updatable = false)
    @CreationTimestamp
    private LocalDateTime createdAt;
    @UpdateTimestamp
    private LocalDateTime updatedAt;

    

    public TrashCanResponse() {
    }

    public Integer getTemperature() {
        return temperature;
    }

    public void setTemperature(Integer temperature) {
        this.temperature = temperature;
    }

    public TrashCanResponse(Integer temperature, Integer humidity, Integer distance) {
             this.setTemperature(temperature);
            this.distance=distance;
            this.humidity=humidity;
            
    }

        // Getters and Setters

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }



    public Integer getHumidity() {
        return humidity;
    }

    public void setHumidity(Integer humidity) {
        this.humidity = humidity;
    }

    public Integer getDistance() {
        return distance;
    }

    public void setDistance(Integer distance) {
        this.distance = distance;
    }

  

 
    
}

all things is good just i want to searsh for a specific rows who's check my conditions because iam bad in query ..


Solution

  • Your method should have some return type.
    You can use the @Query as shown below without nativeQuery.

    @Query(value = "select TCR from TrashCanResponse TCR WHERE TCR .temperature=21 OR TCR .distance=24 OR TCR .humidity=82 ")
    public List<TrashCanResponse> TrashCanResponse notif();