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();
}
}
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
– adelmo00
I am using Hibernate with JPA, the Hibernate itself generates the table... Do not create the table manually
– felipe.rce
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
– felipe.rce