1
我想在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();
}
}
由於你可以在看到屬性我使用的值update
這將創建表,如果不存在於數據庫中,而是我得到了日食的控制檯此錯誤消息:
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的 – Kalyan
這是問題,謝謝:) 你能發佈此評論作爲一個答案,讓我可以使這個話題解決。 –
發表回覆:) – Kalyan