我对以下错误感到困惑,我尝试了许多堆栈溢出建议,但均未奏效。
我收到此错误。
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productService': Unsatisfied dependency expressed through field 'ProductRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ProductRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.entities.product
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.entities.product
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1694) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.entities.product
at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:473) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:74) ~[spring-data-jpa-2.0.11.RELEASE.jar:2.0.11.RELEASE]
产品服务类别:
@Autowired
DataSource database;
@Autowired
ProductRepository productRepository;
@Transactional
public product getAccounts(String account) {
Product prd = productRepository.selectAccounts(accountId);
return prd;
}
产品存储库:
@Repository
public interface productRepository extends JpaRepository<product, String> {
@Query(value="SELECT Demo FROM Test WHERE ACC_ID=?1,nativeQuery=true)
product selectAccounts(@Param("accountId") String accountId );
}
实体
/*@Entity(name="Product")
@Table(name="test", schema=demo")*/
@Embeddable
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name="ACCOUNT_ID")
private String accountId;
我浏览了很多类似的问题,但是并没有帮助。请帮助我解决上述异常。
答案 0 :(得分:0)
请确保:
@Service
注释@Embeddable
注释。参见here。 Product类中已注释的注释看起来不错,请取消注释并删除@Embeddable
。答案 1 :(得分:0)
Not a managed type: class com.entities.product
异常清楚地表明Product类不是此处的托管类型。通常在Spring Boot / JPA无法扫描存储库中使用的实体时出现。用@EntityScan
注释您的主配置类,该异常应消失,例如@EntityScan("com.entities")
EntityScan的文档-https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/domain/EntityScan.html
答案 2 :(得分:0)
我在您的代码中看到的原因是您注释了@Entity
批注,该批注基本上用于说明JPA这是实体类;而且您仍然尝试使用Product
作为实体。 @Embeddable
具有不同的含义,用于为您的实体编写复合主键。
确保Product(一个实体)类具有一个空的构造函数(一个没有参数的构造函数),并且其所有字段都具有getter和setter方法。