当前位置: 首页 > 知识库问答 >
问题:

冬眠恩弗斯一个多月瀑布。所有组织。冬眠HibernateeException:找到多个具有给定标识符的行

袁秦迟
2023-03-14

我有两个实体,由单向连接OneTo很多:

@Entity
@Audited
public class Car {

    @Id
    @GeneratedValue
    private Long id;

    private String brand;

    // Constructor, getters and setters omitted

}
@Entity
@Audited
public class Driver {

    @Id
    @GeneratedValue
    private Long id;

    @OneToMany(cascade = CascadeType.ALL)
    @AuditJoinTable(name = "car_aud")
    @JoinColumn(name = "DRIVER_ID")
    private Set<Car> cars;

    // Constructor, getters and setters omitted

}

当我试图用汽车创造我的司机时,像这样

driver:{"cars":[{"brand":"Alfa Romeo"}]} //json input


entityManager.persist(driver); //java code

ENVERS创建:

  • 驱动程序表中包含新驱动程序数据的行,//例如,ID=1
  • 包含新车数据的CAR表中的一行,//例如,ID=3
  • 修订实体表中的一行,//例如,ID=8
  • 驱动程序中的一行\u AUD:
| REVTYPE | REV | ID |
---------------------- | 0 | 8 | 1 |
  • 一辆车有两排:
| REVTYPE | REV | ID | DRIVER_ID |   BRAND    |
----------------------------------------------- | 0 | 8 | 3 | null | Alfa Romeo |
----------------------------------------------- | 0 | 8 | 3 | 1 | null |

所以当我试图修改ID=1的驱动程序时

return AuditReaderFactory
                .get(entityManager)
                .createQuery()
                .forRevisionsOfEntity(Driver.class, true, true)
                .add(AuditEntity.property("id").eq(id))
                .getResultList();

我得到了HibernateException,因为有两行具有相同的实体id和汽车id。

org.hibernate.HibernateException: More than one row with the given identifier was found: {REV=DefaultRevisionEntity(id = 8, revisionDate = REV_TIMESTAMP), id=3}, for class: com.follow.me.entity.Car_AUD
    at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.extractEntityResult(AbstractLoadPlanBasedEntityLoader.java:286) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:230) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.doLoad(AbstractEntityPersister.java:4396) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4386) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:569) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:537) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:208) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:332) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:108) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:74) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:113) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.internal.SessionImpl.fireLoadNoChecks(SessionImpl.java:1176) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1041) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:687) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.type.EntityType.resolve(EntityType.java:464) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.type.ManyToOneType.resolve(ManyToOneType.java:240) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.type.EntityType.resolve(EntityType.java:457) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.type.EntityType.nullSafeGet(EntityType.java:272) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.loader.hql.QueryLoader.getResultRow(QueryLoader.java:457) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.loader.hql.QueryLoader.getResultColumnOrRow(QueryLoader.java:440) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:775) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.loader.Loader.processResultSet(Loader.java:1008) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.loader.Loader.doQuery(Loader.java:964) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2838) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2820) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2652) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.loader.Loader.list(Loader.java:2647) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:506) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:396) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:219) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1404) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1562) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1530) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.envers.internal.entities.mapper.relation.lazy.initializor.AbstractCollectionInitializor.initialize(AbstractCollectionInitializor.java:51) ~[hibernate-envers-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.envers.internal.entities.mapper.relation.lazy.proxy.CollectionProxy.checkInit(CollectionProxy.java:33) ~[hibernate-envers-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.envers.internal.entities.mapper.relation.lazy.proxy.CollectionProxy.hashCode(CollectionProxy.java:130) ~[hibernate-envers-5.4.21.Final.jar:5.4.21.Final]
    at java.util.Arrays.hashCode(Arrays.java:4146) ~[na:1.8.0_221]
    at java.util.Objects.hash(Objects.java:128) ~[na:1.8.0_221]
    at com.follow.me.entity.Driver.hashCode(Driver.java:63) ~[classes/:na]
    at org.hibernate.envers.internal.tools.Triple.hashCode(Triple.java:63) ~[hibernate-envers-5.4.21.Final.jar:5.4.21.Final]
    at java.util.HashMap.hash(HashMap.java:339) ~[na:1.8.0_221]
    at java.util.HashMap.put(HashMap.java:612) ~[na:1.8.0_221]
    at org.hibernate.envers.internal.reader.FirstLevelCache.putOnEntityNameCache(FirstLevelCache.java:87) ~[hibernate-envers-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.envers.internal.entities.EntityInstantiator.createInstanceFromVersionsEntity(EntityInstantiator.java:100) ~[hibernate-envers-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.envers.query.internal.impl.RevisionsOfEntityQuery.getQueryResultRowValue(RevisionsOfEntityQuery.java:203) ~[hibernate-envers-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.envers.query.internal.impl.RevisionsOfEntityQuery.getQueryResults(RevisionsOfEntityQuery.java:185) ~[hibernate-envers-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.envers.query.internal.impl.RevisionsOfEntityQuery.list(RevisionsOfEntityQuery.java:136) ~[hibernate-envers-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.envers.query.internal.impl.AbstractAuditQuery.getResultList(AbstractAuditQuery.java:112) ~[hibernate-envers-5.4.21.Final.jar:5.4.21.Final]
    at com.follow.me.service.Controller.getRevision(Controller.java:62) ~[classes/:na]
    at com.follow.me.service.Controller$$FastClassBySpringCGLIB$$b397937b.invoke() ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:687) ~[spring-aop-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at com.follow.me.service.Controller$$EnhancerBySpringCGLIB$$15c599d0.getRevision() ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_221]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_221]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_221]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.33.jar:9.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) [tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594) [tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.33.jar:9.0.33]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_221]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_221]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.33.jar:9.0.33]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_221]

