Cannot apply where condition clause in JPA query





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I wrote a Query in my ProductRepository as below



@Query(value = "select p from Product p where p.code = :code")
Product findByCode(@Param("code")String code);


it's obviously that I just want the query return 1 Product, but some how when I call the API it always return a list of all my products.



this is the generated query I get from the log



Hibernate: select product0_.code as code1_4_, product0_.name as name2_4_, product0_.price as price3_4_, product0_.unit as unit4_4_ from product product0_


I cannot see the WHERE condition clause that I wrote



This is my Product class:



package com.example.model;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;
import java.util.List;
import java.util.Objects;

@Entity
public class Product {
@Id
private String code;
private String name;
private Float price;
private String unit;
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY, mappedBy = "product", cascade
=CascadeType.ALL)
private List<OrderLine> orderLines;
public Product(String code, String name, float price, String unit) {
this.code = code;
this.name = name;
this.price = price;
this.unit = unit;
}


public Product(String code, float price, String unit) {
this.code = code;
this.price = price;
this.unit = unit;
}

public Product(String code) {
this.code = code;
this.price = Float.valueOf(0);
}

public Product(String code, String unit) {
this.code = code;
this.unit = unit;
this.price = Float.valueOf(0);
}

public Product() {}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


public Float getPrice() {
return price;
}

public void setPrice(float price) {
this.price = price;
}

public String getUnit() {
return unit;
}

public void setUnit(String unit) {
this.unit = unit;
}

@Override
public String toString() {
return "Product{" +
"code='" + code + ''' +
", name='" + name + ''' +
", price=" + price +
", unit='" + unit + ''' +
'}';
}

public List<OrderLine> getOrderLines() {
return orderLines;
}

public void setOrderLines(List<OrderLine> orderLines) {
this.orderLines = orderLines;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Product)) return false;
Product product = (Product) o;
return this.getCode().equals(product.getCode());
}

@Override
public int hashCode() {
return Objects.hash(getCode());
}}


My controller



@RestController
@RequestMapping("/products")
public class ProductController {

@Autowired
ProductRepo productRepo;
@Autowired
StaffRepo staffRepo;
@Autowired
InventoryRepo inventoryRepo;
@GetMapping("/get")
public ResponseEntity getAll(){
List<Product> products = productRepo.findAll();
if(products== null){
return ResponseEntity.status(404).body("Product not found !");
}
return ResponseEntity.ok().body(products);
}
@GetMapping("/get/{code}")
public ResponseEntity getProductByCode(@PathVariable String code){
Product p = productRepo.findByCode(code);
if(p== null){
return ResponseEntity.status(404).body("Product not found !");
}
return ResponseEntity.ok().body(p);
}









share|improve this question




















  • 1





    You're not calling that method, or you're calling it but are looking at the logs of another method. A method with return type Product can't possibly return a list of products. That would cause an exception. BTW, since code is the ID of the entity, it would be much simpler and more efficient to simply call findById().

    – JB Nizet
    Nov 24 '18 at 0:07













  • you're right, it seems I'm not calling the right method. this is the URL (localhost:8080/products/get/?code=AMM) and I just add my Controller on the post

    – Hector
    Nov 24 '18 at 0:22













  • The URL for the method getting a single product by code is /products/get/AMM.

    – JB Nizet
    Nov 24 '18 at 0:27


















0















I wrote a Query in my ProductRepository as below



@Query(value = "select p from Product p where p.code = :code")
Product findByCode(@Param("code")String code);


it's obviously that I just want the query return 1 Product, but some how when I call the API it always return a list of all my products.



this is the generated query I get from the log



Hibernate: select product0_.code as code1_4_, product0_.name as name2_4_, product0_.price as price3_4_, product0_.unit as unit4_4_ from product product0_


I cannot see the WHERE condition clause that I wrote



This is my Product class:



package com.example.model;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;
import java.util.List;
import java.util.Objects;

@Entity
public class Product {
@Id
private String code;
private String name;
private Float price;
private String unit;
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY, mappedBy = "product", cascade
=CascadeType.ALL)
private List<OrderLine> orderLines;
public Product(String code, String name, float price, String unit) {
this.code = code;
this.name = name;
this.price = price;
this.unit = unit;
}


public Product(String code, float price, String unit) {
this.code = code;
this.price = price;
this.unit = unit;
}

public Product(String code) {
this.code = code;
this.price = Float.valueOf(0);
}

public Product(String code, String unit) {
this.code = code;
this.unit = unit;
this.price = Float.valueOf(0);
}

public Product() {}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


public Float getPrice() {
return price;
}

public void setPrice(float price) {
this.price = price;
}

public String getUnit() {
return unit;
}

public void setUnit(String unit) {
this.unit = unit;
}

@Override
public String toString() {
return "Product{" +
"code='" + code + ''' +
", name='" + name + ''' +
", price=" + price +
", unit='" + unit + ''' +
'}';
}

