簡體   English   中英

在我的數據庫 Java Derby 中插入新數據時生成的 ID 錯誤

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

我正在構建一個應用程序,該應用程序具有使用 Java Derby 在數據庫中插入新用戶的功能。

一切都很順利,但是當我關閉應用程序時,我再次運行它並嘗試插入一個新用戶(在數據庫中稱為 USUARIO),該用戶生成的新 ID 是最后一個用戶 ID 的一百倍跑。

這是它向我展示的輸出控制台:

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

這是我正在創建新數據的代碼:

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

這是我的實體 Usuario 類:

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 = "CONTRASE\u00d1A")
    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 + " ]";
    }

}

我不知道這里會發生什么。

有什么幫助嗎? 謝謝你!

這可能發生在您沒有關閉 Derby 時。 請參閱derby.language.sequence.preallocator

您可以通過將;shutdown=true添加到您的 JDBC URL 來;shutdown=true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM