-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Annotation problems using @TableGenerator (Postgre).
PostPosted: Wed Jul 30, 2008 4:51 pm 
Newbie

Joined: Wed Jul 30, 2008 3:58 pm
Posts: 1
Hello!

I'm trying to use Annotations (@TableGenerator) to have ID control in my table, but rather than I'm getting an Exception hheeh.

Actually I did follow each step from Hibernate Annotation Manual and JPA Specification, to create my Bean.

When I run the method it executes an inserts into the hibernate.id_gen and increment wonderfully, but the hibernate.tbpessoa it doesn't insert nothing and then throws this exception.

I don't know what am I doing wrong with my code.

Thanks in advance.

Data Base:
PostgreSQL 8.2

Hibernate version:
hibernate-3.2.6.ga
hibernate-annotations-3.4.0.CR1


Full stack trace of any exception that occurs:

Code:
org.hibernate.exception.SQLGrammarException: could not get or update next value
   org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
   org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   org.hibernate.engine.TransactionHelper$1Work.doWork(TransactionHelper.java:41)
   org.hibernate.engine.transaction.Isolater$JdbcDelegate.delegateWork(Isolater.java:187)
   org.hibernate.engine.transaction.Isolater.doIsolatedWork(Isolater.java:43)
   org.hibernate.engine.TransactionHelper.doWorkInNewTransaction(TransactionHelper.java:51)
   org.hibernate.id.MultipleHiLoPerTableGenerator.generate(MultipleHiLoPerTableGenerator.java:192)
   org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:99)
   org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
   org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
   org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
   org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
   org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
   org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:539)
   org.hibernate.impl.SessionImpl.save(SessionImpl.java:527)
   org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   java.lang.reflect.Method.invoke(Method.java:597)
   org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:318)
   $Proxy9.save(Unknown Source)
   br.action.pessoaAction.savePeople(pessoaAction.java:38)
   br.action.pessoaAction.doGet(pessoaAction.java:23)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


Here is my Bean and Annotation codes:

Code:
package br.vo;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

@Entity
@Table(name="tbpessoa", schema="hibernate")
@TableGenerator(name= "emp_gen", schema="hibernate", table= "id_gen",
            pkColumnName="gen_name", valueColumnName="gen_val",
            pkColumnValue = "emp_gen")
               
public class Pessoa implements Serializable{
   
   private static final long serialVersionUID = 1L;

   @Id @GeneratedValue(strategy=GenerationType.TABLE, generator="emp_gen")
   private int idPessoa;
   
   @Column(name="nome", length=150)
   private String nomePessoa;
      
   @Column(name="idade")
   private int idadePessoa;
   
   public Pessoa(){}

   gets and sets
   
}



Code between sessionFactory.openSession() and session.close():

Code:
public void savePerson(String nomePessoa, int idadePessoa) {

      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();
      
      //Cadastrar Novo Objeto.
      Pessoa objPessoa = new Pessoa();
       
       //objPessoa.setIdPessoa(id);
       objPessoa.setIdadePessoa(idadePessoa);
       objPessoa.setNomePessoa(nomePessoa);
       
       session.save(objPessoa);
       session.getTransaction().commit();
   }



+- - - - - - - - - - - - - - - - +
|hibernate.tbpessoa |
+- - - - - - - - - - - - - - - - +
|id nome idade
|null null null


Here is the SQL to create hibernate.id_ge table

Code:
CREATE TABLE hibernate.id_gen(
  gen_name character varying(80) NOT NULL,
  gen_val integer,
  CONSTRAINT pk_id_gen PRIMARY KEY (gen_name))
WITHOUT OIDS;
ALTER TABLE hibernate.id_gen OWNER TO postgres;


Simulation after executes the code above.

+- - - - - - - - - - - - - - - - +
|hibernate.id_gen |
+- - - - - - - - - - - - - - - - +
|gen_name(PK) gen_val |
| Emp_Gen = 6


Mapping documents: hibernate.cfg.xml

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

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://localhost:5432/arcega</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">654321</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">10</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>


        <!-- Drop and re-create the database schema on startup -->
        <!-- <property name="hbm2ddl.auto">create</property> -->

        <mapping class="br.vo.Pessoa"/>

    </session-factory>

</hibernate-configuration>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.