我正在绑定一个示例代码,用jpa运行我的springboot数据项目,用于H2 DB。
代码如下,应用程序正常运行,但我没有看到任何表创建,我也没有在服务器控制台中看到任何错误。我检查了日志,我没有看到任何查询被创建一个被解雇。我做错了吗?
域类:
@Entity
public class Account {
@Id
@GeneratedValue
private Integer accountId;
private String name;
private String accType;
public Integer getAccountId() {
return accountId;
}
public String getName() {
return name;
}
public String getAccType() {
return accType;
}
public Account(Integer accountId, String name, String accType) {
super();
this.accountId = accountId;
this.name = name;
this.accType = accType;
}
public Account(String name, String accType) {
super();
this.name = name;
this.accType = accType;
}
public Account() {
}
Data Loader类:
@Component
public class AccountLoaded {
private AccountRepository accountRepository;
public AccountLoaded(AccountRepository accountRepository) {
this.accountRepository = accountRepository;
}
@PostConstruct
private void loadData() {
Account account = new Account(1,"Madhu","Savings");
accountRepository.save(account);
System.out.println("Loaded Account " + account.toString());
}
}
存储库类:
@Repository
public interface AccountRepository extends CrudRepository<Account, Integer> {
}
application.properties:
spring.datasource.jdbc-url=jdbc:h2:mem:test
spring.h2.console.enabled=true
spring.h2.console.path=/console
spring.datasource.platform=h2
我的日志在服务器控制台中:
2016-10-11 12:38:26.202 INFO 20020 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2016-10-11 12:38:26.217 INFO 20020 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2016-10-11 12:38:26.285 INFO 20020 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.0.11.Final}
2016-10-11 12:38:26.287 INFO 20020 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2016-10-11 12:38:26.288 INFO 20020 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2016-10-11 12:38:26.327 INFO 20020 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2016-10-11 12:38:26.554 INFO 20020 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2016-10-11 12:38:26.995 INFO 20020 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export
2016-10-11 12:38:27.004 INFO 20020 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export complete
2016-10-11 12:38:27.044 INFO 20020 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
Loaded Account Account [accountId=1, name=Madhu, accType=Savings]
2016-10-11 12:38:27.587 INFO 20020 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@b472aa: startup date [Tue Oct 11 12:38:23 CDT 2016]; root of context hierarchy
2016-10-11 12:38:27.663 INFO 20020 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-10-11 12:38:27.664 INFO 20020 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-10-11 12:38:27.696 INFO 20020 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-10-11 12:38:27.696 INFO 20020 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-10-11 12:38:27.732 INFO 20020 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-10-11 12:38:27.959 INFO 20020 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-10-11 12:38:28.011 INFO 20020 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-10-11 12:38:28.015 INFO 20020 --- [ main] com.example.SpringdataApplication : Started SpringdataApplication in 4.898 seconds (JVM running for 5.255)
答案 0 :(得分:0)
将spring.datasource.jdbc-url=jdbc:h2:mem:test
更改为spring.datasource.jdbc-url=jdbc:h2:file:test
(test是db文件的名称,也可以包含名称的路径),并使用these tools之一查看数据库。
您还可以通过将spring.jpa.show-sql: true
添加到您的application.properties
答案 1 :(得分:0)
即使所有内存都在内存中完成,当spring.jpa.show-sql设置为true时(在application.properties文件中),您应该看到SQL日志。
不要忘记将“spring.jpa.generate-ddl”设置为true(默认为false)并使loadData方法为事务性(请参阅Spring @Transactional注释)。
答案 2 :(得分:0)
如果你去嵌入式h2控制台(http://localhost:8080/console),并输入正确的连接URL(jdbc:h2:mem:test),你应该看到的东西。
默认情况下,嵌入式h2控制台没有显示正确的连接URL,它忘记了:mem:part IIRC。