【问题标题】:create a table if not exits in Hibernate如果 Hibernate 中不存在,则创建一个表
【发布时间】:2014-05-24 13:01:22
【问题描述】:

我想在 postgresql 中创建一个不存在的表。

hibernate.cfg.xml

<hibernate-configuration>
 <session-factory name="">
  <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
  <property name="hibernate.connection.password">toor</property>
  <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
  <property name="hibernate.connection.username">postgres</property>
  <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
  <property name="current_session_context_class">thread</property>
  <property name="hbm2dll.auto">update</property>
  <property name="show_sql">true</property>
  <mapping class="org.firsthibernatproject.dto.Person"/>
 </session-factory>
</hibernate-configuration>

实体:

@Entity
@Table (name="PERSON_DETAILS")
public class Person {
    @Id
    @Column (name="id")
    private int id;
    @Column (name="first_name")
    private String firstName;
    @Column (name="last_name")
    private String lastName;
    private Date joinedDate;
    private String adresse;
    private String description;



    public String getAdresse() {
        return adresse;
    }
    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public Date getJoinedDate() {
        return joinedDate;
    }
    public void setJoinedDate(Date joinedDate) {
        this.joinedDate = joinedDate;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }


}

这是我尝试创建新表的代码:

public class HibernateTest {
    public static void main (String args[]) {
        Person person = new Person();
        person.setId(3);
        person.setFirstName("Orihime");
        person.setLastName("Inoue");
        person.setAdresse("Karakura Town");
        person.setJoinedDate(new Date());
        person.setDescription("She has a unique power.");

        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(person);
        session.getTransaction().commit();

    }
}

正如您在 hbm2dll.auto 属性中看到的那样,我使用了值 update,如果数据库中不存在该表,它将创建该表,但是我在 Eclipse 的控制台中收到了以下错误消息:

Hibernate: insert into PERSON_DETAILS (adresse, description, first_name, joinedDate, last_name, id) values (?, ?, ?, ?, ?, ?)
mai 24, 2014 2:49:06 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 42P01
mai 24, 2014 2:49:06 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ERREUR: la relation « person_details » n'existe pas
  Position : 13
mai 24, 2014 2:49:06 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute statement

表示我尝试添加的表不存在。

如何让我的程序创建新表。

我还有另一个问题,就是在已经创建的表中添加一个新列,它显示该列不存在,但如果表中不存在该列,则应该创建该列。

【问题讨论】:

  • 您是否尝试将属性名称更改为 hibernate.hbm2ddl.auto
  • 这就是问题所在,谢谢 :) 您能否发表此评论作为答案,以便我可以解决此主题。

标签: java hibernate postgresql


【解决方案1】:

请将属性名改为

hibernate.hbm2ddl.auto

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2010-12-03
    • 2013-05-26
    • 1970-01-01
    • 2016-02-15
    相关资源
    最近更新 更多