Java 如果Hibernate中不存在,则创建一个表
我想在postgresql中创建一个不存在的表 hibernate.cfg.xmlJava 如果Hibernate中不存在,则创建一个表,java,hibernate,postgresql,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.pas
<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>
这是我试图创建新表的代码:
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
请尝试将属性名称更改为hibernate.hbm2ddl.autot这就是问题所在,谢谢:)您能否将此评论作为答案发布,以便我可以解决此主题。
hibernate.hbm2ddl.auto