Could not execute JDBC batch update

14,067

Solution 1

Thanks for the feedback, but i figured it out.

I changed my Meal to MealEntry relation to @ManyToMany and it solved the problem.

Solution 2

Looks like you have a database constraint that does not allow you to insert these duplicates. Update your database structure or find a way to represent the meal in the current structure.

Share:
14,067

Related videos on Youtube

lassejl
Author by

lassejl

Codes code

Updated on June 04, 2022

Comments

  • lassejl
    lassejl almost 2 years

    i have a little trouble while making my first play application.

    I got a class Meal which can have one or more MealEntries in it. One meal entry can be used by several meals (i.e if u eat 2 eggs for more than one meal). But im getting a exception.

    I can understand why i get the exception, but i cant figure how i can cure it.

    This is my exception:

    Caused by: java.sql.BatchUpdateException: Duplicate entry '4' for key 'mealEntries_id'
        at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2020)
        at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1451)
        at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
        ... 12 more
    

    Meal Class:

    public class Meal extends Model {
    public String name;
    public Date consumed;
    
    @OneToMany
    public List<MealEntry> mealEntries;
    }
    

    MealEntry Class:

    public class MealEntry extends Model {
    public int amount;
    
    @OneToOne
    public Unit unit;
    
    @OneToOne
    public FoodType type;
    
    @OneToOne
    public MealEntry with;
    }
    

    Thanks in advance

    Edit: Tried to create a testcase, but ran into some other error. I'll try to explain: The exception happens when i try to create a second meal, using MealEntries that are also used by the first Meal.