detached entity passed to persist when using RequestFactory
I'm trying to use the RequestFactory but I have trouble changing existing records. I keep getting a org.hibernate.PersistentObjectException: detached entity passed to persist while I do not understand what's wrong.
I have a the following class:
@Service(Product.class)
public interface ProductRequest extends RequestContext {
Request<Long> countProducts();
Request<ProductProxy> findProduct(Long id);
Request<List<ProductProxy>> findAllProducts();
InstanceRequest<ProductProxy, Void> persist();
InstanceRequest<ProductProxy, Void> remove();
}
I have a function that lists all products (retrieved via findAllProducts()) and then I want to change one product. For this purpose I use the following code:
public void changeProductDetails(ProductProxy prod) {
ProductRequest newProductRequest = MyApplication.getRequestFactory().productRequest();
ProductProxy editedProduct = newProductRequest.edit(prod);
editedProduct.setPurpose("new purpose string");
newProductRequest.persist().using(editedProduct).fire(new Receiver<Void>() {
@Override
public void onSuccess(Void arg0)
{
System.out.println("Product changed开发者_Python百科");
}
});
}
What is wrong, is that persist should not be called to detached objects. Purpose of persist-method is to persist new objects.
What you want to do is to merge changes from the detached object. That is done via method named merge in Hibernate/JPA.
精彩评论