java – Hibernate异常;找到了同一个集合的两个表示

前端之家收集整理的这篇文章主要介绍了java – Hibernate异常;找到了同一个集合的两个表示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在使用Hibernate处理Spring 3 MVC项目时,我们遇到了一个恼人的错误.我们正在创建一个拼车应用程序.人们可以添加路线(使用jQuery的Gmap 3插件)并可以为其路线添加路标.在数据库中,航点具有路线的外键.当尝试更新路线(添加/删除航点并重新保存路线)时,我们得到“找到同一集合的两个表示”错误.我们研究了互联网,但主要讨论Play框架的主题(我们不使用它),而且他们将注释称为映射方法(当我们使用XML映射时).有谁知道我们如何解决这个问题?或者这是Hibernate本身的问题?

一些代码澄清了问题:

路由类的XML映射:

航点类的XML映射:

我们服务中的代码

@Override
@Transactional
public void add(String departureCoordinates,String departure,String destinationCoordinates,String destination,String coordinatesWaypoints,String namesWaypoints,String user,int carID,Date beginDate,Date endDate,Date departureTime) {

    User usr = userDao.get(user);

    Route route = new Route();

    //formaat: latitude-longtitude
    String[] coords = departureCoordinates.split(",");

    String departureLatitude = coords[0];
    String departureLongtitude = coords[1];

    coords = destinationCoordinates.split(",");

    String destinationLatitude = coords[0];
    String destinationLongtitude = coords[1];

    // add route information
    route.setDeparture(departure);
    route.setLatitude_departure(departureLatitude);
    route.setLongtidude_departure(departureLongtitude);

    route.setDestination(destination);
    route.setLatitude_destination(destinationLatitude);
    route.setLongtidude_destination(destinationLongtitude);

    route.setDriver(usr);
    route.setDefaultCar(carDao.get(carID));

    route.setStartDate(beginDate);
    route.setDepartureTime(departureTime);
    route.setEndDate(endDate);

    if (coordinatesWaypoints != null && coordinatesWaypoints.trim().length() != 0) {
        String[] waypoints = coordinatesWaypoints.split(";;;");
        String[] waypointNames = namesWaypoints.split(";;;");

        int i = 0;

        for (String waypointAddress : waypointNames) {
            String[] waypointCoord = waypoints[i].split(",");

            String waypointLat = waypointCoord[0];
            String waypointLon = waypointCoord[1];

            Waypoint waypoint = new Waypoint();

            waypoint.setAddress(waypointAddress);
            waypoint.setLatitude(waypointLat);
            waypoint.setLongtidude(waypointLon);

            route.addWaypoint(waypoint);

            i++;
        }
    }
        else{
            for (Waypoint waypoint : route.getWaypoints()) {
                waypointDao.delete(waypoint);
            }


    }

    routeDao.save(route);

    socialMediaService.post(user,"http://localhost:8080/route/detail/" + route.getRouteid(),"Posted a new route! From " + route.getDeparture() + " to " + route.getDestination(),"Posted new route!","Carpool teamb","Posted a new route! From " + route.getDeparture());

}

错误堆栈跟踪:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing Failed; nested exception is org.hibernate.HibernateException: Found two representations of same collection: be.kdg.teamb.model.pojo.Route.waypoints
 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
 org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
 org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
 org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:101)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.authentication.logout.logoutFilter.doFilter(logoutFilter.java:105)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173)
 org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
 org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
 org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause

org.hibernate.HibernateException: Found two representations of same collection: be.kdg.teamb.model.pojo.Route.waypoints
 org.hibernate.engine.Collections.processReachableCollection(Collections.java:175)
 org.hibernate.event.def.FlushVisitor.processCollection(FlushVisitor.java:60)
 org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:122)
 org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:83)
 org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:77)
 org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:165)
 org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
 org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
 org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:58)
 org.hibernate.impl.SessionImpl.autoFlushIfrequired(SessionImpl.java:1185)
 org.hibernate.impl.SessionImpl.list(SessionImpl.java:1261)
 org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
 org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:890)
 be.kdg.teamb.model.dao.impl.UserDaoImpl.get(UserDaoImpl.java:31)
 be.kdg.teamb.model.service.impl.UserServiceImpl.get(UserServiceImpl.java:222)
 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 java.lang.reflect.Method.invoke(Method.java:597)
 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
 org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
 org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
 $Proxy33.get(Unknown Source)
 be.kdg.teamb.model.service.impl.SocialMediaServiceImpl.post(SocialMediaServiceImpl.java:36)
 be.kdg.teamb.model.service.impl.RouteServiceImpl.update(RouteServiceImpl.java:202)
 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 java.lang.reflect.Method.invoke(Method.java:597)
 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
 $Proxy40.update(Unknown Source)
 be.kdg.teamb.controller.RouteController.edit(RouteController.java:258)
 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 java.lang.reflect.Method.invoke(Method.java:597)
 org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
 org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
 org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
 org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
 org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
 org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
 org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:101)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.authentication.logout.logoutFilter.doFilter(logoutFilter.java:105)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
 org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173)
 org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
 org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
 org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.25 logs.

Apache Tomcat/7.0.25

我们还尝试添加route.getWaypoints().clear();但这似乎没有帮助.

有关该怎么办的任何建议?如果您想了解更多信息,请询问.

最佳答案
在Waypoint类的映射中,

只需删除cascade =“all”并修改您的映射,如下所示,

尝试执行代码.

你也可以参考博客文章here或hibernate论坛here上的一个帖子.阅读here关于hibernate中cascade选项的用法.

原文链接:https://www.f2er.com/java/438032.html

猜你在找的Java相关文章