Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
I need to get the product repository (where i added a query to fetch productname with I'd because we can't fetch from transaction table ) calling that method into transaction controller response(so that i can show productname in my response) can anyone suggest how should I get it

What I have tried:

I tried declaring method in product by getproductnme and returning it in entity class

Product repo
package com.retailsols.gasstation.repo;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.retailsols.gasstation.entity.ProductType;

public interface ProductTypeRepository extends JpaRepository<producttype, string=""> {

      @Query("SELECT p.productName FROM ProductType p WHERE p.productId =:productId")
      String getproductName(@Param("productId") String productId);



Entity class:(i need to getproductName it is showing null)
public String getProductName(ProductTypeRepository productRepo) {
        if (productId != null) {
            return productRepository.getproductName(productId);
        return null;
Updated 11-Jun-23 1:13am
Andre Oosthuizen 10-Jun-23 6:08am    
Maybe you should post your method code that you tried and show us where it does not work for better understanding. Click on 'Improve Question' and give us more information so that we can understand where you are stuck.
minnon dumbo 18-Jun-23 2:08am    
Can u please help me out with the other please
minnon dumbo 12-Jun-23 5:36am    
Thank you

1 solution

1. From what I can see, in your repository your method name is defined as 'getproductName', but in your entity class, you are using 'getProductName', the naming must be identical, update your repository method to -
String getProductName(@Param("productId") String productId);

2. In your entity class, you are injecting the 'ProductTypeRepository' trying to call the 'getproductName' method. This is not the correct way to use a repository in a service or entity class. Repositories should be used in service classes or controllers. You can creat a service class 'ProductService' that uses the 'ProductTypeRepository' to retrieve the product name. You can then inject the 'ProductServic'e into your transaction controller and call the 'getProductName' method to include the product name -
public class ProductService {

    private final ProductTypeRepository productRepository;

    public ProductService(ProductTypeRepository productRepository) {
        this.productRepository = productRepository;

    public String getProductName(String productId) {
        if (productId != null) {
            return productRepository.getProductName(productId);
        return null;

3. Update your transaction controller to include the 'ProductService' -
public class TransactionController {

    private final ProductService productService;

    public TransactionController(ProductService productService) {
        this.productService = productService;

    public ResponseEntity<Transaction> getTransaction(@PathVariable("transactionId") String transactionId) {
        // Your code to fetch the transaction
        Transaction transaction = ...;

        // Get the product name using the ProductService
        String productName = productService.getProductName(transaction.getProductId());

        // Set the product name in the transaction object

        // Return the transaction in the response
        return ResponseEntity.ok(transaction);

The above code is based on what you gave in your code, you need to adjsut it to run error free depending on the rest of your code.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900