javaspringspring-data-jpaspring-bean

JpaRepository keeps throwing exceptions


I recently stared learning spring data jpa.

I was directly following a bunch of tutorials but whan it comes to create repository I receive bean exceptions.

I tried using annotations and bean creating in xml file but nothing helped.

Beneath is my latest attepmt.

package com.convential.mainclasspackage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringDataJpaConventialApplication {

    public static void main(String[] args) {
        
        SpringApplication.run(SpringDataJpaConventialApplication.class, args);
    }

    @Bean
    CommandLineRunner commandlineRunner(ProductRepository productRepository) {
        
        return (args) -> {
            product tv = new product("TV", 250);
            productRepository.save(tv);
        };
        
    } 
}
package com.convential.mainclasspackage;

import jakarta.annotation.Nullable;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;

@Entity(name = "product")
@Table(

        name = "product", uniqueConstraints = {
                @UniqueConstraint(name = "product_name_unique_constraint", columnNames = "product_name") }

)
public class product {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int productId;

    @Column(name = "product_name", nullable = true, unique = true, columnDefinition = "TEXT"

    )
    private String productName;

    @Column(name = "product_quantity", nullable = true, updatable = true)
    private int productQuantity;

    public product() {
    }

    public product(String productName, int productQuantity) {
        this.productName = productName;
        this.productQuantity = productQuantity;
    }

    public product(int productId, String productName, int productQuantity) {
        this.productId = productId;
        this.productName = productName;
        this.productQuantity = productQuantity;
    }

    public int getProductId() {
        return productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getProductQuantity() {
        return productQuantity;
    }

    public void setProductQuantity(int productQuantity) {
        this.productQuantity = productQuantity;
    }

    @Override
    public String toString() {
        return "Product [productId=" + productId + ", productName=" + productName + ", productQuantity="
                + productQuantity + "]";
    }

}




package com.convential.mainclasspackage;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

public interface ProductRepository extends JpaRepository<product, Integer> {

    List<product> findByName();
    product findByProduct_id(long id);
    
    
}

Exception

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'commandlineRunner' defined in com.convential.mainclasspackage.SpringDataJpaConventialApplication: Unsatisfied dependency expressed through method 'commandlineRunner' parameter 0: Error creating bean with name 'productRepository' defined in com.convential.mainclasspackage.ProductRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Could not create query for public abstract com.convential.mainclasspackage.product com.convential.mainclasspackage.ProductRepository.findByProduct_id(long); Reason: Failed to create query for method public abstract com.convential.mainclasspackage.product com.convential.mainclasspackage.ProductRepository.findByProduct_id(long); No property 'product' found for type 'product'; Did you mean 'productId'

Solution

  • The solution is simple. The method name must be product findByProductId(long id); not product findByProduct_id(long id);