我正在使用PostgreSQL并创建了称为hib的表和架构。当我在db中创建外来表并运行应用程序时,它成功地将数据持久保存到数据库中。但是如果数据库中没有该表,则Hibernate不会在hib模式中创建表,我认为hibernate不会读取hibernate.cfg.xml中提到的hdm2ddl.auto属性。根据日志,它不提供类似创建或更新表(如果存在)的日志。它直接尝试插入值。 尝试了许多方法,但无法像创建,更新,创建-删除那样工作。 每次相同的错误ERROR:关系“ hib.alien”不存在。 这是持久性类。
@Entity()
@Table(name="hib.alien")
public class Alien {
@Id
private int aid;
private String aname;
private String acolor;
public Alien() {}
public Alien(int aid, String aname, String acolor) {
this.aid = aid;
this.aname = aname;;
this.acolor = acolor;
}
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getAname() {
return aname;
}
public void setAname(String aname) {
this.aname = aname;
}
public String getAcolor() {
return acolor;
}
public void setAcolor(String acolor) {
this.acolor = acolor;
}
@Override
public String toString() {
return "Alien [aid=" + aid + ", aname=" + aname + ", acolor=" + acolor + "]";
}
}
hibernate.cfg.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hib</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">123</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
和主班
public class App
{
public static void main( String[] args )
{
Alien alien = new Alien(1, "Nagaraj Kharvi", "Red");
System.out.println(alien);
Configuration con = new Configuration().configure().addAnnotatedClass(Alien.class);
SessionFactory sf = con.buildSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
s.save(alien);
tx.commit();
}
}
这是日志:
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
Mar 03, 2019 10:28:08 AM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Hibernate: insert into hib.alien (acolor, aname, aid) values (?, ?, ?)
Mar 03, 2019 10:28:09 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 42P01
Mar 03, 2019 10:28:09 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ERROR: relation "hib.alien" does not exist
Position: 13
Mar 03, 2019 10:28:09 AM org.hibernate.internal.ExceptionMapperStandardImpl mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [org.hibernate.exception.SQLGrammarException: could not execute statement]
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
答案 0 :(得分:1)
对于PostgreSQL实体应如下所示,这样该表将创建 在那种模式下。
@Entity()
@Table(name = "alien" schema = "\"hib\"")
public class Alien {
@Id
private int aid;
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
}