简体   繁体   English

使用RequestFactory时传递的分离实体可以持久化

[英]detached entity passed to persist when using RequestFactory

I'm trying to use the RequestFactory but I have trouble changing existing records. 我正在尝试使用RequestFactory,但是我无法更改现有记录。 I keep getting a org.hibernate.PersistentObjectException: detached entity passed to persist while I do not understand what's wrong. 我不断收到org.hibernate.PersistentObjectException:独立的实体传递给持久化,而我不明白这是怎么回事。

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. 我有一个列出所有产品的功能(通过findAllProducts()检索),然后我想更改一个产品。 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");
            }
        });
    }

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. 这是通过Hibernate / JPA中名为merge的方法完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM