BrunoPires
BrunoPires

Reputation: 383

JPA error while removing entity containing @ManyToOne

So I'm working with the two following classes:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING, length = 1)
@Table(name = "calibracao", catalog = "aquitel", schema = "")
@XmlRootElement
public abstract class Calibracao implements Serializable {    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idCalibracao")
    private Long idCalibracao;

    @Basic(optional = false)
    @Column(name = "type")
    private String type;

    @Basic(optional = false)
    @Column(name = "adc")
    private int adc;

    @Basic(optional = false)
    @Column(name = "voltagem")
    private float voltagem;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "calibracao")
    private CalibracaoPoco calibracaoPoco;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "calibracao")
    private CalibracaoRepetidor calibracaoRepetidor;

    @JoinColumn(name = "parametroStatus", referencedColumnName = "idParametro_Status")
    @ManyToOne(optional = false)
    private ParametroStatus parametroStatus;

    //Getters and setters        

}

and

@Entity
@Table(name = "parametro_status", catalog = "aquitel", schema = "")
@XmlRootElement
public class ParametroStatus implements Serializable {        
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idParametro_Status")
    private Integer idParametroStatus;

    @Basic(optional = false)
    @Column(name = "nome")
    private String nome;

    @Column(name = "unidadeMedida")
    private String unidadeMedida;    

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "parametro")
    private List<Status> statusList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "parametroStatus")
    private List<Calibracao> calibracaoList;    

    //Getters and setters

}

The entity objects are instantiated using the following code:

public class AdcCalibratior extends javax.swing.JFrame {

    List<Calibracao> points;

    public AdcCalibrator(){
        //Unrelated code ...
        points = getCalibration(selectedNode, selectedParameter);
    }

    //Unrelated code ...

    private List<Calibracao> getCalibration(Object node, ParametroStatus parameter) {
        if (node instanceof Poco) {
            TypedQuery query = em.createNamedQuery("CalibracaoPoco.findByParametro", CalibracaoPoco.class);
            query.setParameter("poco", (Poco) node);
            query.setParameter("parametro", parameter);
            return query.getResultList();
        } else {
            TypedQuery query = em.createNamedQuery("CalibracaoRepetidor.findByParametro", CalibracaoRepetidor.class);
            query.setParameter("repetidor", (Repetidor) node);
            query.setParameter("parametro", parameter);
            return query.getResultList();
        }
    }

//More unrelated code ...

}

When I try to delete an instance of Calibracao using EntityManager.remove(calibracao) i get the following error:

[EL Warning]: 2014-02-15 22:49:43.27--UnitOfWork(2118956727)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'parametroStatus' cannot be null
Error Code: 1048
Call: UPDATE aquitel.calibracao SET parametroStatus = ? WHERE (idCalibracao = ?)
        bind => [2 parameters bound]
Query: DeleteObjectQuery(unesp.lebac.aquitel.dataLink.enitity.CalibracaoPoco[ idCalibracao=6 ])

Does anyone knows why this happens?

Upvotes: 0

Views: 479

Answers (2)

Koitoer
Koitoer

Reputation: 19533

Consider to do this:

  1. Set to null ParametroStatus property in the Calibracao entity.
  2. Try to em.remove() over that Calibracao Entity.

Set reference to null could solve the problem, the only think that is weird is the update statement that appears when you try to remove the entity.

Upvotes: 1

eduardohl
eduardohl

Reputation: 1216

You cannot delete an instance that is referenced by a foreign key unless you specify a CascadeType. What you could do is to add the correct effect you want on the cascade attribute of your ManyToOne relationship.

Possible values are: http://docs.oracle.com/javaee/6/api/javax/persistence/CascadeType.html

Also, make sure your database table DDL (foreign keys) allows for that deletion.

Upvotes: 1

Related Questions