0

我在 JSF2 中开发了 Web 应用程序,其中我必须坚持两个具有一对多双向关系的实体类。当我尝试保留表(Documenti_tg)的记录时,出现此错误:

Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-4002]             (Eclipse Persistence Services - 2.3.2.v20111125-r10461):
org.eclipse.persistence.exceptions.DatabaseException Internal Exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Result consisted of more
than    one row Error Code: 1172 Call: 
INSERT INTO DOCUMENTI_TG (ANNO, CODDOC, DTAGLIAR, NRIGO, PROGRESSIVO, QUANTITA, TABCOLORI, TAGLIA, IDDOCR) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) bind => [9 parameters bound] Query: InsertObjectQuery(entity.DocumentiTg[ id=null ]) 
at 
org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:786)
 atcom.sun.enterprise.container.common.impl.EntityManagerWrapper.flush(EntityManagerWrapper.java:418) at controller.kontabT.sincronizaDoc(kontabT.java:419)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at   
 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at 
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:601) at    org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java :1052) at  org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124) at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388) at   com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619) at
 com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571) at
 com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162) at     
 com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at
 java.lang.reflect.Method.invoke(Method.java:601) at
 com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861) at
 com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) at
 com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370) at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5360) at
 com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348) at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214) ... 47 more

对于每个 DocumentiR 实体,我有许多 DocumentiTG 实体,这些是类: Documenti_R

@Entity
@Table(name = "DOCUMENTI_R")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "DocumentiR.findAll", query = "SELECT d FROM DocumentiR d"),
@NamedQuery(name = "DocumentiR.findById", query = "SELECT d FROM DocumentiR d WHERE d.id = :id"),
@NamedQuery(name = "DocumentiR.findByCodart", query = "SELECT d FROM DocumentiR d WHERE d.codart = :codart"),
@NamedQuery(name = "DocumentiR.findBySeriale", query = "SELECT d FROM DocumentiR d WHERE d.seriale = :seriale"),
@NamedQuery(name = "DocumentiR.findByDescart", query = "SELECT d FROM DocumentiR d WHERE d.descart = :descart"),
@NamedQuery(name = "DocumentiR.findByUm", query = "SELECT d FROM DocumentiR d WHERE d.um = :um"),
@NamedQuery(name = "DocumentiR.findByQuantita", query = "SELECT d FROM DocumentiR d WHERE d.quantita = :quantita"),
@NamedQuery(name = "DocumentiR.findByPrezzounit", query = "SELECT d FROM DocumentiR d WHERE d.prezzounit = :prezzounit"),
@NamedQuery(name = "DocumentiR.findByCodcolore", query = "SELECT d FROM DocumentiR d WHERE d.codcolore = :codcolore"),
@NamedQuery(name = "DocumentiR.findByImpnet", query = "SELECT d FROM DocumentiR d WHERE d.impnet = :impnet"),
@NamedQuery(name = "DocumentiR.findByIva", query = "SELECT d FROM DocumentiR d WHERE d.iva = :iva"),
@NamedQuery(name = "DocumentiR.findBySconto", query = "SELECT d FROM DocumentiR d WHERE d.sconto = :sconto"),
@NamedQuery(name = "DocumentiR.findByProgressivo", query = "SELECT d FROM DocumentiR d WHERE d.progressivo = :progressivo"),
@NamedQuery(name = "DocumentiR.findByNrigo", query = "SELECT d FROM DocumentiR d WHERE d.nrigo = :nrigo"),
@NamedQuery(name = "DocumentiR.findByAnno", query = "SELECT d FROM DocumentiR d WHERE d.anno = :anno"),
@NamedQuery(name = "DocumentiR.findByCoddoc", query = "SELECT d FROM DocumentiR d WHERE d.coddoc = :coddoc"),
@NamedQuery(name = "DocumentiR.findByTabcolori", query = "SELECT d FROM DocumentiR d WHERE d.tabcolori = :tabcolori"),
@NamedQuery(name = "DocumentiR.findByCompo", query = "SELECT d FROM DocumentiR d WHERE d.compo = :compo"),
@NamedQuery(name = "DocumentiR.findBySconti", query = "SELECT d FROM DocumentiR d WHERE d.sconti = :sconti")})
public class DocumentiR implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID")
private Integer id;
@Size(max = 255)
@Column(name = "CODART")
private String codart;
@Column(name = "SERIALE")
private Integer seriale;
@Size(max = 255)
@Column(name = "DESCART")
private String descart;
@Size(max = 255)
@Column(name = "UM")
private String um;
// @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Column(name = "QUANTITA")
private Float quantita;
@Column(name = "PREZZOUNIT")
private Float prezzounit;
@Size(max = 25)
@Column(name = "CODCOLORE")
private String codcolore;
@Column(name = "IMPNET")
private Float impnet;
@Column(name = "IVA")
private Float iva;
@Column(name = "SCONTO")
private Float sconto;
@Column(name = "PROGRESSIVO")
private Integer progressivo;
@Column(name = "NRIGO")
private Integer nrigo;
@Column(name = "ANNO")
private Integer anno;
@Size(max = 255)
@Column(name = "CODDOC")
private String coddoc;
@Size(max = 10)
@Column(name = "TABCOLORI")
private String tabcolori;
@Size(max = 10)
@Column(name = "COMPO")
private String compo;
@Size(max = 20)
@Column(name = "SCONTI")
private String sconti;
@JoinColumn(name = "IDDOCT", referencedColumnName = "ID")
@ManyToOne
private DocumentiT iddoct;
@OneToMany(mappedBy = "iddocr")
private Collection<DocumentiTg> documentiTgCollection;
//CONSTRUCTOR, GETTER AND SETTER....

