JPA: SQL Error: 1062, SQLState: 23000 ERROR: Duplicate entry:

error 1062 sqlstate 23000 er_dup_entry
error 23000
mysql error 1062
mysql connector errors integrityerror 1062 23000 duplicate entry 3 for key primary
error 1062 23000 at line 1 duplicate entry root for key 'primary
duplicate entry for key
duplicate entry '' for key email
duplicate entry for key 'primary

i'm trying to teach myself a little bit Java and JPA and now is the point where nothing happens since days.

I have two entities (ITEM and ITEMLIST) linked by a OneToMany relationship.

I persisted the ITEM to the database separately.

The Goal is to get a table with a primary key of the ITEMLIST with the foreign keys of the ITEMS.

But when I save a second ITEMLIST the "duplicate..." error occurs.

WARN: SQL Error: 1062, SQLState: 23000
ERROR: Duplicate entry '1' for key 'xxxxx'
Information: HHH000010: On release of batch it still contained JDBC statements
Information: ERROR: javax.persistence.RollbackException: Error while committing the transaction

When I start the app and put an ITEM in the ITEMLIST that was befor in a ITEMLIST, the error arises in the ITEMLIST_ITEM table.

My ITEM entity:

@Entity
public class Item implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

private String name;
private String description;
private double price;

public Item(String name, String description, double price) {
    this.name = name;
    this.description = description;
    this.price = price;
}

My ITEMLIST entity:

@Entity
public class ItemList implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@OneToMany
private Set<Item> itemInList;

public ItemList() {
    itemInList = new HashSet<>();
}

My methods to persist the entities:

public void saveItem(Item item) {
    EntityManager em = EntityFactory.getEntityManager();       
    try {
        em.getTransaction().begin();      
        em.persist(item);                      
        em.getTransaction().commit();
    } catch (EntityExistsException e) {
        System.out.println(e);
    } finally {
        em.close();
    }
}

public void saveItemList(ItemList itemlist) {
    EntityManager em = EntityFactory.getEntityManager();
    try {
        em.getTransaction().begin();                           
        em.merge(itemlist);
        em.getTransaction().commit();
    } catch (EntityExistsException e) {
        System.out.println(e);
    } finally {
        em.close();
    }
}

Help is welcome, even if it's the code in generell.

You need save in database with a unique ID. Probably, your primary key is repeating when save that in database. Try do a auto increment id

Duplicate Entry Exception: Spring Hibernate/JPA cascade save , I get duplicate key violation. Stack trace: org.hibernate.engine.jdbc.spi.​SqlExceptionHelper logExceptions WARN: SQL Error: 1062, SQLState: 23000 Feb 18,  WARNING: SQL Error: 1062, SQLState: 23000 May 14, 2013 4:35:23 PM org.hibernate.util.JDBCExceptionReporter logExceptions SEVERE: Duplicate entry '2' for key 'navigation_id' May 14, 2013 4:35:23 PM org.hibernate.event.def.AbstractFlushingEventListener performExecutions SEVERE: Could not synchronize database state with session

This could be because when coming to saving your ItemSet, your are trying to persist same Item values of this Set, which you can't do.

Resolving hibernate Duplicate Entry Issue, Exception stack trace from duplicate entry key issue: 12:37:30,969 WARN [main] logExceptions(38) - SQL Error: 1062, SQLState: 23000 PLESK_ERROR: DB query failed: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'X' for key 'PRIMARY', query was: replace <table_name> set When installing a custom instance of WordPress at Domains > example.com > WordPress > Install > Install (Custom), the operation loads continuously and no errors appear.

I solved this problem by changing the relationship to @ManyToMany. In my case it works.

JPA: SQL Error: 1062, SQLState: 23000 ERROR: Duplicate entry:, WARN: SQL Error: 1062, SQLState: 23000 ERROR: Duplicate entry '1' for key '​xxxxx' Information: HHH000010: On release of batch it still  "SQL Error: 1062, SQLState: 23000 2008-08-21 11:54:33,768 ERROR [org.hibernate.util.JDBCExceptionReporter] Duplicate entry '1-0' for key 1" I dosen't occur always.In that table only ID is primary key and auto increment.and i have one unique field.

WARNING: SQL Error: 1062, SQLState , WARNING: SQL Error: 1062, SQLState: 23000 SEVERE: Duplicate entry '*' for key '************'. May 14, 2013 4:35:23 PM org.hibernate.util. Pushed into 5.0.76 (revid:joro@sun.com-20090105160414-8q9j4bi1klkfwiup) (version source revid:azundris@mysql.com-20081230114734-nmsc37ak330zlygn) (merge vers: 5.0.76) (pib:6)

mysql: Spring JPA OneToMany Duplicate Entry MySQLConstraint , SqlExceptionHelper : SQL Error: 1062, SQLState: 23000 2018-05-01 21:10:​12.154 ERROR 3493 --- [nio-8080-exec-9] o.h.engine.jdbc.spi. • ERROR 1062 (23000): Duplicate entry '31592' for key 'PRIMARY' Adarsh Sharma: 28 Apr • Re: ERROR 1062 (23000): Duplicate entry '31592' for key 'PRIMARY' Adarsh Sharma: 28 Apr • Re: ERROR 1062 (23000): Duplicate entry '31592' for key 'PRIMARY' misiaq: 28 Apr

How to do an UPSERT with Hibernate and MySQL and avoid , SQLIntegrityConstraintViolationException: Duplicate entry SqlExceptionHelper] - SQL Error: 1062, SQLState: 23000 03:16:17,740 ERROR  Duplicate entry ‘%s’ for key %d. Duplicate entry ‘1’ for key ‘PRIMARY’ The data you are trying to insert is already present in the column primary key. The primary key column is unique and it will not accept the duplicate entry. You are trying to add a column to an existing table which contains data and set it as unique.

Comments
  • Have a look at this, perhaps of some help!
  • After changing from @OneToMany to @ManyToMany everything looks good at the moment
  • If you mean @GeneratedValue(strategy = GenerationType.AUTO) ? Unfortunately that does not change anything. The same error occurs.