如果在Hibernate中不存在则创建一个表

时间:2014-05-24 13:01:23

标签: java hibernate postgresql

我想在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>

Enity:

@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

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

如何允许我的程序创建新表。

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

1 个答案:

答案 0 :(得分:2)

请将属性名称更改为

hibernate.hbm2ddl.auto