public List<OrderLine> getOrderLines() {
return orderLines;
}

public void setOrderLines(List<OrderLine> orderLines) {
this.orderLines = orderLines;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Product)) return false;
Product product = (Product) o;
return this.getCode().equals(product.getCode());
}

@Override
public int hashCode() {
return Objects.hash(getCode());
}}


My controller



@RestController
@RequestMapping("/products")
public class ProductController {

@Autowired
ProductRepo productRepo;
@Autowired
StaffRepo staffRepo;
@Autowired
InventoryRepo inventoryRepo;
@GetMapping("/get")
public ResponseEntity getAll(){
List<Product> products = productRepo.findAll();
if(products== null){
return ResponseEntity.status(404).body("Product not found !");
}
return ResponseEntity.ok().body(products);
}
@GetMapping("/get/{code}")
public ResponseEntity getProductByCode(@PathVariable String code){
Product p = productRepo.findByCode(code);
if(p== null){
return ResponseEntity.status(404).body("Product not found !");
}
return ResponseEntity.ok().body(p);
}









share|improve this question




















  • 1





    You're not calling that method, or you're calling it but are looking at the logs of another method. A method with return type Product can't possibly return a list of products. That would cause an exception. BTW, since code is the ID of the entity, it would be much simpler and more efficient to simply call findById().

    – JB Nizet
    Nov 24 '18 at 0:07













  • you're right, it seems I'm not calling the right method. this is the URL (localhost:8080/products/get/?code=AMM) and I just add my Controller on the post

    – Hector
    Nov 24 '18 at 0:22













  • The URL for the method getting a single product by code is /products/get/AMM.

    – JB Nizet
    Nov 24 '18 at 0:27














0












0








0








I wrote a Query in my ProductRepository as below



@Query(value = "select p from Product p where p.code = :code")
Product findByCode(@Param("code")String code);


it's obviously that I just want the query return 1 Product, but some how when I call the API it always return a list of all my products.



this is the generated query I get from the log



Hibernate: select product0_.code as code1_4_, product0_.name as name2_4_, product0_.price as price3_4_, product0_.unit as unit4_4_ from product product0_


I cannot see the WHERE condition clause that I wrote



This is my Product class:



package com.example.model;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;
import java.util.List;
import java.util.Objects;

@Entity
public class Product {
@Id
private String code;
private String name;
private Float price;
private String unit;
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY, mappedBy = "product", cascade
=CascadeType.ALL)
private List<OrderLine> orderLines;
public Product(String code, String name, float price, String unit) {
this.code = code;
this.name = name;
this.price = price;
this.unit = unit;
}


public Product(String code, float price, String unit) {
this.code = code;
this.price = price;
this.unit = unit;
}

public Product(String code) {
this.code = code;
this.price = Float.valueOf(0);
}

public Product(String code, String unit) {
this.code = code;
this.unit = unit;
this.price = Float.valueOf(0);
}

public Product() {}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


public Float getPrice() {
return price;
}

public void setPrice(float price) {
this.price = price;
}

public String getUnit() {
return unit;
}

public void setUnit(String unit) {
this.unit = unit;
}

@Override
public String toString() {
return "Product{" +
"code='" + code + ''' +
", name='" + name + ''' +
", price=" + price +
", unit='" + unit + ''' +
'}';
}

public List<OrderLine> getOrderLines() {
return orderLines;
}

public void setOrderLines(List<OrderLine> orderLines) {
this.orderLines = orderLines;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Product)) return false;
Product product = (Product) o;
return this.getCode().equals(product.getCode());
}

@Override
public int hashCode() {
return Objects.hash(getCode());
}}


My controller



@RestController
@RequestMapping("/products")
public class ProductController {

@Autowired
ProductRepo productRepo;
@Autowired
StaffRepo staffRepo;
@Autowired
InventoryRepo inventoryRepo;
@GetMapping("/get")
public ResponseEntity getAll(){
List<Product> products = productRepo.findAll();
if(products== null){
return ResponseEntity.status(404).body("Product not found !");
}
return ResponseEntity.ok().body(products);
}
@GetMapping("/get/{code}")
public ResponseEntity getProductByCode(@PathVariable String code){
Product p = productRepo.findByCode(code);
if(p== null){
return ResponseEntity.status(404).body("Product not found !");
}
return ResponseEntity.ok().body(p);
}









share|improve this question
















I wrote a Query in my ProductRepository as below



@Query(value = "select p from Product p where p.code = :code")
Product findByCode(@Param("code")String code);


it's obviously that I just want the query return 1 Product, but some how when I call the API it always return a list of all my products.



this is the generated query I get from the log



Hibernate: select product0_.code as code1_4_, product0_.name as name2_4_, product0_.price as price3_4_, product0_.unit as unit4_4_ from product product0_


I cannot see the WHERE condition clause that I wrote



This is my Product class:



package com.example.model;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;
import java.util.List;
import java.util.Objects;

@Entity
public class Product {
@Id
private String code;
private String name;
private Float price;
private String unit;
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY, mappedBy = "product", cascade
=CascadeType.ALL)
private List<OrderLine> orderLines;
public Product(String code, String name, float price, String unit) {
this.code = code;
this.name = name;
this.price = price;
this.unit = unit;
}


public Product(String code, float price, String unit) {
this.code = code;
this.price = price;
this.unit = unit;
}

public Product(String code) {
this.code = code;
this.price = Float.valueOf(0);
}

public Product(String code, String unit) {
this.code = code;
this.unit = unit;
this.price = Float.valueOf(0);
}

public Product() {}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


public Float getPrice() {
return price;
}

public void setPrice(float price) {
this.price = price;
}

public String getUnit() {
return unit;
}

public void setUnit(String unit) {
this.unit = unit;
}

@Override
public String toString() {
return "Product{" +
"code='" + code + ''' +
", name='" + name + ''' +
", price=" + price +
", unit='" + unit + ''' +
'}';
}

public List<OrderLine> getOrderLines() {
return orderLines;
}

public void setOrderLines(List<OrderLine> orderLines) {
this.orderLines = orderLines;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Product)) return false;
Product product = (Product) o;
return this.getCode().equals(product.getCode());
}

