java 找到了具有给定标识符的不止一行:1,对于类:com.model.Diagnosis

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25718929/
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-11-02 08:32:09  来源:igfitidea点击:

More than one row with the given identifier was found: 1, for class: com.model.Diagnosis

javaspringhibernate

提问by Anshu Bhattarai

Below is the model, service implementation and the error trace of my code.

下面是我的代码的模型、服务实现和错误跟踪。

Model class:

模型类:

@Entity
@Table(name = "ph_diagnosis_history")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id",scope=Diagnosis.class)
public class Diagnosis extends AbstractEntity {
    @Column(name = "diagnosis_notes", length=255)
    @Length(max = 255, message = IprProp.MAX_LEN_255_MSG)
    private String diagnosisNote;

    @Enumerated(EnumType.STRING)
    private DiagnosisTypeEnum type;

    @Temporal(TemporalType.DATE)
    @Column(name = "diagnosis_date")
    @JsonSerialize(using = JsonDateSerializer.class)
    private Date diagnosisDate;

    @ManyToOne
    @JoinColumn(name = "patient_chart_id")
    private PatientChart patientChart;

    public String getDiagnosisNote() {
        return diagnosisNote;
    }

    public void setDiagnosisNote(String diagnosisNote) {
        this.diagnosisNote = diagnosisNote;
    }

    public DiagnosisTypeEnum getType() {
        return type;
    }

    public void setType(DiagnosisTypeEnum type) {
        this.type = type;
    }

    public Date getDiagnosisDate() {
        return diagnosisDate;
    }

    public void setDiagnosisDate(Date diagnosisDate) {
        this.diagnosisDate = diagnosisDate;
    }

    public PatientChart getPatientChart() {
        return patientChart;
    }

    public void setPatientChart(PatientChart patientChart) {
        this.patientChart = patientChart;
    }
}

My service:

我的服务:

public Diagnosis updateDiagnosis(IPRDTO clientDto, Long userId,
        Long patientChartId, DiagnosisDTO diagnosisDTO, Long diagnosisId) throws Exception {
    Diagnosis diagnosis = diagnosisRepository.findOne(diagnosisId);
    if (diagnosis!=null){
        if(diagnosis.getType().equals(DiagnosisTypeEnum.WORKING)){
            BeanCopyUtil.copyProperties(diagnosisDTO, diagnosis, true);
            return this.diagnosisRepository.saveAndFlush(diagnosis);
        }
        else{
            throw new BusinessException("updateDiagnosisValidation"); 
        }
    }
    throw new BusinessException("noDiagnosis"); 
}

My stack trace:

我的堆栈跟踪:

 [2014-09-08 11:34:08,148] INFO  com.zurelsoft.ipr.service.IDiagnosisService- Logged In User Id [1]:Started  updateDiagnosis
    diagnosis id 2
    [2014-09-08 11:34:08,969] INFO  org.hibernate.event.internal.DefaultLoadEventListener- HHH000327: Error performing load command : org.hibernate.HibernateException: More than one row with the given identifier was found: 2, for class: com.zurelsoft.ipr.model.Diagnosis
    [2014-09-08 11:34:09,001] ERROR com.zurelsoft.ipr.service.IDiagnosisService- Logged In User Id [1]:Error Occurred updateDiagnosis
    org.springframework.orm.jpa.JpaSystemException: org.hibernate.HibernateException: More than one row with the given identifier was found: 2, for class: com.zurelsoft.ipr.model.Diagnosis; nested exception is javax.persistence.PersistenceException: org.hibernate.HibernateException: More than one row with the given identifier was found: 2, for class: com.zurelsoft.ipr.model.Diagnosis
    [2014-09-08 11:34:09,006] ERROR com.zurelsoft.ipr.client.DiagnosisWebService- org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:321)
    org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:403)
    org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
    org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
    org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    com.sun.proxy.$Proxy85.findOne(Unknown Source)
    com.zurelsoft.ipr.service.impl.DiagnosisServiceImpl.updateDiagnosis(DiagnosisServiceImpl.java:76)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:51)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:51)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    com.sun.proxy.$Proxy86.updateDiagnosis(Unknown Source)
    com.zurelsoft.ipr.client.DiagnosisWebService.updateDiagnosis(DiagnosisWebService.java:98)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:849)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)

I get this HibernateException in this line alhough my table has now two rows with the same id.

尽管我的表现在有两行具有相同的 id,但我在这一行中得到了这个 HibernateException。

Diagnosis diagnosis = diagnosisRepository.findOne(diagnosisId);

Please anyone help me with my mistake. What can be probable error in my code.

请任何人帮助我解决我的错误。我的代码中可能有什么错误。

回答by ssergiao

Hy, It woks for me:

嘿,它对我有用:

Seems I have two choices: either cascade all and delete orphan on the one-to-one relationship, or explicitly call my repository class to look up and save the original Sheet every time I remove its relationship with Plate. I opted for the first choice and added this to the plate class:

似乎我有两个选择:要么在一对一关系上全部级联并删除孤立对象,要么在每次删除与 Plate 的关系时显式调用我的存储库类以查找并保存原始 Sheet。我选择了第一选择并将其添加到板块类中:

@OneToOne(mappedBy = "plate", cascade = CascadeType.ALL, orphanRemoval = true)

https://stackoverflow.com/a/32636186/3255595

https://stackoverflow.com/a/32636186/3255595

回答by Ashish Jagtap

it sounds like you have more than one row in the database with the same id values! Are you sure your id is really unique in the DB ?

听起来您在数据库中有不止一行具有相同的 id 值!你确定你的 id 在数据库中真的是唯一的吗?

Hibernate Exception

休眠异常

More than one row with the given identifier was found: 2, for class: com.zurelsoft.ipr.model.Diagnosis

tells that hibernate query executes returns more than one row (i.e 2) of class DiagnosisPlease enable SQL logging and execute the select by hand and see what it returns

告诉 hibernate 查询执行返回多于一行(即 2)的类诊断请启用 SQL 日志记录并手动执行选择并查看它返回的内容

回答by Dmytro Plekhotkin

From Spring documentation:

来自 Spring 文档:

T findOne(ID id)

Retrieves an entity by its id.

Seem that diagnosisId is not a primary key.

似乎diagnosticId 不是主键。