如何修复此问题,让驱动程序的修订列表成功构建?

共有1个答案

崔单弓
2023-03-14

好吧,问题是当你尝试的时候

Car car = new Car();
car.setBrand("Alfa Romeo");
Set<Car> cars = new HashSet<>();
cars.add(car);

Driver driver = new Driver();
driver.setCars(cars);

em.persist(driver);

对于单向一对多,hibernate执行3条sql语句:

  1. 插入到驱动程序(id)值(?)
  2. 插入汽车(品牌,id)值 (?, ?)
  3. 更新汽车driver_id=?id=?

切换到双向关系

@Entity
@Audited
public class Car {

    @Id
    @GeneratedValue
    private Long id;

    private String brand;

    @ManyToOne
    private Driver driver;

    // Constructor, getters and setters omitted
}

@Entity
@Audited
public class Driver {

    @Id
    @GeneratedValue
    private Long id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "driver")
    @AuditJoinTable(name = "car_aud")
    private Set<Car> cars = new HashSet();

    public void setCars(Set<Car> cars) {
        books.forEach(b -> b.setDriver(this));
        this.cars.addAll(cars);
    }

    // Constructor, getters and setters omitted

}

使Hibernate仅创建2条SQL语句:

  1. 插入到驱动程序(id)值(?)
  2. 插入汽车(driver_id,品牌,id)值 (?, ?, ?)

同样的结果。因此,CAR_AUD表中不会出现歧义。

 类似资料:
  • 组织。冬眠HibernateeException:找到了多个具有给定标识符的行:578,用于类:com。冬眠查询表演坚持不懈模型百货商店 数据库没有标识符为578的重复存储行。使用SQL对其进行了检查: 它返回0条记录。 关于一、二的其他问题指出,问题可能与OneToOne映射本身有关。商店和员工实体具有OneToOne关系,我的映射如下: 工作人员: 商店: 如何修复它? 更新: 当查询被修改为

  • 我有两张桌子,两张桌子之间有一对一的关系。 A表中的 用户表: 我称之为询问 和错误日志: 当我在数据库中有一个带有这个Bname的条目时,我得到了这个执行选项。 有人有什么想法吗 干杯

  • 我正在开发一个电子商务后端在Spring启动。每次我为同一个创建两个以上的时,我都会得到以下错误:.要创建一个订单,我首先创建一个付款,然后创建由付款生成的订单,所以问题可能也出在付款的创建上。 实体: 实体: 实体: : : 支付服务: ################################################################################

  • 我在我的spring boot应用程序中出现了这个错误。当我想调用这个存储库时,我遇到了这个错误 这是我的存储库: 这就是我称之为此方法的地方: 如何检查方法返回的结果?我调试了我的代码,但在断点它只是返回这个错误。 谢谢你的帮助。

  • 我有以下配置: 我有我的Geoname类在com.example.domain: 然而,在运行时,我会遇到以下异常: 原因:组织。冬眠AnnotationException:未为实体com指定标识符。实例领域组织的地理名称。冬眠cfg公司。继承状态。在org上确定了faultaccesstype(InheritanceState.java:277)。冬眠cfg公司。继承状态。位于org的getEl

  • 我有一个实体A,它与TempA实体有关系,如下所示: 然后在doInHibernate我称之为 然后我收到了错误。TempA是一个共享表,因此其他服务可以实际插入该表。我需要的是防止冬眠者称之为TempA。我能够阻止它与A进行左连接,但是,它仍然会导致这个错误。