报错Could not execute JDBC batch update和java.sql.BatchUpdateException: Duplicate e
最近学hibernate,写了一段代码,第一次执行没问题,第二次执行报错了,想了一下,不应该是其他毛病,且报错是更新(update)出错,估计是第二次执行的时候刚好与第一次执行的结果出现了矛盾,于是去数据库将第一次执行的结果(主码)改了一下,再执行就不会报错了,呵呵(主要原因还是我在配置文件中对Department类和Employee类的id设置了唯一性)
package cn.itcast.hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import cn.itcast.hibernate.HibernateUtils;
import cn.itcast.hibernate.domain.Department;
import cn.itcast.hibernate.domain.Employee;
public class Many2One {
public static void main(String[] args){
add();
}
static Department add(){
Session s = null ;
Transaction tx = null;
try{
Department depart = new Department();
depart.setName("depart name");
Employee emp = new Employee();
emp.setDepart(depart);//因为这一行代码,将Employee和Department关联起来了
emp.setName("emp name");
s = HibernateUtils.getSession();
tx = s.beginTransaction();
s.save(depart);//先保存了depart
s.save(emp);//后保存emp,从而保证了emp中的Department不是空的
tx.commit();
return depart;
}finally{
if(s != null)
s.close();
}
}
}
分享到:
相关推荐
本人做系统时遇到的一些Hibernate问题及解决方案。
Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean) on project
spring-petclinic-master运行mvn install报错:Failed to execute goal io.spring.javaformat:spring-javaformat-maven-plugin:0.0.19:validate (default) on project spring-petclinic: Formatting violations ...
idea创建Maven项目时,报错显示Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.0.2:resources,并且Maven插件内看不到 mybatis-generator。如下图: 折腾了好久发现配置放错地方了,...
Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update 或者org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not insert: ...
在<plugins></plugins>中添加 <groupId>org.apache.maven.plugins <artifactId>maven-compiler-plugin <version>3.8.1 <source>1.8 <target>1.8 </plugin>
以下的问题是由于java的堆内存已满,需要java运行时加大java的堆内存空间 2019-10-09 18:02:32.858 [http-nio-8239-exec-6] ERROR c.a.b.c.exceptionHandler.CodeBaseExceptionHandler:69 - Handler dispatch failed...
类google FS,不是通用的文件系统,只能通过专有API访问,目前提供了C、Java和PHP API 为互联网应用量身定做,解决大容量文件存储问题,追求高性能和高扩展性 FastDFS可以看做是基于文件的key value pair存储系统,...
1、JDBC(Java Database Connection):java连接数据库统一接口API,底层主要通过直接的JDBC驱动和 JDBC-ODBC桥驱动实现与数据库的连接。 1>.JDBC驱动程序类型: <1>.JDBC-ODBC桥加ODBC驱动程序:需要ODBC驱动,适合...
could not execute:bad executable format(win32 error 193) 下了一个实现MD5的代码,编译时没有错误,但是执行的时候却提示:could not execute:bad executable format(win32 error 193)
java.lang.SecurityException: class "org.apache.commons.collections.SequencedHashMap"'s signer information does not match signer information of other classes in the same package at java.lang....
2. JDBC用到的类库基本都位于java.sql.*包中,程序中引入该包: Import java.sql.*; 3. 添加要用的数据库中的包,找到数据库中的Driver.class文件: 项目名上点右键,Build Path—Add External Archives… 构建路径...
thingsboard编译错误 Failed to execute goal com.github.eirslett:frontend-maven-plugin:1.0:npm (npm install) on project js-executor: Failed to run task: 'npm install' failed. 该错误如果是3.0上的,请将...
return super.executeSql(sql, user.getUsername(),user.getSex(),user.getAge(),user.getTellphone(),user.getAddress(),user.getPermission(),user.getPassword(),user.getUserno()); } //删除 public ...
pit1 spring boot之 maven install 报错[Maven][ERROR] Failed to execute goal org.spri
y2ssh.sg.chp1.action.AddAction.execute(AddAction.java:18) y2ssh.sg.chp1.framework.Controller.doGet(Controller.java:25) y2ssh.sg.chp1.framework.Controller.doPost(Controller.java:33) javax.servlet....
关于Java中内存溢出的解决办法 关于Java中内存溢出的解决办法
ExecuteSQL.java
主要介绍了Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1的解决办法的相关资料,需要的朋友可以参考下
3.java -Djdbc.drivers=oracle.jdbc.driver.OracleDriver jdbc.drivers=System.gerProperties(); 2)获得Connection 1.conn = DriverManager.getConnection(url.userName,passwd); 2.driver.conn(url,properties); 3...