在数据库Java Derby中插入新数据时生成的ID出错
我正在使用JavaDerby构建一个应用程序,该应用程序具有在数据库中插入新用户的功能 一切都进行得很顺利,但当我关闭应用程序时,我再次运行它,并尝试插入一个新用户(在数据库中称为USUARIO)。该用户生成的新ID比上次运行时生成的新ID多100倍 这是它向我展示的输出控制台:在数据库Java Derby中插入新数据时生成的ID出错,java,sql,database,derby,Java,Sql,Database,Derby,我正在使用JavaDerby构建一个应用程序,该应用程序具有在数据库中插入新用户的功能 一切都进行得很顺利,但当我关闭应用程序时,我再次运行它,并尝试插入一个新用户(在数据库中称为USUARIO)。该用户生成的新ID比上次运行时生成的新ID多100倍 这是它向我展示的输出控制台: 1)Nombre NombreEmail email 2)Nombre NombreEmail emailt 3)Nombre NombreEmail emailte 4)Nombre NombreEmail emai
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 + " ]";
}
}
我不知道这里会发生什么
有什么帮助吗?谢谢大家! 如果你没有关闭德比,这可能会发生。看
您可以通过添加
来关闭;shutdown=true
到您的JDBC URL。您为什么关心这个问题?这是一个技术ID。重要的是它是唯一的。因为当我试图用for数组显示整个用户列表时。当我试着处理这个问题时,它不会向我显示100个位置的所有用户(i@JBNizet你能为我提供这些问题的解决方案吗?我不明白问题可能是什么。你使用查询选择用户,这样你就得到了一个列表。列表中用户的ID不会改变你在列表中迭代的方式。如果你有具体的问题,那么编辑你的问题,精确地描述它,然后发布代码。Dupl副本