我的应用程序有一个具有此属性和 JPA 注释的实体类 (Ativo):
@JoinColumn(name = "BOLSA", referencedColumnName = "ID")
@ManyToOne(optional = false, cascade = {CascadeType.PERSIST})
private Bolsa bolsa;
当我尝试持久化实体类(Ativo)时,会引发此异常:
内部异常:java.sql.SQLIntegrityConstraintViolationException:该语句被中止,因为它会导致唯一或主键约束或由“BOLSA”上定义的“SQL131102225757700”标识的唯一索引中的重复键值。
我不明白我的代码有什么问题。如果这应该只是现有对象的外键,为什么还要尝试创建 Bolsa 类型的新对象?
Ativo 类的标题:
@Entity
@Table(name = "ATIVO")
public class Ativo implements EntityInterface<Ativo>, Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID")
private Long id;
@Basic(optional = false)
@Column(name = "CODIGO", unique=true, nullable = false)
private String codigo;
@Basic(optional = false)
@Column(name = "TIPO_MERCADO", nullable = false)
private String tipoMercado;
@Column(name = "DESCRICAO", nullable = false, length = 10000)
private String descricao;
@JoinColumn(name = "BOLSA", referencedColumnName = "ID")
@ManyToOne(optional = false, cascade = {CascadeType.PERSIST})
private Bolsa bolsa;
Bolsa 类的标题:
@Entity
@Table(name = "BOLSA")
public class Bolsa implements EntityInterface<Bolsa>, Serializable, Comparable<Bolsa> {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID")
private Long id;
@Basic(optional = false)
@Column(name = "NOME", unique = true, nullable = false)
private String nome;
@Column(name = "DESCRICAO", nullable=false, length = 10000)
private String descricao;