Javadb generating sequence of ids with 3 digits

Asked

Viewed 157 times

3

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

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

Why and how useful it is 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: JavaDB

Edit: (I forgot to put persistence.xml)

<?xml version="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>
  • See if it helps you: http://stackoverflow.com/questions/14316187/alter-a-table-column-with-auto-increment-by-1-in-derby

  • I am using Hibernate with JPA, the Hibernate itself generates the table... Do not create the table manually

  • The problem with changing the table by code is that it doesn’t solve the problem... When it is generated again (if it is removed) the problem continues, not to mention that I would have to use this in all tables

1 answer

3


From JPA 2.1 (Java EE 7) the annotation Column won an attribute columnDefinition which 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 would guess that you’re seeing increments of 100 due to preallocation of values. You’re probably not "shutting down" your database, so every time you run your application it allocates a new block.

In accordance with that answer in Soen the two options are:

  1. Uncheck the database (i.e., open a connection to your database by passing the parameter ;shutdown=true before leaving your application).
  2. Setar derby.language.sequence.preallocator=1, see that this potentially worsens competition between sequences.
  • Anthony, for a connection pool where it does not close the connections, only the sessions, the "columnDefinition" would solve the problem?

  • I don’t think so, but the parameter would solve it in exchange for performance. I would recommend shutdown (you can customize your application or your application server shutdown. Alternatively you can run Javadb as a separate server instead of Embedded and shutdown manually)

  • but if I take ;create=true the database is not generated in execution, then how do I do in this case? @Anthonyaccioly

  • I don’t understand your doubt Felipe. Yours persistence.xml may have create=true if the idea is to generate a new database (new physical files) in each run. My suggestion was for you to close the bank when turning off the application (e. g, open a simple JDBC connection with shutdown=true). It would be even better to start Java DB in server mode and later to do a manual shutdown.

  • I never used Javadb, it’s the first time I see this option... So, I kept the same configuration of my persistence.xml that I posted on the question, I only changed the part of the jdbc:derby:Teste01;create=true for jdbc:derby:Teste01;shutdown=true it turns out with this error stating that DB does not exist, and I need DB to be generated if it does not exist, I can not use a normal connection of jdbc, I have to use only Hibernate. (I forgot to put persistence.xml in the question, now I put!!!) @Anthonyaccioly

  • I also tried to put both jdbc:derby:Teste01;create=true;shutdown=true Did not give, it does not create DB automatically, practically ignores this create command

  • Felipe. There is no way out. You need to shutdown to avoid re-reading the log and allocation problems. You can: Shutdown with JDBC DriverManager.getConnection("jdbc:derby:Teste01;shutdown=true"); or open the database in server mode and shutdown manually (see documentation) or try shutdown using the Derby API (documentation). Anyway the answer is persistence.xml with create and something doing shutdown.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.