【问题标题】:Spring-boot error using Apache Derby as embedded database使用 Apache Derby 作为嵌入式数据库的 Spring-boot 错误
【发布时间】:2017-08-31 19:56:04
【问题描述】:

我是使用弹簧靴的新手。 我正在尝试使用 apache derby 作为嵌入式数据库创建 REST API,但出现此错误:

“创建名为 'entityManagerFactory' 的 bean 时出错”。

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>

控制器:

@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);
}

}

商务服务:

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

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

实体类存储库:

public interface TopicRepository extends CrudRepository<Topic, Long>{

}

实体类:

@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;
}

}

应用:

@SpringBootApplication
public class RestApiApplication {

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

}

错误:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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]

我没有在 application.properties 中定义任何东西。

【问题讨论】:

  • 试试这个链接它可能会有所帮助stackoverflow.com/questions/40058001/…
  • 请配置`application.properties spring boot需要它来配置datasource和其他底层功能
  • @RajithPemabandu 如果我使用像 derby 这样的嵌入式数据库,是否还需要配置 application.properties 文件?
  • "Spring Boot 可以自动配置嵌入式 H2、HSQL 和 Derby 数据库。您无需提供任何连接 URL,只需包含对您要使用的嵌入式数据库的构建依赖项。"我在 spring 文档中找到了这个。
  • @jon 根据github.com/spring-projects/spring-boot/issues/7706这里的最新帖子,看来问题还没有解决。我所做的是改用 h2。

标签: java maven spring-boot spring-data spring-data-jpa


【解决方案1】:

异常清楚地表明您正在混合使用 hibernate 和 JPA 库。由于跨库导入和尝试运行应用程序给了你异常。您正在尝试根据异常创建 Hibernate SessionFactory,并且您可能正在为其他导入所有 JPA 库。 确保您正在导入所有 JPA 库或所有休眠库。 不应该交叉导入库。 请检查您的代码并确保。

“org.springframework.beans.factory.BeanCreationException:在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]中定义名称为“entityManagerFactory”的bean创建错误:调用init方法失败; 嵌套异常是 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]"

【讨论】:

  • 根据 OP 的最后评论,问题已解决(或至少:已诊断和侧步)
【解决方案2】:

第 1 步 -> 使用更新版本的父工件。

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

第 2 步 -> 更新您的 Maven 项目 (ALT+F5)

【讨论】:

    【解决方案3】:

    当您参考教程视频时,我相信您在业务服务类中使用 Stereotype Annotations 作为@Service。 请配置“application.properties”,spring boot需要它来配置datasource等底层功能。 我已经配置如下。请根据您的要求更改配置。我也在使用嵌入式 derby 数据库并且遇到了同样的异常。我通过在 application.properties 文件中添加配置来解决。

    #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
    

    【讨论】:

      猜你喜欢
      • 2020-09-18
      • 1970-01-01
      • 2021-10-11
      • 2016-04-06
      • 1970-01-01
      • 2012-02-04
      • 2015-10-19
      • 1970-01-01
      • 2019-09-04
      相关资源
      最近更新 更多