我最近开始学习spring data jpa。
我直接遵循了一堆教程,但当它涉及到创建存储库时,我收到了bean异常。
我尝试在xml文件中使用注解和bean创建,但没有任何帮助。
下面是我最新的证明。
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);
}
例外
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'
1条答案
按热度按时间bxgwgixi1#
解决办法很简单。方法名称必须是
product findByProductId(long id);
,而不是product findByProduct_id(long id);