gpt4 book ai didi

java - 组织.postgresql.util.PSQLException : ERROR: relation "employee1" does not exist

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

我是 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 搜索时它没有在我的数据库中更新?

在公共(public)模式的数据库 postgesql 中,我创建了一个表 employee1,所有列和表名都用小写字母表示。我的映射文件“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

}

}

最佳答案

对不起,这个错误,

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

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

代替

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

关于java - 组织.postgresql.util.PSQLException : ERROR: relation "employee1" does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25889105/

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