org.postgresql.util.PSQLException:错误:关系“employee1”不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25889105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.postgresql.util.PSQLException: ERROR: relation "employee1" does not exist
提问by monu
I am very new in postgresql and hybernate and i am getting a problem in my first program, can you please help me out and thanks in advance.
我对 postgresql 和 hybernate 非常陌生,我在我的第一个程序中遇到了问题,请您帮帮我,并提前致谢。
It prints comment
它打印评论
Hibernate:
/* insert testhybernate1.Employee
*/ insert
into
employee1
(ename, mobile, email, id)
values
(?, ?, ?, ?)
and gives error:-
并给出错误:-
org.postgresql.util.PSQLException: ERROR: relation "employee1" does not exist
I added
我加了
<property name="hibernate.hbm2ddl.auto">create</property>
in my config file then that problem solved a new table created by hybernate with name Employee. But whatever i am inserting in this table through my program it is not updating in my database. First question is why by adding
在我的配置文件中,该问题解决了由 hybernate 创建的名为 Employee 的新表。但是无论我通过我的程序在这个表中插入什么,它都不会在我的数据库中更新。第一个问题是为什么通过添加
<property name="hibernate.hbm2ddl.auto">create</property>
my problem solved and second question is why it is not updating in my database when i search through pgadmin?
我的问题解决了,第二个问题是为什么当我通过 pgadmin 搜索时它没有在我的数据库中更新?
In the database postgesql in public schema, i created a table employee1 with all column and table name in small letter.my mapping file "empmapping.hbm.xml"
在公共模式的数据库 postgesql 中,我创建了一个表 employee1,其中所有列和表名都在小写字母.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>
My config file "hypernate.cfg.xml" is given below:-
我的配置文件“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>
my pogo class:- Employee.java
我的 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;
}
}
My application class Hybernate1.java
我的应用程序类 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
}
}
回答by monu
Sorry for the bug,
对不起,错误,
I got the answer, In my config file there should be
我得到了答案,在我的配置文件中应该有
<property name="hibernate.connection.url">jdbc:postgresql:postgres</property>
instead of
代替
<property name="hibernate.connection.url">jdbc:postgresql:template1</property>