-->
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.  [ 4 posts ] 
Author Message
 Post subject: [SOLVED] SEQUENCE - entity id with different value from DB
PostPosted: Fri Jun 19, 2009 8:45 am 
Newbie

Joined: Thu Jun 18, 2009 4:11 pm
Posts: 3
I need a help for a question:
I use postgresql sequence mapped with annotation hibernate/jpa.
The hibernate returns a value like 3800, but the postgresql sequence have a value like 75.

I never see this before.


when i execute the follow method three times:

Code:
   public Integer inserir(CargoTerceirizado cargo) throws DAOException {
      //Exibe informações sobre o valor atual da sequence      
      Object object =
         getEntityManager().createNativeQuery("select nextval ('cet.num_seq_caet')").getSingleResult();
      System.out.println(object);
      System.out.println(object.getClass().getName());

      //executa a inserção
      getEntityManager().persist(cargo);
      getEntityManager().flush();      
      return cargo.getId();
   }

I receive the log:
Code:

17:19:58,584 DEBUG [org.hibernate.SQL]
    select
        nextval ('cet.num_seq_caet')
17:19:58,584 TRACE [org.hibernate.type.BigIntegerType] returning '75' as column: nextval
17:19:58,584 INFO  [STDOUT] 75
17:19:58,584 INFO  [STDOUT] java.math.BigInteger
17:19:58,584 DEBUG [org.hibernate.SQL]
    select
        nextval ('cet.num_seq_caet')
17:19:58,896 DEBUG [org.hibernate.SQL]
    insert
    into
        cet.cargo_empregado_terc
        (desc_caet, num_seq_caet)
    values
        (?, ?)
17:19:58,912 TRACE [org.hibernate.type.StringType] binding 'aaaaaaaa' to parameter: 1
17:19:58,912 TRACE [org.hibernate.type.IntegerType] binding '3800' to parameter: 2
17:19:59,052 DEBUG [com.cet.apresentacao.CargoBean] Persist id=3800
(...)
17:20:11,365 DEBUG [org.hibernate.SQL]
    select
        nextval ('cet.num_seq_caet')
17:20:11,381 TRACE [org.hibernate.type.BigIntegerType] returning '77' as column: nextval
17:20:11,381 INFO  [STDOUT] 77
17:20:11,381 INFO  [STDOUT] java.math.BigInteger
17:20:11,381 DEBUG [org.hibernate.SQL]
    insert
    into
        cet.cargo_empregado_terc
        (desc_caet, num_seq_caet)
    values
        (?, ?)
17:20:11,381 TRACE [org.hibernate.type.StringType] binding 'bbbbbbbbbbb' to parameter: 1
17:20:11,381 TRACE [org.hibernate.type.IntegerType] binding '3801' to parameter: 2
17:20:11,396 DEBUG [com.cet.apresentacao.CargoBean] Persist id=3801
(...)
17:24:30,318 DEBUG [org.hibernate.SQL]
    select
        nextval ('cet.num_seq_caet')
17:24:30,318 TRACE [org.hibernate.type.BigIntegerType] returning '78' as column: nextval
17:24:30,334 INFO  [STDOUT] 78
17:24:30,334 INFO  [STDOUT] java.math.BigInteger
17:24:30,334 DEBUG [org.hibernate.SQL]
    insert
    into
        cet.cargo_empregado_terc
        (desc_caet, num_seq_caet)
    values
        (?, ?)
17:24:30,334 TRACE [org.hibernate.type.StringType] binding 'ccccccccccccc' to parameter: 1
17:24:30,334 TRACE [org.hibernate.type.IntegerType] binding '3802' to parameter: 2
17:24:30,334 DEBUG [com.cet.apresentacao.CargoBean] Persist id=3802


The first nextval returns 75 and is used for sysout.
The second is called by hibernate when persist the entity 'aaaaaaaa' parameter 1. Hibernate set 3800 in the id.
The thrid nextval returns returns 77 and is used for sysout. It prove that hibernate has really called nextval (76) when persist the previous entity.
In the next step, hibernate persists the entity 'bbbbbbb' with id 3801 and don't log the nextval call.
The next nextval returns 78 and is used for sysout. This prove that hibernate don't call nextval when persist the previous entity.
Following hibernate persists the entity 'ccccccc' with id 3802 without log nextval call.




That's my configuration:

classe CargoTerceirizado
Code:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;


@Entity
@Table(name = "cargo_empregado_terc", schema = "cet")
public class CargoTerceirizado implements Serializable {
(...)
   
