我正在尝试创建一个简单的Spring启动应用程序,它将连接到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);
}
}
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
答案 0 :(得分:3)
您的堆栈跟踪可以解决问题。
引起:org.hibernate.AnnotationException:没有为实体指定标识符:hello.Account
在@Id
课程中切换Account
注释导入。
可能您正在使用: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}。