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();
}
}
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>