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

17.8k Views Asked by At

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.

4

There are 4 best solutions below

0
On BEST ANSWER

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

1
On

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

0
On

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.

0
On

what the error says that what you have entered i.e., the key "xxxxxx" is a duplicate value. That value present in your database and you are not saving any duplicate values for that key. To avoid this error you shouldn't give any duplicate values otherwise you must allow the duplicate values to save the data for that "xxxxx" key.