English 中文(简体)
最新信息
原标题:update @OneToMany in Play framework

I have a simple form that intend to update a list of items in a cart. The form works pretty well, I display an input text for each item in the cart. But if I change an item text and then save, it fails with an hibernate error : PersistenceException occured : org.hibernate.PersistentObjectException: detached entity passed to persist: models.Item

此处为:

<section class="form">
    #{form @save()}
        <input type="hidden" name="cart.id" value="${cart?.id}">

        <p class="field">
            <label for="title">Title :</label>
            <input type="text" name="cart.title" value="${cart?.title}"
            maxlength="50" size="50"/>
            <span class="error">${errors.forKey( cart.title )}</span>
        </p>

        <div id="items">
            #{if cart && cart.items}
                            #{list items:cart.items, as: item }
                                    <input type="hidden" name="cart.items[${item_index}].id" value="${item?.id}">
                                    <input type="text"    name="cart.items[${item_index}].text" value="$
{item?.text}"/>
                        #{/list}
                    #{/if}
        </div>

        <p class="buttons">
            <a href="@{index()}">Cancel</a> <input type="submit" value="Save" id="savecart">
        </p>
    #{/form}
</section>

我的实体和控制人:

@Entity
public class Cart extends Model {
    @Required
    public String title;

    @OneToMany(mappedBy = "cart", cascade = CascadeType.ALL)
    public List<Item> items;

    public Cart(String title) {
            this.title = title;
            this.items = new ArrayList<Item>();
    }
}


@Entity
public class Item extends Model {
    @Required
    public String text;

    @ManyToOne
    @Required
    public Cart cart;

    public Item(String text, Cart cart) {
            this.text = text;
            this.cart = cart;
    }

}

public static void save(Cart cart){
    validation.valid(cart);
            if (validation.hasErrors()) {
                    validation.keep();
                    formCart(cart.id);
            }
            cart.save();
            index();
} 

I think it s a common use case, however I m not sure to do it the right way.

问题回答

您在保留该实体之前必须将其合并。

cart = cart.merge();
cart.save();




相关问题
query must begin with SELECT or FROM: delete [delete

I am using JPA in addition to spring(3.0.0.M4). While deleting multiple records using query.executeUpdate() i am getting the following exception. org.springframework.web.util.NestedServletException: ...

Last update timestamp with JPA

I m playing around a bit with JPA(Eclipselink to be specific). The below entity have a timestamp that s supposed to reflect whenever that entity was last updated. What are the strategies for making ...

@IdClass with non primative @Id

I m trying to add a composite primary key to a class and having a bit of trouble. Here are the classes. class User { private long id; ... } class Token { private User user; private ...

Database not dropped in between unit test

Hello good people i came accross a weird behaviour in my test.I m using JPA hibernate annotation with spring. let say i have an Class MyObject and it s property email is marqued @Column(name="EMAIL", ...

Toplink trying to persist null object

I have an object "Instance" with another object "Course" inside. When trying to persist a new Instance object, I get the following error if Course is null: java.lang.IllegalStateException: During ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

热门标签