Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8823021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:15:03+00:00 2026-06-14T06:15:03+00:00

please analize the following two codes and tell me why the first one fails

  • 0

please analize the following two codes and tell me why the first one fails with primary key violation when doing commit, and the second one does’t.

Code which fails at commit:

try{

        Query q = em.createQuery("DELETE FROM Puntaje");
        q.executeUpdate();
        //em.getTransaction().commit();

        //em.getTransaction().begin();
        Iterator it = l.iterator();
        while(it.hasNext()){
            DataPuntaje dp = (DataPuntaje)it.next();
            Cliente c = new Cliente(dp.getCliente());
            Puntaje p = new Puntaje(dp.getPuntaje(),c);
            c.agregarPuntaje(p);

            em.merge(c);

        }


   System.out.println("test1");
        em.getTransaction().commit();
   System.out.println("test2");
    }

Code which works fine:

try{

        Query q = em.createQuery("DELETE FROM Puntaje");
        q.executeUpdate();
        em.getTransaction().commit();

        em.getTransaction().begin();
        Iterator it = l.iterator();
        while(it.hasNext()){
            DataPuntaje dp = (DataPuntaje)it.next();
            Cliente c = new Cliente(dp.getCliente());
            Puntaje p = new Puntaje(dp.getPuntaje(),c);
            c.agregarPuntaje(p);

            em.merge(c);

        }


   System.out.println("test1");
        em.getTransaction().commit();
   System.out.println("test2");
    }

The only difference is that the first one does not commit the delete query, but instead commit it all together at the end.
Cliente and Puntaje is a 1:N bidirectional relation with cascade = ALL.
And all the inserted instances of Cliente have the same ID, but merge should be smart enough to update instead of insert after the first one is persisted, but that seems to fail at the first example and i cant find any explanation.
Also im using H2 embedded database.

Also i would like to add, the first code works FINE if there is an already inserted Cliente value, this fails when the table is actually empty and so delete actually is doing nothing.

This is the error im getting:

Internal Exception: org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "PRIMARY_KEY_5 ON PUBLIC.CLIENTE(NICK)"; SQL statement:
INSERT INTO CLIENTE (NICK) VALUES (?) [23505-169]
Error Code: 23505
Call: INSERT INTO CLIENTE (NICK) VALUES (?)
        bind => [cbaldes]
Query: InsertObjectQuery(Clases.Cliente@21cd5b08)
javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.2.v20100323-r6872): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "PRIMARY_KEY_5 ON PUBLIC.CLIENTE(NICK)"; SQL statement:
INSERT INTO CLIENTE (NICK) VALUES (?) [23505-169]
Error Code: 23505
Call: INSERT INTO CLIENTE (NICK) VALUES (?)
        bind => [cbaldes]
Query: InsertObjectQuery(Clases.Cliente@21cd5b08

)

These are the tables:

@Entity
public class Puntaje implements Comparable, Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private int total;

    @ManyToOne(cascade=CascadeType.ALL, optional = false)
    @JoinColumn(name="NICK")
    private Cliente cliente;


@Entity
public class Cliente implements Serializable {

    @Id
    private String nick;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="cliente")
    private List<Puntaje> puntajes;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:15:05+00:00Added an answer on June 14, 2026 at 6:15 am

    When you perform operations on the object, all the operations are recorded in the cache ONLY. JPA will prepare an internal list of all objects to be inserted, updated and deleted. Which will be flushed together when flush or commitis called.

    Now take your first example. You deleted all Puntaje, which adds all Puntaje in the deleted list. Now when you call merge, i*ts indeed smart enough* and it figured out that it should be inserted and not updated and added in the insert list. When you call commit, it tries to insert the objects from the insert list first and as you can expect, it will fail as old objects are not yet deleted.

    Only difference in your second example is that, by force, you are deleting the objects first before insertion and hence it’s not failing.

    I am sure, it will not fail even if your use flush in place of commit.

    Hope this helps you understand the reasoning behind the failure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Please look the following query: SELECT ID, START, END FROM TABLEA the result is:
Can some one please give the explanation. I tried to analyze a lot. but
Please help me somebody to convert unicodestring into string This is how i am
Please let me know what are default locations for Downloads folder on devices with
Please i need help, this code below works fine on my localhost, php5.3+ but
Please see demo here: http://jsfiddle.net/mA6qm/1/ (expand console) Why aren't events being sent or received
Please see here , at the end of the post, the 70px author image
Please bear with me as I am new to dojo, javascript, and web programming
Please help me with this update query. I COMPLETELY understand how redundant this is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.