@Override
public int hashCode() {
return Objects.hash(getCode());
}}


My controller



@RestController
@RequestMapping("/products")
public class ProductController {

@Autowired
ProductRepo productRepo;
@Autowired
StaffRepo staffRepo;
@Autowired
InventoryRepo inventoryRepo;
@GetMapping("/get")
public ResponseEntity getAll(){
List<Product> products = productRepo.findAll();
if(products== null){
return ResponseEntity.status(404).body("Product not found !");
}
return ResponseEntity.ok().body(products);
}
@GetMapping("/get/{code}")
public ResponseEntity getProductByCode(@PathVariable String code){
Product p = productRepo.findByCode(code);
if(p== null){
return ResponseEntity.status(404).body("Product not found !");
}
return ResponseEntity.ok().body(p);
}






java hibernate spring-boot jpa spring-data-jpa






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 0:25







Hector

















asked Nov 24 '18 at 0:04









HectorHector

11




11








  • 1





    You're not calling that method, or you're calling it but are looking at the logs of another method. A method with return type Product can't possibly return a list of products. That would cause an exception. BTW, since code is the ID of the entity, it would be much simpler and more efficient to simply call findById().

    – JB Nizet
    Nov 24 '18 at 0:07













  • you're right, it seems I'm not calling the right method. this is the URL (localhost:8080/products/get/?code=AMM) and I just add my Controller on the post

    – Hector
    Nov 24 '18 at 0:22













  • The URL for the method getting a single product by code is /products/get/AMM.

    – JB Nizet
    Nov 24 '18 at 0:27














  • 1





    You're not calling that method, or you're calling it but are looking at the logs of another method. A method with return type Product can't possibly return a list of products. That would cause an exception. BTW, since code is the ID of the entity, it would be much simpler and more efficient to simply call findById().

    – JB Nizet
    Nov 24 '18 at 0:07













  • you're right, it seems I'm not calling the right method. this is the URL (localhost:8080/products/get/?code=AMM) and I just add my Controller on the post

    – Hector
    Nov 24 '18 at 0:22













  • The URL for the method getting a single product by code is /products/get/AMM.

    – JB Nizet
    Nov 24 '18 at 0:27








1




1





You're not calling that method, or you're calling it but are looking at the logs of another method. A method with return type Product can't possibly return a list of products. That would cause an exception. BTW, since code is the ID of the entity, it would be much simpler and more efficient to simply call findById().

– JB Nizet
Nov 24 '18 at 0:07







You're not calling that method, or you're calling it but are looking at the logs of another method. A method with return type Product can't possibly return a list of products. That would cause an exception. BTW, since code is the ID of the entity, it would be much simpler and more efficient to simply call findById().

– JB Nizet
Nov 24 '18 at 0:07















you're right, it seems I'm not calling the right method. this is the URL (localhost:8080/products/get/?code=AMM) and I just add my Controller on the post

– Hector
Nov 24 '18 at 0:22







you're right, it seems I'm not calling the right method. this is the URL (localhost:8080/products/get/?code=AMM) and I just add my Controller on the post

– Hector
Nov 24 '18 at 0:22















The URL for the method getting a single product by code is /products/get/AMM.

– JB Nizet
Nov 24 '18 at 0:27





The URL for the method getting a single product by code is /products/get/AMM.

– JB Nizet
Nov 24 '18 at 0:27












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454070%2fcannot-apply-where-condition-clause-in-jpa-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454070%2fcannot-apply-where-condition-clause-in-jpa-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini