I have a model with 2 tables, Cuentas
and Movimien
. merge()
When the table method is called Movimien
, it tries to perform a insert
on the table Cuentas
. I am not clear if the relations raise them correctly. Could someone give me a hand with this?
Below is the model of the table Movimien
, which when doing the update
triggers the insert
in the table Cuenta
:
@Entity
@Table(name = "movimien")
public class Movimien implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private Date fecha;
private short cuentaid;
private float importe;
private String concepto;
@JoinColumn(name = "cuentaid", insertable = false, updatable = false)
@ManyToOne
private Cuenta cuenta;
public Movimien() {
this.cuenta = new Cuenta();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getFecha() {
return fecha;
}
public void setFecha(Date fecha) {
this.fecha = fecha;
}
public short getCuentaid() {
return cuentaid;
}
public void setCuentaid(short cuentaid) {
this.cuentaid = cuentaid;
}
public float getImporte() {
return importe;
}
public void setImporte(float importe) {
this.importe = importe;
}
public String getConcepto() {
return concepto;
}
public void setConcepto(String concepto) {
this.concepto = concepto;
}
public Cuenta getCuenta() {
return cuenta;
}
public void setCuenta(Cuenta cuenta) {
this.cuenta = cuenta;
}
@Override
public int hashCode() {
int hash = 7;
hash = 53 * hash + this.id;
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Movimien other = (Movimien) obj;
if (this.id != other.id) {
return false;
}
return true;
}}
And the model of the table Cuenta
, which is fired insert
when a is done update
on the table Movimien
:
@Entity
@Table(name = "cuentas")
public class Cuenta implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String nombre;
private short tipo;
@OneToMany(mappedBy="cuenta") //Con mappedBy estamos indicando quién es el dueño de la relación.
private List<Movimien> movimien;
// Constructor - Requerido para lo EJB
public Cuenta() {
this.nombre="";
this.tipo = 0;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public short getTipo() {
return tipo;
}
public void setTipo(short tipo) {
this.tipo = tipo;
}
@Override
public int hashCode() {
int hash = 7;
hash = 97 * hash + this.id;
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Cuenta other = (Cuenta) obj;
if (this.id != other.id) {
return false;
}
return true;
}}
Any idea what could be the reason for this behavior?
Finally I found the correct solution, in the Movement class, replace the following code:
for this:
The problem was very subtle. I hope someone else will find it useful! Cheers