Reputation: 463
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.
Upvotes: 2
Views: 14764
Reputation: 463
Thanks for the feedback, but i figured it out.
I changed my Meal to MealEntry relation to @ManyToMany and it solved the problem.
Upvotes: 2
Reputation: 4427
Give us a bit more information because without the requests you perform, it's not really easy to see why you have duplicates...
Upvotes: 0
Reputation: 23629
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.
Upvotes: 1