   @Id
   @Column(name = "num_seq_caet")
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CARGO_SEQ")
   @SequenceGenerator(name = "CARGO_SEQ", sequenceName = "cet.num_seq_caet")
   private Integer id;
(...)


the postgres table:
Code:
CREATE TABLE cargo_empregado_terc
(
  num_seq_caet numeric(5) NOT NULL,
  desc_caet character varying(50),
  CONSTRAINT pk_caet PRIMARY KEY (num_seq_caet)
)



the postgres sequence:
Code:
CREATE SEQUENCE num_seq_caet
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 78
  CACHE 1;



persistence.xml
Code:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
   xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

   <persistence-unit name="CET_JTA" transaction-type="JTA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:CET_DB_DS</jta-data-source>
(...)
      <class>com.cet.entidades.CargoTerceirizado</class>
(...)
         <property name="hibernate.archive.autodetection" value="class" />
         <property name="hibernate.format_sql" value="true" />
         <property name="use_sql_comments" value="true" />
         <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
         <property name="hibernate.cache.use_second_level_cache" value="false"/>
      </properties>

   </persistence-unit>

</persistence>



All the entitys in the system have the same problem.
I really need that entity ids respects some range for integratio with another system.

Sorry about the poor english, and thanks for help!


Last edited by pdrvaz on Mon Jul 06, 2009 11:54 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: SEQUENCE - entity id with different value from database
PostPosted: Wed Jun 24, 2009 7:15 am 
Newbie

Joined: Thu Jun 18, 2009 4:11 pm
Posts: 3
I think is bether workaround and set the id manually.
But if someone have some idea about this, please, post me something.

Thanks.


Top
 Profile  
 
 Post subject: [SOLVED] SEQUENCE - entity id with different value from data
PostPosted: Mon Jul 06, 2009 11:53 am 
Newbie

Joined: Thu Jun 18, 2009 4:11 pm
Posts: 3
I'm using jboss 4.2.1 and hibernate-3.2, hibernate-entitymanager-3.2.1.GA and hibernate-annotations-3.2.1.GA.

The method org.hibernate.id.BinderHelper.makeIdGenerator(...) set the attribute identifierGeneratorProperties

who que determine the attribute value of maxLo from org.hibernate.id.SequenceHiLoGenerator class. This class is

responsible for id generation.

The specification of SequenceGenerator class ( http://java.sun.com/javaee/5/docs/api/j ... rator.html ), say that the defaul value of allocationSize is 50.

If the responsible annotation for sequence mapping (@SequenceGenerator(name = "CARGO_SEQ", sequenceName = "cet.num_seq_caet")) don't have the attribute allocationSize, the class SequenceHiLoGenerator assumes 49 for the attribute maxLo.

The class org.hibernate.id.SequenceHiLoGenerator :
Code:

   public synchronized Serializable generate(SessionImplementor session, Object obj)
   throws HibernateException {
      if (maxLo < 1) {
         //keep the behavior consistent even for boundary usages
         long val = ( (Number) super.generate(session, obj) ).longValue();
         if (val == 0) val = ( (Number) super.generate(session, obj) ).longValue();
         return IdentifierGeneratorFactory.createNumber( val, returnClass );
      }
      if ( lo>maxLo ) {
         long hival = ( (Number) super.generate(session, obj) ).longValue();
         lo = (hival == 0) ? 1 : 0;
         hi = hival * ( maxLo+1 );
         if ( log.isDebugEnabled() )
            log.debug("new hi value: " + hival);
      }

      return IdentifierGeneratorFactory.createNumber( hi + lo++, returnClass );
   }

}


Otherwise, if the allocationSize isn't informed, the return value for id in the first persist is hi = 50 * DB sequence value. The next 49 inserts don't call the database sequence, they increment the value of lo and add to hi.

The answare to the problem is to set the optional attribute allocationSize = 0.

Code:
@SequenceGenerator(name = "CARGO_SEQ", sequenceName = "cet.num_seq_caet", allocationSize = 0)


Top
 Profile  
 
 Post subject: Re: [SOLVED] SEQUENCE - entity id with different value from DB
PostPosted: Wed Apr 07, 2010 12:13 pm 
Newbie

Joined: Thu Apr 01, 2010 6:09 pm
Posts: 8
Hi,

For me, I see this happening only with some tables in the db while other tables have the dbseq val as the id value - Am really confused why this is seen only with some tables (the multiplication of dbseq by 50) while others have the correct dbseq value as the id.can anyone please help?


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

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.