Skip to content
Advertisement

Error with the ID generated when i insert new data in my database Java Derby

i am building an app with a function to insert new Users in the database with Java Derby.

Everything is going well, but when i close the app, i run it again and i try to insert a new User (called USUARIO in the DB) the new ID generated of the User is one hundred times more than the last one on the last run.

This is the output console that it shows me:

1)Nombre NombreEmail email
2)Nombre NombreEmail emailt
3)Nombre NombreEmail emailte
4)Nombre NombreEmail emailteet
5)Nombre Email tetete
6)Nombre Email tetetetetetet
7)Nombre Email tetetetetetettetetet
8)Nombre Email tetetetetetetteteteteeeeee
9)Nombre Email 

// At this moment i closed the app, and run it again...
// The new Register ID is starting up 100!

101)Nombre LuisEmail kik196@hotmail.com
102)Nombre Email ghjghjghjghjghjg
103)Nombre Email ghjghjghjjytyutu
104)Nombre Email ghjghjghjjytyututyrtytryr

This is the code that i am creating new data:

public void createUser(String nombre, String email, String apellido, String cedula, String telefono, String contraseña) {

        Usuario usuarioNuevo = new Usuario(email, nombre, apellido, contraseña, cedula, telefono);
        em.getTransaction().begin();
        em.persist(usuarioNuevo);
        em.getTransaction().commit();
    }

This is my entity Usuario class:

public class Usuario implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "EMAIL")
    private String email;
    @Basic(optional = false)
    @Column(name = "NOMBRES")
    private String nombres;
    @Basic(optional = false)
    @Column(name = "APELLIDOS")
    private String apellidos;
    @Basic(optional = false)
    @Column(name = "CONTRASEu00d1A")
    private String contraseña;
    @Column(name = "CEDULA")
    private String cedula;
    @Column(name = "TELEFONO")
    private String telefono;

    public Usuario(){

    }

    public Usuario(String email, String nombres, String apellidos, String contraseña, String cedula, String telefono) {
        this.email = email;
        this.nombres = nombres;
        this.apellidos = apellidos;
        this.contraseña = contraseña;
        this.cedula = cedula;
        this.telefono = telefono;
    }

    public Usuario(Integer id) {
        this.id = id;
    }

     ....

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Usuario)) {
            return false;
        }
        Usuario other = (Usuario) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "ComandosSQL.sql.Usuario[ id=" + id + " ]";
    }

}

I dont know what could be happening here.

Any help? thank you!

Advertisement

Answer

This could happen when you hadn’t shut down Derby. See derby.language.sequence.preallocator.

You can shut down by adding ;shutdown=true to your JDBC URL.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement