Spring 引导无法使用 HSQLDB 启动

Spring boot fails to start with HSQLDB

我正在尝试创建一个简单的 spring 启动应用程序,它将连接到 HSQLDB 并与用户 table 一起工作,但是我在尝试启动它时遇到了这个问题。

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

Account.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

您的堆栈跟踪为问题提供了一些方向。

Caused by: org.hibernate.AnnotationException: No identifier specified for entity: hello.Account

Account class 上切换你的 @Id 注释导入。

可能您正在使用:import org.springframework.data.annotation.Id。换成import javax.persistence.Id,尝试重新启动你的申请;


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

@SpringBootApplication

Indicates a {@link Configuration configuration} class that declares one or more {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration auto-configuration} and {@link ComponentScan component scanning}.

This is a convenience annotation that is equivalent to declaring {@code @Configuration}, {@code @EnableAutoConfiguration} and {@code @ComponentScan}.