- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在使用 Hibernate 处理 Spring 3 MVC 项目时,我们遇到了一个恼人的错误。我们正在创建一个拼车应用程序。人们可以添加路线(使用 jQuery 的 Gmap 3 插件)并可以向他们的路线添加路标。在数据库中,航路点具有路线的外键。尝试更新路线时(添加/删除航路点并重新保存路线),我们收到“找到同一集合的两个表示”错误。我们已经研究了 Internet,但大多数主题都在谈论 Play 框架(我们不使用它),而且他们还谈论作为映射方法的注释(而我们使用 XML 映射)。有谁知道我们如何解决这个问题?或者这是 Hibernate 本身的问题?
澄清问题的一些代码:
路由类的XML映射:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="be.kdg.teamb.model.pojo.Route" table="route">
<id name="routeid" type="java.lang.Integer">
<generator class="identity"/>
</id>
<property name="departure" not-null="true"/>
<property name="latitude_departure" />
<property name="longtidude_departure" />
<property name="destination" not-null="true"/>
<property name="latitude_destination" />
<property name="longtidude_destination" />
<property name="departureTime" not-null="true" type="java.util.Date" />
<property name="startDate" not-null="true" type="java.util.Date"/>
<property name="endDate" type="java.util.Date" />
<many-to-one name="driver" column="userid" not-null="true" cascade="save-update" />
<many-to-one name="defaultCar" column="carid" not-null="true" cascade="save- update"/>
<set name="waypoints" cascade="all,delete-orphan" inverse="true">
<key column="routeid" on-delete="cascade" />
<one-to-many class="be.kdg.teamb.model.pojo.Waypoint" />
</set>
<set name="rides" cascade="all,delete-orphan" inverse="true">
<key column="routeid" on-delete="cascade" />
<one-to-many class="be.kdg.teamb.model.pojo.Ride"/>
</set>
<set name="rules" cascade="all,delete-orphan" inverse="true">
<key column="routeid" on-delete="cascade" />
<one-to-many class="be.kdg.teamb.model.pojo.RouteRule" />
</set>
</class>
</hibernate-mapping>
航路点类的 XML 映射:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="be.kdg.teamb.model.pojo.Waypoint" table="waypoint">
<id name="waypointid" type="java.lang.Integer">
<generator class="identity"/>
</id>
<property name="latitude" not-null="true" />
<property name="longtidude" not-null="true" />
<property name="address" not-null="true" />
<many-to-one name="route" column="routeid" not-null="true" cascade="all" />
</class>
</hibernate-mapping>
我们服务中的代码:
@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
类的映射中,
<many-to-one name="route" column="routeid" not-null="true" cascade="all" />
只需删除 cascade="all"
并按如下方式修改您的映射,
<many-to-one name="route" column="routeid" not-null="true" />
尝试执行代码。
您也可以引用博文here或 hibernate 论坛上的帖子 here .阅读here关于 hibernate 中cascade选项的使用。
关于java - hibernate 异常;找到同一集合的两个表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9598890/
问题很简单:我正在寻找一种优雅的使用方式 CompletableFuture#exceptionally与 CompletableFuture#supplyAsync 一起.这是行不通的: priva
对于 Web 服务,我们通常使用 maven-jaxb2-plugin 生成 java bean,并在 Spring 中使用 JAXB2 编码。我想知道如何处理 WSDL/XSD 中声明的(SOAP-
这个问题已经有答案了: Array index out of bound behavior (10 个回答) 已关闭 8 年前。 我对下面的 C 代码感到好奇 int main(){
当在类的开头使用上下文和资源初始化 MediaPlayer 对象时,它会抛出 NullPointer 异常,但是当在类的开头声明它时(因此它是 null),然后以相同的方式初始化它在onCreate方
嘿 我尝试将 java 程序连接到 REST API。 使用相同的代码部分,我在 Java 6 中遇到了 Java 异常,并且在 Java 8 中运行良好。 环境相同: 信任 机器 unix 用户 代
我正在尝试使用 Flume 和 Hive 进行 Twitter 分析。为了从 twitter 获取推文,我在 flume.conf 文件中设置了所有必需的参数(consumerKey、consumer
我在 JavaFX 异常方面遇到一些问题。我的项目在我的 Eclipse 中运行,但现在我的 friend 也尝试访问该项目。我们已共享并直接保存到保管箱文件夹中。但他根本无法让它发挥作用。他在控制台
假设我使用 blur() 事件验证了电子邮件 ID,我正在这样做: $('#email').blur(function(){ //make ajax call , check if dupli
我这样做是为了从 C 代码调用非托管函数。 pCallback 是一个函数指针,因此在托管端是一个委托(delegate)。 [DllImport("MyDLL.dll")] public stati
为什么这段代码是正确的: try { } catch(ArrayOutOfBoundsException e) {} 这是错误的: try { } catch(IOException e) {} 这段
我遇到了以下问题:有导出函数的DLL。 代码示例如下:[动态链接库] __declspec(dllexport) int openDevice(int,void**) [应用] 开发者.h: __de
从其他线程,我知道我们不应该在析构函数中抛出异常!但是对于下面的例子,它确实有效。这是否意味着我们只能在一个实例的析构函数中抛出异常?我们应该如何理解这个代码示例! #include using n
为什么需要异常 引出 public static void main(String[
1. Java的异常机制 Throwable类是Java异常类型的顶层父类,一个对象只有是 Throwable 类的(直接或者间接)实例,他才是一个异常对象,才能被异常处理机制识别。JDK中内
我是 Python 的新手,我对某种异常方法的实现有疑问。这是代码(缩写): class OurException(Exception): """User defined Exception"
我已经创建了以下模式来表示用户和一组线程之间的关联,这些线程按他们的最后一条消息排序(用户已经阅读了哪些线程,哪些没有): CREATE TABLE table(user_id bigint, mes
我正在使用 Python 编写一个简单的自动化脚本,它可能会在多个位置引发异常。在他们每个人中,我都想记录一条特定的消息并退出程序。为此,我在捕获异常并处理它(执行特定的日志记录操作等)后引发 Sys
谁能解释一下为什么这会导致错误: let xs = [| "Mary"; "Mungo"; "Midge" |] Array.iter printfn xs 虽然不是这样: Array.iter pr
在我使用 Play! 的网站上,我有一个管理部分。所有 Admin Controller 都有一个 @With 和一个 @Check 注释。 断开连接后,一切正常。连接后,每次加载页面(任何页面,无论
我尝试连接到 azure 表存储并添加一个对象。它在本地主机上工作得很好,但是在我使用的服务器上我得到以下异常及其内部异常: Exception of type 'Microsoft.Wind
我是一名优秀的程序员,十分优秀!