java 无法执行 JDBC 批量更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5020786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:05:14  来源:igfitidea点击:

Could not execute JDBC batch update

javajpajdbcplayframework

提问by lassejl

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.

我有一个类 Meal,其中可以包含一个或多个 MealEntries。一餐条目可用于多餐(即,如果您在多餐中吃 2 个鸡蛋)。但我有一个例外。

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.

编辑:试图创建一个测试用例,但遇到了其他一些错误。我将尝试解释:当我尝试使用第一餐也使用的 MealEntries 创建第二餐时,会发生异常。

采纳答案by lassejl

Thanks for the feedback, but i figured it out.

感谢您的反馈,但我想通了。

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

我将 Meal 更改为 MealEntry 与 @ManyToMany 的关系,它解决了问题。

回答by jzd

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.

看起来您有一个数据库约束,不允许您插入这些重复项。更新您的数据库结构或找到一种方法来表示当前结构中的膳食。

回答by mandubian

Give us a bit more information because without the requests you perform, it's not really easy to see why you have duplicates...

给我们更多信息,因为如果没有您执行的请求,就很难看出为什么会有重复...