Home > Software design >  Hibernate GetbyID : java.lang.NoSuchMethodException Entity error
Hibernate GetbyID : java.lang.NoSuchMethodException Entity error

Time:01-19

Hibernate and trying to build simple feature where we can search Product by Id. Hibernate has inbuit function to search an entity by its id. I tried the same but i am getting "java.lang.NoSuchMethodException" .

MyController.java :

 @GetMapping(value = "/getProducts/{id}" , produces ="application/json")
    public ResponseEntity<Product> display(@PathVariable int id) {
        Product products = productServiceImp.getAllProducts(id);
        return ResponseEntity.ok(products);

MyProductServiceImp:

@Override
    public Product getAllProducts(int product_id ) {
        return productRepository.getById(product_id );
    }

MyProductRepository:
@Repository
public interface ProductRepository extends JpaRepository<Product, Integer> {
}

Schema of Product table : (product_id, desciption,display_name, qty, amount)

When i try to invoke API by postman curl --location --request GET 'http://localhost:8080/admin/getProducts/1. I see it is Caused by: java.lang.NoSuchMethodException: com.Project.OrderProcessing.OrderProcessing.Entity.Product$HibernateProxy$zAdAYVvM.<init>().I am unable to understand reason behind it

CodePudding user response:

Try findById since getById is deprecated. Untested, but something like:

MyProductServiceImp:

@Override
public Optional<Product> findById(Integer productId) {
    return productRepository.findById(productId);
}

Product.java

@Entity //make sure this is present
@Getter //from Lombok
@Setter //from Lombok
public class Product {

@Id //need this
@GeneratedValue //need this to auto-generate
private Integer id;

private String description;

//the rest: displayName, quantity, amount...

}

Your @Repository interface looks fine. There are different variations for your controller depending on what you need to do. But for now, just try calling your service method so you know you get the result back from the DB and work from there.

Camel-case your variables in general for consistency. Then you can use the Spring conventions in interfaces for repositories so your method could look like findAllByDisplayName() instead of findAllByDisplay_Name() and Spring will handle the query for you.

Also note that presumably, you're not getting all products with one product ID, right? So it should just be called getProduct or findProduct or getProductById or findProductById.

CodePudding user response:

MyControllerClass:
@RequestMapping("/admin")
@RestController
public class ProductController {
 @GetMapping(value = "/getProducts/{id}" , produces ="application/json")
    public Optional<Product> display(@PathVariable int id) {
        Optional<Product> products = productServiceImp.getProductDetailsbyID(id);
        return products;
}
}
MyProductServiceImp :
@Override
    public Optional<Product> getProductDetailsbyID(int product_id ) {
        Optional<Product> prodresult=productRepository.findById(product_id);
        return prodresult;
    }

I have used FindbyID in place of GetById and it worked !!

  • Related