My app has an entity class (Ativo) with this attribute and JPA annotations:
@JoinColumn(name = "BOLSA", referencedColumnName = "ID")
@ManyToOne(optional = false, cascade = {CascadeType.PERSIST})
private Bolsa bolsa;
When I try to persist the entity class (Ativo), this exception is thrown:
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL131102225757700' defined on 'BOLSA'.
I don't understand what is wrong with my code. Why is it trying to create a new object of type Bolsa if this should be just a foreign key to an existing object??
Header of class 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;
Header of class 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;
cascade = {CascadeType.PERSIST}fromManyToOne...