2

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;
5
  • What don't you understand about the error? Commented Nov 3, 2013 at 17:07
  • 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?? Commented Nov 3, 2013 at 17:10
  • 1
    try by removing cascade = {CascadeType.PERSIST} from ManyToOne... Commented Nov 3, 2013 at 17:32
  • Thanks @AhsanShah, removing the cascade it works. You could post this as an answer if you like. Commented Nov 3, 2013 at 17:40
  • 1
    Congrats @ceklock. i will post now with some explanation for your reference. Commented Nov 4, 2013 at 5:57

3 Answers 3

4

The error message is explicit: you're trying to insert a row with an id that already exists in the data base, hence an integrity constraint violation (a duplicate primary key) ensues.

Check your code, the part where the id is being set, and make sure that it uses an unique identifier. Or use annotations in the id field to auto generate a new key each time. Or define a database sequence to do this.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, but why is that happening? It is not me, it is JPA trying to do that, why??
@ceklock it's hard to tell with such little information, post the relevant code (how was the id defined, how are you inserting the row, etc)
As posted by @Ahsan Shah, the real problem was the cascade. Thank you for your answer, it was also of good help.
1

as suggested in the comments, removing cascade = {CascadeType.PERSIST} from the below relation will work.

@JoinColumn(name = "BOLSA", referencedColumnName = "ID")
@ManyToOne(optional = false, cascade = {CascadeType.PERSIST})
private Bolsa bolsa;

The reason is, Cascade Persist take care of getting things put into the database for you via transitive persistence, so you don't have to explicitly persist the parent (in your case 'Bolsa'). However, it is always your responsibility to maintain the in-memory state of the objects before persisting.

I am suspecting (as it is not clear from the provided snips) that you are explicitly persisting Bolsa object before saving Ativo, so the exception is resolved by removing the cascade persist constraint.

Comments

1

The error message gives you enough information to keep digging:

a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL131102225757700'

It states the it is either a primary key or a unique key constraint, and given the unique-constraints indicated by your mappings (and the fact that I can't see anything wrong with the mappings themselves), I would guess that it a unique key constraint violation, for instance the nome field. But, the error message gives us something more, and that is the name of the constraint that is being violated: SQL131102225757700. So, what you need to do is to figure out which column this constraint applies to, and once you do I'm guessing you will be a lot closer to figuring out the error. (Exactly how to do this depends on your database provider, google it..)

1 Comment

As posted by @Ahsan Shah, the real problem was the cascade. Thank you for your answer, it was also of good help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.