我有一个错误:javax.persistence.PersistenceException:
org.hibernate.exception.ConstraintViolationException:无法执行JDBC批量更新
我通过entiyManager将我的对象持久化到数据库,代码如下:
public void addPrincipal(Principal u) {
trns.start();
trns.getManager().persist(u);
trns.commit();
}
trns是我自己编写的事务类的对象,这里是使用方法的代码:
public void start() {
EntityManager manager = managerFactory.createEntityManager();
manager.getTransaction().begin();
entityManager.set(manager);
}
public void commit() {
EntityManager manager = entityManager.get();
if (manager == null || !manager.getTransaction().isActive()) {
throw new IllegalStateException("No transaction is in progress");
}
manager.getTransaction().commit();
manager.clear();
}
public EntityManager getManager() {
EntityManager manager = entityManager.get();
if (manager == null) {
start();
manager = entityManager.get();
}
if (!manager.getTransaction().isActive()) {
manager.getTransaction().begin();
}
return entityManager.get();
}
Principal映射类如下所示:
@Entity
@Table(name = "principal")
public class Principal implements Serializable {
@Id
@Column(name = "id_principal")
private Long id_principal;
@Column(name = "name")
private String name;
@Column(name = "surname")
private String surname;
@Column(name = "designation")
private String designation;
@Column(name = "address")
private String address;
@Column(name = "telephone")
private String telephone;
@Column(name = "fax")
private String fax;
@Column(name = "email")
private String email;
@Column(name = "legal_form")
private String legal_form;
@Column(name = "krs")
private String krs;
@Column(name = "authority")
private String authority;
@Column(name = "pesel_regon")
private String pesel_regon;
@Column(name = "nip")
private String nip;
@Column(name = "stock")
private float stock;
@OneToOne(mappedBy = "principal", cascade = CascadeType.ALL)
private RepresentationPerson representation_person;
@OneToOne(mappedBy = "principal", cascade = CascadeType.ALL)
private ContactPerson contact_person;
@OneToMany(mappedBy = "principal")
private Set<StudyCommisioned> study_commisioned;
从数据库获取记录不是问题。当我想将新记录保存到数据库时,唯一的错误就出现了
这是完整的堆栈跟踪。对不起,我忘记了:
com.vaadin.server.ServerRpcManager$RpcInvocationException: Unable to invoke method click in com.vaadin.shared.ui.button.ButtonServerRpc
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:172)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118)
at com.vaadin.server.AbstractCommunicationManager.handleBurst(AbstractCommunicationManager.java:1678)
at com.vaadin.server.AbstractCommunicationManager.handleVariables(AbstractCommunicationManager.java:1564)
at com.vaadin.server.AbstractCommunicationManager.handleUidlRequest(AbstractCommunicationManager.java:582)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:315)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:201)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:170)
... 26 more
Caused by: com.vaadin.event.ListenerMethod$MethodException: Invocation of method buttonClick in pl.edu.utp.badania.ui.innercomponents.AddPrincipal$1 failed.
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:528)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:167)
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:984)
at com.vaadin.ui.Button.fireClick(Button.java:367)
at com.vaadin.ui.Button$1.click(Button.java:56)
... 31 more
Caused by: javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:93)
at pl.edu.utp.badania.postgresql.PgSQLTransaction.commit(PgSQLTransaction.java:35)
at pl.edu.utp.badania.postgresql.dao.PrincipalDAO.addPrincipal(PrincipalDAO.java:28)
at pl.edu.utp.badania.ui.innercomponents.AddPrincipal$1.buttonClick(AddPrincipal.java:147)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
... 35 more
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1389)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1317)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:81)
... 43 more
Caused by: 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:184)
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 org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:76)
... 43 more
Caused by: java.sql.BatchUpdateException: Zadanie wsadowe 0 insert into principal (address, authority, designation, email, fax, krs, legal_form, name, nip, pesel_regon, stock, surname, telephone, id_principal) values ('sdasd', 'dfsd', 'asdasd', 'ppp@ppp.com', '3211', '12456', 'Osoba fizyczna', 'dasdas', '4312', '3212', 11.0, 'asdasd', '1234', 2) zostało przerwane. Wywołaj getNextException by poznać przyczynę.
at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2753)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1887)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:405)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2900)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 51 more
答案 0 :(得分:1)
尝试在堆栈跟踪中运行insert语句
插入主体(地址,权限,指定,电子邮件,传真,krs,legal_form,名称,nip,pesel_regon,股票,姓氏,电话,id_principal)值('sdasd','dfsd','asdasd',' ppp@ppp.com','3211','12456','Osoba fizyczna','dasdas','4312','3212',11.0,'asdasd','1234',2)
检查表格中的约束。表UNIQUE KEY,FOREIGN KEY约束
答案 1 :(得分:0)
问题可能是由于缺少@GeneratedValue注释或管理id的代码中的错误,如果没有@GeneratedValue是故意的。
来自JPA主键的ObjectDB文档:
如果实体的主键字段未标记@GeneratedValue,则不会生成自动主键值,应用程序负责通过初始化主键字段来设置主键。