相关文章推荐
正直的铁链  ·  Android开发之android:proc ...·  1 年前    · 
近视的茶壶  ·  经典卷积网络之Resnet50·  1 年前    · 
强悍的牛排  ·  Delphi中array of ...·  1 年前    · 
失恋的瀑布  ·  VSCode中git上传遇到 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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 {
@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 {
@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.

If you mean @GeneratedValue(strategy = GenerationType.AUTO) ? Unfortunately that does not change anything. The same error occurs. – user10193029 Aug 14, 2018 at 16:23

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.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.