2015-02-11 36 views
0

我想學習Hibernate框架與製作一個簡單的程序,推動一類Cliente到一個表上的Postgres,該錯誤回報情況如下:org.postgresql.util.PSQLException:ERROR:關係「clienti_id_seq」不存在

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> 

這是休眠cfg.xml中文件,關於URL傳遞和用戶的信息是正確的

<?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> 

這是主文件,在這裏我創建一個客戶,並嘗試推入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("[email protected]"); 
    cliente.setClientePermesso("admin"); 
    cliente.setClienteCommessa(0); 
    // Save*/ 
    session.save(cliente); 
    // Commit, calling of commit will cause save an instance of employee 
    session.getTransaction().commit(); 
    } 
} 

感謝大家幫助我,我不明白錯誤,我認爲它是在發電機的聲明。

+1

pgAdmin的顯示大寫的序列名稱的一部分。只能使用小寫來避免這樣的問題。 – 2015-02-11 13:00:08

+0

感謝您的答案,但問題仍然存在後,我在任何聲明 – 2015-02-11 13:58:32

回答

0

請嘗試更改生成器類從序列身份和看到。我想這個問題是ID的自動生成。

請參見以下鏈接: http://www.roseindia.net/hibernate/hibernateidgeneratorelement.shtml http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-property

+0

謝謝你,我讀了你的鏈接,我試圖改變生成器類,現在錯誤更改「org.postgresql。 util.PSQLException:錯誤:關係「clienti」不存在「,但它沒有意義,因爲關係是由Netbeans的映射文件嚮導自動生成的,所以我認爲它看到關係 – 2015-02-13 14:22:28

+0

您是創建自己的模式還是創建公共模式中的表?引用位於另一個架構中的表以外的公共需要在引用表時使用約定schemaname.tablename。 – 2015-02-16 07:38:29

+0

我已經解決了這個問題,錯誤是在ID列的類型聲明,在postgres這個列有序列類型,所以在地圖類我必須給它「java.lang.Long」類型「 謝謝所有幫助! – 2015-02-18 15:15:50