【问题标题】:Spring boot fails to start with HSQLDBSpring boot 无法启动 HSQLDB
【发布时间】:2015-10-23 10:45:26
【问题描述】:

我正在尝试创建一个简单的 Spring Boot 应用程序,该应用程序将连接到 HSQLDB 并使用 User 表,但是在尝试启动它时我得到了这个。

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

这里有整个控制台输出: http://pastebin.com/7HminjFL

我的文件是:

Application.java

@Configuration
@SpringBootApplication
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "hello")
@ComponentScan(basePackages = "hello")
@PropertySource({"classpath:application.properties"})

public class Application {

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

帐户.java

@Entity
@Table(name = "User", schema = "PUBLIC")
public class Account implements Serializable {

    @Id
    private Long id;

    @Column(name = "Login", nullable = false)
    private String login;

    @Column(name = "Password", nullable = false)
    private String password;


    protected Account() {
        // no-args constructor required by JPA spec
        // this one is protected since it shouldn't be used directly
    }

    public Account(String login, String password) {
        this.login = login;
        this.password = password;
    }

    public String getLogin() {
        return login;
    }

    public String getPassword() {
        return password;
    }

    public void setLogin(String login) {
        this.login = login;
        return;
    }

    public void setPassword(String password) {
        this.password = password;
        return;
    }
}

AccountRepository.java

public interface AccountRepository extends JpaRepository<Account, Long> {

    Long countByLogin(String login);
}

application.properties

spring.datasource.url=jdbc:hsqldb:file:C:\DB\TestDB
spring.datasource.username=SA
spring.datasource.password=
spring.datasource.driver-class-name=org.hsqldb.jdbcDriver

【问题讨论】:

  • 您正在使用 Spring Boot,然后使用您正在努力不使用的 Spring Boot。将您的 Application 类移动到 hello 包中,删除除 SpringBootApplication 注释之外的所有注释。然后再次启动应用程序,如果仍然失败,请将堆栈跟踪添加到您的问题中。
  • 我已经删除了注释(我只是在尝试解决这个问题时才添加的)并且同样的错误仍然存​​在,我已经将整个控制台输出粘贴到了 pastebin,因为它很大。

标签: java spring spring-boot hsqldb


【解决方案1】:

您的堆栈跟踪为问题指明了方向。

原因:org.hibernate.AnnotationException:没有为实体指定标识符:hello.Account

Account 类上切换您的 @Id 注释导入。

您可能正在使用:import org.springframework.data.annotation.Id。换成import javax.persistence.Id 并尝试重新启动您的应用程序;


顺便说一下,@SpringBootApplication 是启动 SpringBoot 应用程序的便捷方式。如果使用,则无需添加@Configuration、@EnableAutoConfiguration 和@ComponentScan

@SpringBootApplication

表示声明一个或多个的 {@link Configuration configuration} 类 {@link Bean @Bean} 方法并触发 {@link EnableAutoConfiguration 自动配置} 和 {@link ComponentScan 组件扫描}。

这是一个方便 相当于声明 {@code @Configuration} 的注解, {@code @EnableAutoConfiguration} 和 {@code @ComponentScan}。

【讨论】:

    猜你喜欢
    • 2018-11-09
    • 2019-06-14
    • 2013-09-25
    • 2016-10-22
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2020-03-24
    相关资源
    最近更新 更多