【问题标题】:org.postgresql.util.PSQLException: ERROR: the relation"clienti_id_seq" does not existorg.postgresql.util.PSQLException:错误:关系“clienti_id_seq”不存在
【发布时间】:2015-02-11 12:55:15
【问题描述】:

我正在尝试通过制作一个简单的程序来学习休眠框架,该程序将类 Cliente 推送到 postgres 上的表中,返回的错误如下:

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not extract ResultSet

Caused by: org.postgresql.util.PSQLException: ERROR: the relation "clienti_id_seq" does not exist

这是我的 PGAdmin 数据库(抱歉 imgur 我无法直接上传图片)http://i.imgur.com/Fz9o1fR.png?1

这是类Cliente

public class Cliente {

private long clienteId;
private String clienteNome;
private String clienteCognome;
private String clienteTelefono;
private String clienteMail;
private String clientePermesso;
private long clienteCommessa;

public long getClienteId() {
    return clienteId;
}

public void setClienteId(long clienteId) {
    this.clienteId = clienteId;
}

public String getClienteNome() {
    return clienteNome;
}

public void setClienteNome(String clienteNome) {
    this.clienteNome = clienteNome;
}
public String getClienteCognome() {
    return clienteCognome;
}

public void setClienteCognome(String clienteCognome) {
    this.clienteCognome = clienteCognome;
}

 public String getClienteTelefono() {
    return clienteTelefono;
}

public void setClienteTelefono(String clienteTelefono) {
    this.clienteTelefono = clienteTelefono;
}

 public String getClienteMail() {
    return clienteMail;
}

public void setClienteMail(String clienteMail) {
    this.clienteMail = clienteMail;
}

 public String getClientePermesso() {
    return clientePermesso;
}

public void setClientePermesso(String clientePermesso) {
    this.clientePermesso = clientePermesso;
}

 public long getClienteCommessa() {
    return clienteCommessa;
}

public void setClienteCommessa(long clienteNome) {
    this.clienteCommessa = clienteCommessa;
}

}

这是我的映射文件

<hibernate-mapping>
<class name="beans.Cliente" table="Clienti">
    <id name="clienteId" type="integer" column="id" >

        <generator class="sequence"> 
            <param name="sequence">CLIENTI_ID_seq</param>            
        </generator>
    </id>
    <property name="clienteNome" column="nome" type="string">            
    </property>
    <property name="clienteCognome" column="cognome" type="string">            
    </property>
        <property name="clienteTelefono" column="telefono" type="string">            
    </property>
    <property name="clienteMail" column="mail" type="string">            
 </property>
        <property name="clientePermesso" column="permesso" type="string">            
    </property>
    <property name="clienteCommessa" column="commessa" type="string">            
</property>
</class>
</hibernate-mapping>

这是hibernate cfg.xml文件,url pass和user信息正确

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration   DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
<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:postgres</property>

<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/Georilievi</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">Fabio1990</property>
<property name="hibernate.current_session_context_class">thread</property>


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

</hibernate-configuration>

这是主文件,这里我创建了一个Cliente的实例,并尝试推入postgres数据库

package main;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import beans.Cliente;

public class Main {
public static void main(String [] args){
    // Create a configuration instance
    Configuration configuration = new Configuration();
    // Provide configuration file
    configuration.configure("hibernate.cfg.xml");
    // Build a SessionFactory
    SessionFactory factory = configuration.buildSessionFactory(new StandardServiceRegistryBuilder().configure().build());
    // Get current session, current session is already associated with Thread
    Session session = factory.getCurrentSession();

    // Begin transaction

    session.getTransaction().begin();

    Cliente cliente = new Cliente();
    cliente.setClienteNome("Fabio");
    cliente.setClienteCognome("Tramontana");
    cliente.setClienteTelefono("3343052346");
    cliente.setClienteMail("info.tramontanafabio@gmail.com");
    cliente.setClientePermesso("admin");
    cliente.setClienteCommessa(0);
    // Save*/
    session.save(cliente);
    // Commit, calling of commit will cause save an instance of employee
    session.getTransaction().commit();
    }
}

感谢大家帮助我,我不明白错误,我认为它在生成器的声明中。

【问题讨论】:

  • pgAdmin 以大写形式显示部分序列名称。只使用小写以避免此类问题。
  • 感谢您的回答,但是在我在任何声明中使用小写字母后问题仍然存在

标签: java spring hibernate postgresql jpa


【解决方案1】:

请尝试将生成器类从序列更改为标识并查看。我想问题是 ID 的自动生成。

查看以下链接: http://www.roseindia.net/hibernate/hibernateidgeneratorelement.shtml http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-property

【讨论】:

  • 感谢您的回答,我阅读了您的链接并尝试更改生成器类,现在“org.postgresql.util.PSQLException:错误:关系“clienti”中的错误也发生了变化不存在”,但这没有意义,因为关系是由 Netbeans 的映射文件向导自动生成的,所以我认为它看到了关系
  • 您是在创建自己的架构还是在公共架构中创建表?引用公共以外的其他模式中的表需要在引用表时使用约定 schemaname.tablename。
  • 我已经解决了这个问题,错误出现在 id 列的类型声明中,在 postgres 上此列具有串行类型,所以在地图分类中我必须给它“java.lang.Long “类型”谢谢大家的帮助!
猜你喜欢
  • 2017-04-11
  • 2015-10-09
  • 2017-04-26
  • 2020-09-03
  • 1970-01-01
  • 1970-01-01
  • 2020-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多