和 documenti_Tg 类:

package entity;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "DOCUMENTI_TG")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "DocumentiTg.findAll", query = "SELECT d FROM DocumentiTg d"),
@NamedQuery(name = "DocumentiTg.findById", query = "SELECT d FROM DocumentiTg d WHERE d.id = :id"),
@NamedQuery(name = "DocumentiTg.findByProgressivo", query = "SELECT d FROM DocumentiTg d WHERE d.progressivo = :progressivo"),
@NamedQuery(name = "DocumentiTg.findByNrigo", query = "SELECT d FROM DocumentiTg d WHERE d.nrigo = :nrigo"),
@NamedQuery(name = "DocumentiTg.findByTaglia", query = "SELECT d FROM DocumentiTg d WHERE d.taglia = :taglia"),
@NamedQuery(name = "DocumentiTg.findByQuantita", query = "SELECT d FROM DocumentiTg d WHERE d.quantita = :quantita"),
@NamedQuery(name = "DocumentiTg.findByAnno", query = "SELECT d FROM DocumentiTg d WHERE d.anno = :anno"),
@NamedQuery(name = "DocumentiTg.findByCoddoc", query = "SELECT d FROM DocumentiTg d WHERE d.coddoc = :coddoc"),
@NamedQuery(name = "DocumentiTg.findByDtagliar", query = "SELECT d FROM DocumentiTg d WHERE d.dtagliar = :dtagliar"),
@NamedQuery(name = "DocumentiTg.findByTabcolori", query = "SELECT d FROM DocumentiTg d WHERE d.tabcolori = :tabcolori")})
public class DocumentiTg implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID")
private Integer id;
@Column(name = "PROGRESSIVO")
private Integer progressivo;
@Column(name = "NRIGO")
private Integer nrigo;
@Column(name = "TAGLIA")
private Integer taglia;
@Size(max = 100)
@Column(name = "QUANTITA")
private String quantita;
@Column(name = "ANNO")
private Integer anno;
@Size(max = 255)
@Column(name = "CODDOC")
private String coddoc;
@Size(max = 6)
@Column(name = "DTAGLIAR")
private String dtagliar;
@Size(max = 10)
@Column(name = "TABCOLORI")
private String tabcolori;
@JoinColumn(name = "IDDOCR", referencedColumnName = "ID")
@ManyToOne
private DocumentiR iddocr;
// CONSTRUCTOR, GETTER AND SETTER....

我遇到上述错误的程序代码如下:

DocumentiR driga=new DocumentiT();
//HERE I SET SEVERAL ENTITY PROPERTIES AND SAVE THIS RECORD ON MY DB
driga.setCodart("a string");
driga.setCompo("a string");
driga.setTabcolori("a string");
driga.setCoddoc("a string");
driga.setSeriale(an integer);
driga.setProgressivo(an integer);
driga.setNrigo(an integer);
driga.setDescart("a string");
driga.setImpnet(a float);    
driga.setPrezzounit(a float);
driga.setSconto(rs2.getFloat(29));
driga.setAnno(an integer);
driga.setCodcolore("a string");
driga.setQuantita(a float);
driga.setUm("a string"); 
driga.setIva(a float); 

entityManager.persist(driga);

//FOR every driga i have many DocumentiTg entity
for(int i=0; i<N;i++){ //Where N depends on the specific entity driga above

    DocumentiTg dtaglia= new DocumentiTg();

    //HERE I SET SEVERAL dtaglia PROPERTIES
    dtaglia.setAnno(an integer);
    dtaglia.setCoddoc("a string");
    dtaglia.setProgressivo(an integer);
    dtaglia.setNrigo(an integer);
    dtaglia.setTaglia(an integer);
    dtaglia.setQuantita(an integer);

    entityManager.persist(dtaglia);
    entityManager.flush()
    }            

我的持久性文件是:

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence   http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="FattGimerPU" transaction-type="JTA">
<jta-data-source>jdbc/fattGimer</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<validation-mode>NONE</validation-mode>
<properties>
</properties>
</persistence-unit>
</persistence>
4

1 回答 1

0

我自己解决问题。我的数据库上有一条 TRIGGER 语句(我不知道它的存在),它对这些实体执行相同的操作。所以当我尝试持久化一个实体时,这个触发器语句会调用相同的代码操作,所以我有上面的错误(结果包含不止一行)。只需在我的 MySql 数据库上删除此 TRIGGER STATEMENT 即可,一切正常。

于 2013-09-20T09:28:02.457 回答