【问题标题】:Hibernate Creating an already created table even if property <name="hibernate.hbm2ddl.auto" value="update"> is set to update.(Db is postgres)即使属性 <name="hibernate.hbm2ddl.auto" value="update"> 设置为更新,Hibernate 创建一个已经创建的表。(数据库是 postgres)
【发布时间】:2020-12-05 14:27:38
【问题描述】:

我使用 Hibernate 作为 Spring Boot 应用程序的 ORM 将值写入我的 Postgres 数据库。我已经在我的 persistence.xml 中配置了属性

Persistence.xml

    <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
    <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/ImageHoster" />
    <property name="javax.persistence.jdbc.user" value="postgres" />
    <property name="javax.persistence.jdbc.password" value="postgres" />

    <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect" />
    <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>

    <property name="hibernate.hbm2ddl.auto" value="update"/>
    <property name="hibernate.show_sql" value="true"/>
    <property name="hibernate.format_sql" value="true"/>
</properties>

我创建了一个名为 Movie 的实体

电影

@Entity
@Table(name = "Movie")
public class Movie {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    public String id;

    @Column(name = "movie")
    public String movie;

    @Column(name = "updatedDate")
    public Date updatedDate;

    @Lob
    @Column(name = "image")
    public String image;

    public String getMovie() {
        return movie;
    }

    public void setMovie(String movie) {
        this.movie = movie;
    }

    public Date getUpdatedDate() {
        return updatedDate;
    }

    public void setUpdatedDate(Date updatedDate) {
        this.updatedDate = updatedDate;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }
}

第一次当我的 postgres 数据库中没有电影表时,该表正在创建并且没有错误。但是当我在创建表后运行应用程序时,它正在尝试再次创建表导致以下错误

org.hibernate.tool.schema.spi.CommandAcceptanceException:通过 JDBC 语句执行 DDL 时出错。

我的 JPA 配置是这样的

JPA配置

@Configuration
public class JpaConfig {

    @Bean
    public EntityManagerFactory entityManagerFactory(){
        LocalContainerEntityManagerFactoryBean emf =new LocalContainerEntityManagerFactoryBean();
        emf.setPersistenceXmlLocation("classpath:META-INF/persistence.xml");
        emf.afterPropertiesSet();

        return emf.getObject();
    }

    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource ds = new DriverManagerDataSource();
        ds.setDriverClassName("org.postgresql.Driver");
        ds.setUrl("jdbc:postgresql://localhost:5432/ImageHoster");
        ds.setUsername("postgres");
        ds.setPassword("postgres");

        return ds;
    }
}

知道我哪里出错了,因为我有另一个名为 User 的实体工作正常,并且不会在每次应用程序运行时创建。

【问题讨论】:

  • 附带说明,将public EntityManagerFactory entityManagerFactory() 更改为public LocalContainerEntityManagerFactoryBean entityManagerFactory()。不要使用 .getObject() 自己创建 EntityManagerFactory - LocalContainerEntityManagerFactoryBeanDisposableBean,如果不将其设为正确的 bean,则不会让 Spring 清理它

标签: spring-boot hibernate spring-mvc


【解决方案1】:

一旦我在我的实体中将表名从 Movie 更改为 movie,一切都按预期工作。

@实体 @Table(name = "电影"){

}

这让一切都很好..知道为什么会这样吗?因为我无法理解为什么这是一个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-20
    • 2022-01-14
    • 1970-01-01
    • 2021-02-19
    • 2016-09-08
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    相关资源
    最近更新 更多