gpt4 book ai didi

java - 删除包含 @ManyToOne 的实体时出现 JPA 错误

转载 作者:行者123 更新时间:2023-12-02 06:11:57 25 4
gpt4 key购买 nike

所以我正在使用以下两个类:

@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 ...

}

当我尝试使用 EntityManager.remove(calibracao) 删除 Calibraao 实例时,出现以下错误:

[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 ])

有谁知道为什么会发生这种情况吗?

最佳答案

考虑这样做:

  1. 在 Calibracao 实体中将 ParametroStatus 属性设置为 null。
  2. 尝试对 Calibracao 实体执行 em.remove() 操作。

将引用设置为 null 可以解决问题,唯一觉得奇怪的是当您尝试删除实体时出现的更新语句。

关于java - 删除包含 @ManyToOne 的实体时出现 JPA 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21805846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com