【问题标题】:Error with the ID generated when i insert new data in my database Java Derby在我的数据库 Java Derby 中插入新数据时生成的 ID 错误
【发布时间】:2020-04-08 07:56:03
【问题描述】:

我正在构建一个应用程序,该应用程序具有使用 Java Derby 在数据库中插入新用户的功能。

一切都很顺利,但是当我关闭应用程序时,我再次运行它并尝试插入一个新用户(在数据库中称为 USUARIO),用户生成的新 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 + " ]";
    }

}

我不知道这里会发生什么。

有什么帮助吗?谢谢!

【问题讨论】:

  • 你为什么在乎?这是一个技术ID。重要的是它是独一无二的。
  • 因为当我尝试使用 for 数组显示整个用户列表时。当我尝试使用 (i
  • @JBNizet 你能为我提供这些问题的解决方案吗?
  • 我不明白问题可能是什么。您使用查询选择用户,因此您得到一个 List。列表中用户的 ID 不会改变您遍历列表的方式。如果您有具体问题,请编辑您的问题,准确描述,然后发布代码。

标签: java sql database derby


【解决方案1】:

当您没有关闭 Derby 时,可能会发生这种情况。见derby.language.sequence.preallocator

您可以通过将;shutdown=true 添加到您的 JDBC URL 来关闭。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多