Java 无法执行 JDBC 批量更新:线程“main”org.hibernate.exception.ConstraintViolationException 中的异常:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21326931/
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
Could not execute JDBC batch update:Exception in thread "main" org.hibernate.exception.ConstraintViolationException:
提问by Reddemma N
I am new to hibernate programming Please help me out, I have problem while executing the below files.I am using MySQL Database. My error is
我是 hibernate 编程的新手 请帮助我,我在执行以下文件时遇到问题。我正在使用 MySQL 数据库。我的错误是
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinderfor further details. Hibernate: insert into UserDetails (userName) values (?) Hibernate: insert into UserDetails (userName) values (?) Hibernate: insert into UserDetails (userName) values (?) Hibernate: insert into vehicle (vehiName) values (?) Hibernate: insert into vehicle (vehiName) values (?) Hibernate: insert into UserDetails_vehicle (UserDetails_userID, vehi_vehiID) values (?, ?) Hibernate: insert into UserDetails_vehicle (UserDetails_userID, vehi_vehiID) values (?, ?) Hibernate: insert into UserDetails_vehicle (UserDetails_userID, vehi_vehiID) values (?, ?)
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:188) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133) at com.annt.java.UserTest.main(UserTest.java:34) Caused by: java.sql.BatchUpdateException: Duplicate entry '1' for key 'vehi_vehiID' at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1669) at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1085) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) ... 8 more
SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。SLF4J:默认为无操作 (NOP) 记录器实现 SLF4J:有关更多详细信息,请参阅http://www.slf4j.org/codes.html#StaticLoggerBinder。Hibernate:插入 UserDetails (userName) 值 (?) Hibernate:插入 UserDetails (userName) 值 (?) Hibernate:插入 UserDetails (userName) 值 (?) Hibernate:插入 Vehicle (vehiName) 值 (?) Hibernate:插入车辆 (vehiName) 值 (?) 休眠:插入 UserDetails_vehicle (UserDetails_userID, vehi_vehiID) 值 (?, ?) Hibernate:插入 UserDetails_vehicle (UserDetails_userID, vehi_vehiID) 值 (?, ?) vehi_vehiID) 值 (?,
线程“main” org.hibernate.exception.ConstraintViolationException 中的异常:无法在 org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96) 处执行 JDBC 批量更新在 org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper. java:66) 在 org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275) 在 org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268) 在 org.hibernate.engine.ActionQueue.executeActions(ActionQueue) .java:188) 在 org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) 在 org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) 在 org.hibernate.impl.会话实现在 org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383) 在 org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133) 在 com.annt.java.UserTest 的刷新(SessionImpl.java:1216) .main(UserTest.java:34) 引起:java.sql.BatchUpdateException:com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1669) 处的键“vehi_vehiID”的重复条目“1”。 jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1085) 在 org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) 在 org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher) ...8:2更多的com.annt.java.UserTest.main(UserTest.java:34) 上的 commit(JDBCTransaction.java:133) 由:java.sql.BatchUpdateException:com.mysql.jdbc 上的键“vehi_vehiID”的重复条目“1” .PreparedStatement.executeBatchSerially(PreparedStatement.java:1669) at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1085) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) ... 8 更多com.annt.java.UserTest.main(UserTest.java:34) 上的 commit(JDBCTransaction.java:133) 由:java.sql.BatchUpdateException:com.mysql.jdbc 上的键“vehi_vehiID”的重复条目“1” .PreparedStatement.executeBatchSerially(PreparedStatement.java:1669) at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1085) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) ... 8 更多PreparedStatement.executeBatch(PreparedStatement.java:1085) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) ...PreparedStatement.executeBatch(PreparedStatement.java:1085) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) ...
UserDetails.java
用户详细信息.java
@Entity
public class UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int userID;
@OneToMany
private List<vehicle> vehi = new ArrayList<vehicle>();
public List<vehicle> getVehi() {
return vehi;
}
public void setVehi(List<vehicle> vehi) {
this.vehi = vehi;
}
public int getUserID() {
return userID;
}
public void setUserID(int userID) {
this.userID = userID;
}
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
vehicle.java
车辆.java
@Entity
public class vehicle {
@Id
@GeneratedValue
private int vehiID;
public int getVehiID() {
return vehiID;
}
public void setVehiID(int vehiID) {
this.vehiID = vehiID;
}
public String getVehiName() {
return vehiName;
}
public void setVehiName(String vehiName) {
this.vehiName = vehiName;
}
private String vehiName;
}
usertest.java
用户测试.java
public class UserTest {
public static void main(String[] args) {
vehicle veh1 = new vehicle();
veh1.setVehiName("car");
vehicle veh2 = new vehicle();
veh2.setVehiName("bus");
UserDetails user1 = new UserDetails();
UserDetails user2 = new UserDetails();
user1.setUserName("user2");
user2.setUserName("user2");
UserDetails user3 = new UserDetails();
user3.setUserName("user3");
user1.getVehi().add(veh1);
user2.getVehi().add(veh2);
user3.getVehi().add(veh1);
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session ss = sf.openSession();
ss.beginTransaction();
ss.save(user1);
ss.save(user2);
ss.save(user3);
ss.save(veh1);
ss.save(veh2);
ss.getTransaction().commit();
ss.close();
}
}
采纳答案by JB Nizet
The error message is quite clear: there is a unique key violation constraint: you're inserting the same vehicle for two different users:
错误消息非常清楚:有一个唯一的密钥违规约束:您为两个不同的用户插入相同的车辆:
user1.getVehi().add(veh1);
//...
user3.getVehi().add(veh1);
That is incorrect, since the association between User and Vehicle is a OneToMany association. A given Vehicle thus belongs to one and only one User. If you want to share a vehicle between users, then you need a ManyToMany association, and you need to remove the unique constraint on UserDetails_vehicle.vehi_vehiID
on (Hibernate won't create it if you use it to generate the schema)
这是不正确的,因为 User 和 Vehicle 之间的关联是 OneToMany 关联。因此,给定的车辆属于一个且仅一个用户。如果您想在用户之间共享车辆,那么您需要一个多对多关联,并且您需要删除对UserDetails_vehicle.vehi_vehiID
上的唯一约束(如果您使用它来生成架构,Hibernate 不会创建它)
回答by cLyric
You're affecting veh1 to 2 differents users, so you need a Many2Many association, not a One2Many :
您将 veh1 影响到 2 个不同的用户,因此您需要一个 Many2Many 关联,而不是 One2Many :
@OneToMany
private List vehi=new ArrayList();
@OneToMany
private List vehi=new ArrayList();
Many2Many : One User can be affected to Many Vehicules And One Vehicule can be affected to Many Users One2Many : One User can be affected to Many Vehicules, but One Vehicule can be only owned by one User
Many2Many : 一个用户可以影响多辆汽车,一辆汽车可以影响很多用户 One2Many : 一个用户可以影响多辆汽车,但一辆汽车只能由一个用户拥有
That's why you got an integrity exception
这就是为什么你有一个完整性例外
回答by ramkumar13579
The object values must be different while doing relation ship give the different value in user3.getVehi().add(veh1)
对象值必须不同,同时在 user3.getVehi().add(veh1) 中给出不同的值