Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未创建PostgreSQL数据库_Java_Spring_Postgresql_Hibernate_Spring Boot - Fatal编程技术网

Java 未创建PostgreSQL数据库

Java 未创建PostgreSQL数据库,java,spring,postgresql,hibernate,spring-boot,Java,Spring,Postgresql,Hibernate,Spring Boot,我有一个连接到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

我有一个连接到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();
    }
}
以及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=.*

是否有我丢失的导致这种情况发生的配置?提前感谢您尝试回忆,但我认为您必须在postgres中创建标识表。可能这就是原因。

我认为您缺少带有序列声明的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
}

所以这不是由Hibernate自动生成的?它的实体映射到带有idYeah的超类,我有一个GenericEntity,它具有id字段,如果db未嵌入
spring.jpa.Hibernate.ddl auto
为none,则用户扩展该字段。尝试将其设置为
create
@SergiiGetman-我添加了这个属性,但它似乎没有创建它。我还是会犯同样的错误
@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
}