JavaDB generating sequence of ids with 3 digits

3

I'm trying to learn Hibernate with JPA, I did a test to write to the JavaDB database and realized that the id autoincrement instead of generating an initial sequence of one digit (1,2,3,4 ...) is generating a sequence which starts at 1 and jumps to 101, 201, 301, 401 ... That is, with each new record it adds 100 to the ID.

How to make autoincrement normal, generating 1,2,3,4?

Why and how useful is it to generate an id with this sequence?

Client Class

@Entity
public class Cliente {

    @Id
    @GeneratedValue
    private int id;
    @Column(nullable=false, length=100)
    private String nome;
    @Column(nullable=false, length=250)
    private String endereco;
    @Column(nullable=false, length=8)
    private int rg;
    @Column(nullable=false, length=12)
    private int cpf;
    @Temporal(TemporalType.DATE)
    private Date date;
    @Temporal(TemporalType.TIME)
    private Date time;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getEndereco() {
        return endereco;
    }

    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }

    public int getRg() {
        return rg;
    }

    public void setRg(int rg) {
        this.rg = rg;
    }

    public int getCpf() {
        return cpf;
    }

    public void setCpf(int cpf) {
        this.cpf = cpf;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

}

The test in the Main class

public class Main {

    public static void main(String args[]){
        System.out.println("Worked!");
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("teste");

        Cliente cliente = new Cliente();
        cliente.setNome("Joao");
        cliente.setCpf(58756456);
        cliente.setDate(new Date());
        cliente.setTime(new Date());
        cliente.setRg(4568);
        cliente.setEndereco("Rua dos anjos n 666");
        EntityManager entityManager = factory.createEntityManager();
        entityManager.getTransaction().begin();
        entityManager.persist(cliente);
        entityManager.getTransaction().commit();
        entityManager.close();
    }
}

Table Screenshot in JavaDB:

Edit:(Iforgotaboutpersistence.xml)

<?xmlversion="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="teste" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <properties>
      <property name="dialect" value="org.hibernate.dialect.DerbyDialect"/>
      <property name="connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format_sql" value="true"/>
      <property name="hibernate.hbm2ddl.auto" value="create"/>
      <!--  atualiza o banco, gera as tabelas se for preciso -->
      <property name="javax.persistence.jdbc.url" value="jdbc:derby:Teste01;create=true"/>
      <!--<property name="javax.persistence.jdbc.user" value="root"/> -->
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
      <!-- <property name="javax.persistence.jdbc.password" value=""/> -->
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
    </properties>
  </persistence-unit>
</persistence>
    
asked by anonymous 17.06.2016 / 05:10

1 answer

3

From JPA 2.1 (Java EE 7) the annotation Column has gained a columnDefinition attribute that can be used to change the definition of a field. Example:

@Id
@GeneratedValue
@Column(name="ID", columnDefinition="INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1")
private int id;

But in the case of Derby (JavaDB is a variation of Derby packaged by Oracle) incrementing by 1 is the default (see official documentation )

I'd say that you're seeing increments of 100 due to pre-indexing . You probably are not "shutting down" your database, so each time you run your application it allocates a new block.

As this answer in SOen both options are:

  • Disconnect the database (ie open a connection to your database by passing the ;shutdown=true parameter before exiting your application).
  • Set derby.language.sequence.preallocator=1 , see that this potentially worsens the competition between sequences.
  • 17.06.2016 / 12:25