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!