2

I am new to using spring boot. I am trying to create a REST API with apache derby as an embedded database but I get this error:

"Error creating bean with name 'entityManagerFactory'".

pom.xml :

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.restapi</groupId>
<artifactId>restApi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>restApi</name>
<description>Demo project for Rest API with spring boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Controller :

@RequestMapping(value="/topics")
public List<Topic> getTopics(){
    return topicService.getAllTopics();
}

@RequestMapping(value="/topics",method=RequestMethod.POST)
public void addTopic(@RequestBody Topic topic){
    topicService.addTopic(topic);
}

}

Business Service :

public List<Topic> getAllTopics(){
    List<Topic> topics = new ArrayList();
    topicRepository.findAll().forEach(topics::add);

    return topics;
}
public void addTopic(Topic topic){
    topicRepository.save(topic);
}

Entity Class Repository:

public interface TopicRepository extends CrudRepository<Topic, Long>{

}

Entity Class:

@Entity
public class Topic {
@Id
@GeneratedValue
private Long id;
private String name;
private String description;

public Topic(){
}

public Topic(Long id, String name, String description) {
    super();
    this.id = id;
    this.name = name;
    this.description = description;
}



public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

}

Application:

@SpringBootApplication
public class RestApiApplication {

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

}

Error:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.2.RELEASE)

2017-04-05 20:11:15.788  INFO 8756 --- [           main] com.restapi.RestApiApplicationTests      : Starting RestApiApplicationTests on DESKTOP-8C6TQN1 with PID 8756 (started by mahid in C:\Users\mahid\Documents\NetBeansProjects\restApi)
2017-04-05 20:11:15.788  INFO 8756 --- [           main] com.restapi.RestApiApplicationTests      : No active profile set, falling back to default profiles: default
2017-04-05 20:11:15.865  INFO 8756 --- [           main] o.s.w.c.s.GenericWebApplicationContext   : Refreshing org.springframework.web.context.support.GenericWebApplicationContext@702b8b12: startup date [Wed Apr 05 20:11:15 CDT 2017]; root of context hierarchy
2017-04-05 20:11:17.915  INFO 8756 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$c3650505] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-04-05 20:11:19.282  INFO 8756 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-04-05 20:11:19.315  INFO 8756 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2017-04-05 20:11:19.432  INFO 8756 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.12.Final}
2017-04-05 20:11:19.435  INFO 8756 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2017-04-05 20:11:19.439  INFO 8756 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2017-04-05 20:11:19.506  INFO 8756 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-04-05 20:11:19.777  INFO 8756 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.DerbyDialect
2017-04-05 20:11:19.784  WARN 8756 --- [           main] org.hibernate.dialect.DerbyDialect       : HHH000430: The DerbyDialect dialect has been deprecated; use one of the version-specific dialects instead
2017-04-05 20:11:20.476  WARN 8756 --- [           main] o.s.w.c.s.GenericWebApplicationContext   : 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/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
2017-04-05 20:11:20.491  INFO 8756 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-04-05 20:11:20.491 ERROR 8756 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at 

 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at 

  org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]

I didn't define anything in the application.properties.

Tal Avissar
  • 10,088
  • 6
  • 45
  • 70
jon
  • 197
  • 1
  • 3
  • 8
  • Try this link It may help http://stackoverflow.com/questions/40058001/error-creating-bean-with-name-entitymanagerfactory-defined-in-class-path-resou – Fady Saad Apr 06 '17 at 01:27
  • please configure the `application.properties spring boot need it to configure datasource and other underlying functions – Rajith Pemabandu Apr 06 '17 at 01:32
  • @RajithPemabandu If I am using an embedded database like derby do I still need to configure the application.properties file? – jon Apr 06 '17 at 01:38
  • "Spring Boot can auto-configure embedded H2, HSQL and Derby databases. You don’t need to provide any connection URLs, simply include a build dependency to the embedded database that you want to use." I found this on the spring documentation. – jon Apr 06 '17 at 02:23
  • @jon According to the latest post here https://github.com/spring-projects/spring-boot/issues/7706, it seems that the problem hasnt been resolved. What I did was to use h2 instead. – active92 Apr 06 '17 at 04:27
  • Running into the same issue, the code is taken from javabrains course https://javabrains.io/courses/spring_bootquickstart/lessons/Adding-Spring-Data-JPA/ – Joel Blum Oct 26 '19 at 15:56

3 Answers3

1

The exception clearly tell that you are mixing both hibernate as well as JPA libraries. Because of cross library importing and trying to run application gave you exception. You are trying to create Hibernate SessionFactory according to exception and you might be importing all JPA libraries for other. Make Sure you are importing all JPA libraries or all hibernate libraries. There shouldn't be cross importing of libraries. Please go through your code and make sure.

"org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]"

  • According to the last comment from the OP, the issue has been resolved (or at least: diagnosed and side stepped) – glytching Jul 25 '17 at 10:28
0

Step 1 -> use the updated version of parent artifact.

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
</parent>

Step 2 -> update your Maven project (ALT+F5)

TylerH
  • 20,799
  • 66
  • 75
  • 101
0

As you are referencing tutorial videos and I believe you are using Stereotype Annotations in your Business Service Class as @Service. Please configure the "application.properties", spring boot need it to configure datasource and other underlying functions. I've configured like below. Please make change to the configuration as per your requirement. I'm also using embedded derby database and got same exception. I solved by adding configuration in application.properties file.

#PROFILES
spring.profiles.active=dev
#JPA (JpaBaseConfiguration, HibernateJpaAutoConfiguration)
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database=default
spring.jpa.show-sql=true
#DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.continue-on-error=false
spring.datasource.generate-unique-name=false