当我尝试使用Hibernate插入数据时,“表不存在”

时间:2018-07-30 18:59:53

标签: postgresql hibernate spring-boot

我在Spring Boot项目中以XML方式使用Hibernate框架。我选择PostgreSQL作为DBMS。我已经设置了配置文件,但是每当尝试插入数据时都会收到“ 无法提取ResultSet ”消息。我得到的例外是

  

org.postgresql.util.PSQLException:错误:关系“雇员”不存在

我测试了与Intellij的连接,这很好。我认为我的hbm文件有问题。


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="dialect">org.hibernate.dialect.PostgreSQL94Dialect</property>
    <property name="connection.url">jdbc:postgresql://localhost:5432/MyDB</property>
    <property name="connection.username">postgres</property>
    <property name="connection.password"></property>
    <property name="connection.driver_class">org.postgresql.Driver</property>
    <mapping resource="employees.hbm.xml" />
</session-factory>


employees.hbm.xml

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

<hibernate-mapping>
<class name="com.company.entities.Employee" table="Employees" schema="public" >
    <id name="eid" type = "int" column = "eid">
        <generator class="increment"/>
    </id>
    <property name="name" column="name" type="java.lang.String" />
    <property name="description" column="description" type="java.lang.String" />
    <property name="salary" column="salary" type="java.lang.Double" />
</class>
</hibernate-mapping>

EmployeeDAO.java

@Repository
public class EmployeeDAO
{
static Session sessionObj;


private static SessionFactory buildSessionFactory()
{
    return new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
}

public void insertEmp(List<Employee> allEmployees)
{
    sessionObj = buildSessionFactory().openSession();
    sessionObj.beginTransaction();
    for (Employee emp: allEmployees)
    {
        sessionObj.save(prod);
    }
    sessionObj.getTransaction().commit();
    if(sessionObj != null)
    {
        sessionObj.close();
    }
}
}

1 个答案:

答案 0 :(得分:1)

错误消息中提到了“雇员”,而在您的映射中,您是在谈论“雇员”……我认为这可能是一个案例问题:

https://blog.xojo.com/2016/09/28/about-postgresql-case-sensitivity/


employees.hbm.xml文件需要从

更改
<class name="com.company.entities.Employee" table="Employees" schema="public" >

<class name="com.company.entities.Employee" table="`Employees`" schema="public" >