gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:02:55 27 4
gpt4 key购买 nike

我想在 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 中不存在,则创建一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23845346/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com