所以我正在使用以下两个类:
@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
}
和
@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
}
使用以下代码实例化实体对象:
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 ...
}
当我尝试使用Calibracao
删除EntityManager.remove(calibracao)
的实例时,我收到以下错误:
[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 ])
有谁知道为什么会这样?
答案 0 :(得分:1)
除非指定CascadeType,否则无法删除外键引用的实例。你可以做的是在ManyToOne关系的cascade属性上添加你想要的正确效果。
可能的值有:http://docs.oracle.com/javaee/6/api/javax/persistence/CascadeType.html
另外,请确保您的数据库表DDL(外键)允许删除。
答案 1 :(得分:1)
考虑这样做:
将引用设置为null可以解决问题,唯一的想法是当您尝试删除实体时出现的更新语句。