0

I have this model with 2 tables, "Cuenta" and "Movimien". When the merge () method of the Movimien table is called, it also attempts to insert into the Cuenta table. I am not sure if relationships are right. Could someone give me a hand with this?

Below, the model of the Movimien table, which when doing the update triggers the insert in the Cuenta table:

@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 Cuenta table, to which the insert is triggered when an update is made in the Movimien table:

@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")  
  private List<Movimien> movimien;

  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 may be the reason for this behavior?, thanks in advance! Fernando

Persitence.xml

enter image description here

Database

enter image description here

Log of update and insert

Loggin

  • What behavior? What do you expect? – Andronicus Aug 28 '19 at 11:48
  • Please provide the thrown exception – Victor Calatramas Aug 28 '19 at 12:00
  • I think you are misusing updatable and insertable. Those refer to the column, not the linked entity. See https://stackoverflow.com/a/3807285/7346454. – Thomas Martin Aug 28 '19 at 12:07
  • Can you add the code where you merge and the SQL code which indicates the wrong insert? One cause could be the usage of `int` instead of `Integer` for the ID. – Tobias Liefke Aug 28 '19 at 14:19
  • By default operations don't cascade. That means you either have cascades configured elsewhere, or somewhere in your code you are manually saving or merging "Cuenta". Please kindly share the relevant code that deals with loading and (re-)persisting/merging in this operation. – Thomas Timbul Aug 29 '19 at 15:08
  • Thank you for your kindly answers.....I again tried to found the problem, but I could not....I add logging to see what happens and add more info. Thanks! – Fernando Rene Aug 29 '19 at 21:39

0 Answers0