javaspring-bootspring-data-jpajparepository

Find a record by Column value is not working using JpaRepository in Spring Boot


I want to fetch a record by its itemcode value. I'm getting null value.

Entity

@Data
@Entity
public class Stock {    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id; 
    private String  itemcode;
    private Integer mrp;
}

Repository

public interface StockRepository extends JpaRepository<Stock, Long> {   
    public Stock findByItemcode(String itemcode);
}
public void getStockByItemcode(String itemcode) {   
    this.stockRepository.findByItemcode(itemcode);      
}

Solution

  • If, as you seem to say in the comments, this works with a literal but does not work with a variable containing the same value, then the two actually do not contain the same value.

    The difference could be whitespace (space characters, line feed, carriage return, tab) or non printable characters (ctrl-some-letter) or possibly different encodings of a String. For example the leter Ü may be created as an actual Ü or by (U plus ¨) in Unicode.

    Use a debugger to examine the value of the variable and to compare it to the literal value.