0

I'm confused with below errors i tried many stack overflow suggestions it did not worked.

And I am getting this error.

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]

Product Service class:

@Autowired
    DataSource database;

    @Autowired
    ProductRepository productRepository;


    @Transactional
    public product getAccounts(String account) {
    Product prd = productRepository.selectAccounts(accountId);

        return prd;
    }

Product Repository:

@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

/*@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;

I looked through a lot of similar questions, but it did not helped.Please help me to solve the above exception.

Varun
  • 196
  • 1
  • 20

3 Answers3

1
Not a managed type: class com.entities.product

Exception clearly tells that Product class is not a managed type here. It generally comes when Spring boot/JPA is not able to scan entities used in repositories. Annotate your main config class with @EntityScan and the exception should disappear e.g. @EntityScan("com.entities")

The documentation for EntityScan - https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/domain/EntityScan.html

Akash
  • 608
  • 5
  • 17
  • As Victor10 mentioned, did you uncomment @Entity and commented @Embeddable? – Akash Dec 27 '18 at 09:08
  • oh! Can you edit your question and post the configuration class and completed Product class if possible? – Akash Dec 27 '18 at 09:11
0

Make sure that:

  1. The ProductService class is annotated with @Service
  2. The ProductService class has an empty constructor(a constructor with no parameters) and that it has getters and setters for all its fields(if the fields are not public).
  3. The @Embeddableannotation is used when the annotated class is used as a field in another class. See here. The commented annotations in the Product class look good, uncomment them and remove @Embeddable.
Victor10
  • 96
  • 7
0

The reason I could see in your code you have commented the @Entity annotation which is basically used to state the JPA that this is entity class; And still you are trying to use Product as entity. The @Embeddable has different meaning its used to write the Composite primary Key for your entity.

Make sure the Product(an entity) class has an empty constructor(a constructor with no parameters) and that it has getters and setters for all its fields.

sham
  • 422
  • 4
  • 18