org.postgresql.util.PSQLException:错误:关系" employee1"不存在

时间:2014-09-17 11:17:17

标签: java postgresql

我是postgresql和hybernate的新手,我的第一个程序遇到了问题,请你帮我解决,并提前致谢。

打印评论

Hibernate: 

/* insert testhybernate1.Employee

*/ insert 
    into
        employee1
        (ename, mobile, email, id) 
    values
        (?, ?, ?, ?)

并给出错误: -

org.postgresql.util.PSQLException: ERROR: relation "employee1" does not exist

我添加了

<property name="hibernate.hbm2ddl.auto">create</property> 

在我的配置文件中,然后该问题解决了由hybernate创建的名为Employee的新表。但无论我通过我的程序在此表中插入什么,它都不会在我的数据库中更新。第一个问题是添加

的原因
<property name="hibernate.hbm2ddl.auto">create</property>

我的问题解决了,第二个问题是我搜索pgadmin时数据库中没有更新的原因?

在公共模式的postgesql数据库中,我创建了一个表employee1,其中包含所有列和表名称的小letter.my映射文件“empmapping.hbm.xml”

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">


<hibernate-mapping>
<class name="testhybernate1.Employee" table="employee1">
<id name="id" column="id" type="integer">
<generator class="assigned"></generator>
</id>
<property name="ename" column="ename" type="string"></property>
<property name="mobile" column="mobile" type="long"></property>
<property name="email" column="email" type="string"></property>
</class>
</hibernate-mapping>

我的配置文件“hypernate.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="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql:template1</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">Monu26@dmail</property>



<property name="hibernate.connection.autocommit">false</property>


<property name="hibernate.show_sql">true</property>


<property name="hibernate.format_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>



<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

<mapping resource="empmapping.hbm.xml"></mapping>
</session-factory>


</hibernate-configuration>

我的pogo课程: - Employee.java

package testhybernate1;

import java.io.Serializable;

public class Employee implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;
private int id;
private String ename;
private long mobile;
private String email;

public Employee(){}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getEname() {
    return ename;
}

public void setEname(String ename) {
    this.ename = ename;
}

public long getMobile() {
    return mobile;
}

public void setMobile(long mobile) {
    this.mobile = mobile;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}



}

我的应用程序类Hybernate1.java

package testhybernate1;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Hybernate1 {

@SuppressWarnings("deprecation")
public static void main(String[] args) {
    System.out.println("starting........");
    Configuration cfg= new Configuration();
//  cfg.configure();
    try
    {
        cfg.configure();
    }
    catch(Exception e)
    {
        System.out.println("*** Exception while configuring:"+e);
    }
    SessionFactory sf=null;
    try
    {
    //  SessionFactory sf=new SessionFactory(cfg);
        sf=cfg.buildSessionFactory();
    }
    catch(Exception e)
    {
        System.out.println("exception in creating sessionfactory :"+e);

    }
    if(sf.equals(null))
    return;
    else
    {
        Session s=sf.openSession();

        Transaction tx = s.beginTransaction();

        Employee emp=new Employee();
        emp.setId(1);
        emp.setEname("Raghav");
        emp.setMobile(95899);
        emp.setEmail("k123@mail");


        Integer eid=(Integer)s.save(emp);
        //s.flush();
        tx.commit();
        s.close();
        System.out.println("emp id in database="+eid);

    }
    // TODO Auto-generated method stub

}

}

1 个答案:

答案 0 :(得分:2)

抱歉,

我得到了答案,在我的配置文件中应该有

<property name="hibernate.connection.url">jdbc:postgresql:postgres</property>

而不是

<property name="hibernate.connection.url">jdbc:postgresql:template1</property>