我在JPA / Hibernate中遇到奇怪的错误......我正在
PersistentObjectException: detached entity passed to persist
尝试删除具有由ProviderId和ServicePointNumber组成的组合键的ServicePoint实体时。 (Provider是另一个实体,ServicePoint表示依赖于Provider实体的弱实体)。
代码如下所示:
@DELETE
@Path("/{servicePointNumber : \\d+}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response removeServicePoint( @PathParam("userId") Long userId,
@PathParam("servicePointNumber") Integer servicePointNumber,
@BeanParam GenericBeanParam params) throws ForbiddenException, NotFoundException {
if(params.getAuthToken() == null) throw new ForbiddenException("Unauthorized access to web service.");
logger.log(Level.INFO, "removing given Service Point by executing ProviderResource.ServicePointResource.removeServicePoint(userId, servicePointNumber) method of REST API");
// find ServicePoint entity that should be deleted
ServicePoint toDeleteServicePoint = servicePointFacade.find( new ServicePointId(userId, servicePointNumber) );
// throw exception if entity hasn't been found
if(toDeleteServicePoint == null)
throw new NotFoundException("Could not find service point to delete for given id: " + userId + ".");
// remove entity from database
servicePointFacade.remove(toDeleteServicePoint);
return Response.status(Status.NO_CONTENT).build();
}
实体外观删除方法的实现:
public void remove(@NotNull T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}
答案 0 :(得分:0)
在某个时候你使用的是Evict?或某些session.clear?