我有一个连接到postgresql数据库的Spring Boot项目,看起来Hibernate并没有像使用h2数据库那样自动创建表。在启动时,我的应用程序会吐出以下堆栈跟踪:
2017-03-02T01:18:46.415146+00:00 app[web.1]: java.lang.IllegalStateException: Failed to execute ApplicationRunner
2017-03-02T01:18:46.415147+00:00 app[web.1]: at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:794) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
2017-03-02T01:18:46.415148+00:00 app[web.1]: at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:781) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
2017-03-02T01:18:46.415149+00:00 app[web.1]: at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:771) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
2017-03-02T01:18:46.415150+00:00 app[web.1]: at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
2017-03-02T01:18:46.415150+00:00 app[web.1]: at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
2017-03-02T01:18:46.415151+00:00 app[web.1]: at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
2017-03-02T01:18:46.415152+00:00 app[web.1]: at com.recipes.Application.main(Application.java:10) [classes!/:na]
2017-03-02T01:18:46.415152+00:00 app[web.1]: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121-cedar14]
2017-03-02T01:18:46.415153+00:00 app[web.1]: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121-cedar14]
2017-03-02T01:18:46.415155+00:00 app[web.1]: at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121-cedar14]
2017-03-02T01:18:46.415154+00:00 app[web.1]: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121-cedar14]
2017-03-02T01:18:46.415156+00:00 app[web.1]: at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [recipe-site-1.0-SNAPSHOT.jar:na]
2017-03-02T01:18:46.415156+00:00 app[web.1]: at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [recipe-site-1.0-SNAPSHOT.jar:na]
2017-03-02T01:18:46.415157+00:00 app[web.1]: at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [recipe-site-1.0-SNAPSHOT.jar:na]
2017-03-02T01:18:46.415158+00:00 app[web.1]: at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:58) [recipe-site-1.0-SNAPSHOT.jar:na]
2017-03-02T01:18:46.415159+00:00 app[web.1]: Caused by: org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
2017-03-02T01:18:46.415160+00:00 app[web.1]: at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:261) ~[spring-orm-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415160+00:00 app[web.1]: at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244) ~[spring-orm-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415161+00:00 app[web.1]: at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:491) ~[spring-orm-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415163+00:00 app[web.1]: at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415164+00:00 app[web.1]: at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415164+00:00 app[web.1]: at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415165+00:00 app[web.1]: at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415166+00:00 app[web.1]: at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133) ~[spring-data-jpa-1.10.3.RELEASE.jar!/:na]
2017-03-02T01:18:46.415166+00:00 app[web.1]: at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415167+00:00 app[web.1]: at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415168+00:00 app[web.1]: at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415174+00:00 app[web.1]: at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415175+00:00 app[web.1]: at com.sun.proxy.$Proxy104.findAll(Unknown Source) ~[na:na]
2017-03-02T01:18:46.415176+00:00 app[web.1]: at com.recipes.service.UserServiceImpl.findAll(UserServiceImpl.java:22) ~[classes!/:na]
2017-03-02T01:18:46.415177+00:00 app[web.1]: at com.recipes.config.DatabaseLoader.run(DatabaseLoader.java:40) ~[classes!/:na]
2017-03-02T01:18:46.415177+00:00 app[web.1]: at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:791) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
2017-03-02T01:18:46.415178+00:00 app[web.1]: ... 14 common frames omitted
2017-03-02T01:18:46.415179+00:00 app[web.1]: Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
2017-03-02T01:18:46.415180+00:00 app[web.1]: at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415180+00:00 app[web.1]: at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415181+00:00 app[web.1]: at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415182+00:00 app[web.1]: at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415183+00:00 app[web.1]: at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415183+00:00 app[web.1]: at org.hibernate.loader.Loader.getResultSet(Loader.java:2117) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415184+00:00 app[web.1]: at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1900) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415185+00:00 app[web.1]: at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1876) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415185+00:00 app[web.1]: at org.hibernate.loader.Loader.doQuery(Loader.java:919) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415186+00:00 app[web.1]: at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415187+00:00 app[web.1]: at org.hibernate.loader.Loader.doList(Loader.java:2617) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415187+00:00 app[web.1]: at org.hibernate.loader.Loader.doList(Loader.java:2600) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415188+00:00 app[web.1]: at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2429) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415189+00:00 app[web.1]: at org.hibernate.loader.Loader.list(Loader.java:2424) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415189+00:00 app[web.1]: at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415190+00:00 app[web.1]: at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415191+00:00 app[web.1]: at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415192+00:00 app[web.1]: at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415191+00:00 app[web.1]: at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1326) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415194+00:00 app[web.1]: at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:483) ~[hibernate-entitymanager-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415195+00:00 app[web.1]: at org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:50) ~[hibernate-entitymanager-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415193+00:00 app[web.1]: at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606) ~[hibernate-entitymanager-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415195+00:00 app[web.1]: at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:327) ~[spring-data-jpa-1.10.3.RELEASE.jar!/:na]
2017-03-02T01:18:46.415196+00:00 app[web.1]: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121-cedar14]
2017-03-02T01:18:46.415197+00:00 app[web.1]: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121-cedar14]
2017-03-02T01:18:46.415197+00:00 app[web.1]: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121-cedar14]
2017-03-02T01:18:46.415202+00:00 app[web.1]: at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:503) ~[spring-data-commons-1.12.3.RELEASE.jar!/:na]
2017-03-02T01:18:46.415203+00:00 app[web.1]: at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:488) ~[spring-data-commons-1.12.3.RELEASE.jar!/:na]
2017-03-02T01:18:46.415198+00:00 app[web.1]: at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121-cedar14]
2017-03-02T01:18:46.415204+00:00 app[web.1]: at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460) ~[spring-data-commons-1.12.3.RELEASE.jar!/:na]
2017-03-02T01:18:46.415208+00:00 app[web.1]: at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415209+00:00 app[web.1]: at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) ~[spring-data-commons-1.12.3.RELEASE.jar!/:na]
2017-03-02T01:18:46.415210+00:00 app[web.1]: at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415210+00:00 app[web.1]: at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415211+00:00 app[web.1]: at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) ~[spring-tx-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415212+00:00 app[web.1]: at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415213+00:00 app[web.1]: at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415213+00:00 app[web.1]: at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
2017-03-02T01:18:46.415214+00:00 app[web.1]: ... 24 common frames omitted
2017-03-02T01:18:46.415215+00:00 app[web.1]: Caused by: org.postgresql.util.PSQLException: ERROR: column user0_.id does not exist
2017-03-02T01:18:46.415216+00:00 app[web.1]: Position: 66
2017-03-02T01:18:46.415216+00:00 app[web.1]: at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2412) ~[postgresql-42.0.0.jre7.jar!/:42.0.0.jre7]
2017-03-02T01:18:46.415217+00:00 app[web.1]: at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2125) ~[postgresql-42.0.0.jre7.jar!/:42.0.0.jre7]
2017-03-02T01:18:46.415218+00:00 app[web.1]: at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:297) ~[postgresql-42.0.0.jre7.jar!/:42.0.0.jre7]
2017-03-02T01:18:46.415219+00:00 app[web.1]: at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:428) ~[postgresql-42.0.0.jre7.jar!/:42.0.0.jre7]
2017-03-02T01:18:46.415219+00:00 app[web.1]: at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:354) ~[postgresql-42.0.0.jre7.jar!/:42.0.0.jre7]
2017-03-02T01:18:46.415220+00:00 app[web.1]: at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:169) ~[postgresql-42.0.0.jre7.jar!/:42.0.0.jre7]
2017-03-02T01:18:46.415220+00:00 app[web.1]: at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:117) ~[postgresql-42.0.0.jre7.jar!/:42.0.0.jre7]
2017-03-02T01:18:46.415221+00:00 app[web.1]: at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:82) ~[tomcat-dbcp-8.5.6.jar!/:8.5.6]
2017-03-02T01:18:46.415222+00:00 app[web.1]: at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:82) ~[tomcat-dbcp-8.5.6.jar!/:8.5.6]
2017-03-02T01:18:46.415222+00:00 app[web.1]: at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
2017-03-02T01:18:46.415223+00:00 app[web.1]: ... 57 common frames omitted
我有一个DatabaseLoader类,可以在启动时自动生成数据,这就是无法运行的ApplicationRunner。以下是相关课程:
User.java
@Entity
public class User extends GenericEntity {
public static final PasswordEncoder PASSWORD_ENCODER = new BCryptPasswordEncoder();
private String username;
@JsonIgnore
private String password;
@JsonIgnore
private String[] roles;
@OneToMany(mappedBy = "createdBy")
private List<Recipe> createdRecipes;
@ManyToMany
private List<Recipe> favoritedRecipes;
public User() {
super();
createdRecipes = new ArrayList<>();
favoritedRecipes = new ArrayList<>();
}
public User(String username, String password, String[] roles) {
this();
this.username = username;
this.roles = roles;
setPassword(password);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = PASSWORD_ENCODER.encode(password);
}
public String[] getRoles() {
return roles;
}
public void setRoles(String[] roles) {
this.roles = roles;
}
public List<Recipe> getCreatedRecipes() {
return createdRecipes;
}
public void setCreatedRecipes(List<Recipe> createdRecipes) {
this.createdRecipes = createdRecipes;
}
public List<Recipe> getFavoritedRecipes() {
return favoritedRecipes;
}
public void setFavoritedRecipes(List<Recipe> favoritedRecipes) {
this.favoritedRecipes = favoritedRecipes;
}
public void addCreatedRecipe(Recipe recipe) {
createdRecipes.add(recipe);
}
public void addFavoritedRecipe(Recipe recipe) {
favoritedRecipes.add(recipe);
}
public void removeCreatedRecipe(Recipe recipe) {
createdRecipes.remove(recipe);
}
public void removeFavoritedRecipe(Recipe recipe) {
favoritedRecipes.remove(recipe);
}
public boolean isAdmin() {
for(int i = 0; i < roles.length; i++) {
if(roles[i].equals("ROLE_ADMIN")) {
return true;
}
}
return false;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return username.equals(user.username) && user.getId() == this.getId();
}
}
DatabaseLoader.java
@Component
public class DatabaseLoader implements ApplicationRunner {
private UserService users;
private IngredientService ingredients;
private RecipeRepository recipes;
private CategoryService categories;
@Autowired
public DatabaseLoader(UserService users, IngredientService ingredients, RecipeRepository recipes, CategoryService categories) {
this.users = users;
this.ingredients = ingredients;
this.recipes = recipes;
this.categories = categories;
}
@Override
public void run(ApplicationArguments args) throws Exception {
if((users.findAll()).isEmpty()) {
User admin = new User("admin", "password", new String[]{"ROLE_ADMIN", "ROLE_USER"});
User nonAdmin = new User("non-admin", "password", new String[]{"ROLE_USER"});
users.save(admin);
users.save(nonAdmin);
Ingredient ingredient = new Ingredient("pizza", "pie", 1);
ingredients.save(ingredient);
Category american = new Category("American");
Category italian = new Category("Italian");
categories.save(american);
categories.save(italian);
Recipe pizza = new Recipe.RecipeBuilder("Pizza", american)
.addIngredient(ingredient)
.setDescription("Mmm... Delicious pizza")
.setCookTime(15)
.setPrepTime(5)
.addInstruction("Place pizza in oven", 0)
.addInstruction("Cook pizza for 15 minutes", 1)
.setImage("http://kingrichiespizza.com/wp-content/uploads/2015/12/d5a3498cfc9e53130b5f815ef44713b7_Jet.jpg")
.build();
pizza.setCreatedBy(admin);
Recipe burger = new Recipe.RecipeBuilder("Burger", american).build();
burger.setCreatedBy(admin);
Recipe spaghetti = new Recipe.RecipeBuilder("Spaghetti", italian).build();
spaghetti.setCreatedBy(nonAdmin);
admin.addFavoritedRecipe(spaghetti);
recipes.save(pizza);
recipes.save(burger);
recipes.save(spaghetti);
users.save(admin);
} else {
Application.main(args.getSourceArgs());
}
}
}
和GenericEntity.java
@MappedSuperclass
public abstract class GenericEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Version
private Long version;
public GenericEntity() {
id = null;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}
这是我的数据库配置类:
@Configuration
public class DataConfig {
@Bean
public BasicDataSource dataSource() throws URISyntaxException {
String dbUrl = System.getenv("JDBC_DATABASE_URL");
String username = System.getenv("JDBC_DATABASE_USERNAME");
String password = System.getenv("JDBC_DATABASE_PASSWORD");
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setUrl(dbUrl);
basicDataSource.setUsername(username);
basicDataSource.setPassword(password);
return basicDataSource;
}
}
和我的application.properties:
spring.data.rest.base-path = /api/v1
spring.datasource.schema = update
spring.datasource.name = recipes
spring.datasource.driver-class-name = org.postgresql.Driver
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.format_sql = true
spring.jpa.properties.hibernate.use_sql_comments = true
restart.include.all=.*
我是否缺少导致这种情况发生的配置?提前致谢
答案 0 :(得分:1)
我认为你错过了带有序列声明的id字段 通常我会宣布像这样的实体......
@Entity
@Table(name = "user")
@SequenceGenerator(name = "user_id_seq", sequenceName = "user_id_seq", allocationSize = 1)
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_id_seq")
private Long id;
... rest of your entity
}
答案 1 :(得分:0)
试着回想一下,但我认为你必须在postgres中创建身份表......可能是造成它的原因。