Search code examples
mysqlspring-boothibernateentity

Spring boot Error creating bean with name 'entityManagerFactory' defined in class path resource


I'm trying to do a 'spring-boot:run` and want to generate columns for all the five variables:

      @Entity
      Class Shopping{
            @Id
            @GeneratedValue
            private int id;
            private ShopifyInfo shop_info;
            private int product_count;
            private List<Products> products;
            private List<Variants> variants; 
    }

But I'm getting this error:

022-05-11 21:46:20.710 ERROR 25420 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.List, at table: shopify, for columns: [org.hibernate.mapping.Column(products)]
2022-05-11 21:46:20.710  WARN 25420 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.List, at table: shopify, for columns: [org.hibernate.mapping.Column(products)]
2022-05-11 21:46:20.710  INFO 25420 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2022-05-11 21:46:20.793  INFO 25420 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2022-05-11 21:46:20.796  INFO 25420 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-05-11 21:46:20.812  INFO 25420 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-05-11 21:46:20.945 ERROR 25420 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

Do I need to use mapping between the Entity class and product class. Need a suggestion on this?


Solution

  • Your comment:

    **Here I'm trying to do a "spring-boot:run" and want to generate columns for all above 
    five variables. But I'm getting this error :**
    

    indicates that you want to have columns with the ids of the products and variants in your shopping table, however what you probably want to do is have a join table.
    The error is telling you that your mapping is not understood, your use case should use a many to many relationship, and you will need a separate table to hold the foreign keys. Basically annotate the products and variants attributes with @ManyToMany and specify the join table, this is left as an exercise to you.
    Check https://www.baeldung.com/jpa-many-to-many for an example