本文介绍了无法在Flowscope中创建输入集的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有输入类型,它的值在flowScope中设置.

I have and input type and it's value is set in flowScope.

<input name="myItem" required="false" value="flowScope.myItem"/>

我正在创建MyOtherItem的列表,并将其发送到这样的控制器方法:

I am creating a list of MyOtherItem and sending it to a controller method like this:

<evaluate expression="myController.save(myOtherItemDataModel.selectedRows,myItem)" result="flowScope.myItem"/>

在MyController内部,我有方法save,其中我想通过从myOtherItemList获取数据来保存myItem的多个实例.

Inside MyController I have method save in which I want to save multiple instances of myItem by getting data from myOtherItemList.

public MyItem save(MyOtherItem[] myOtherItem,MyItem myItem){
    for(int i=0; i<myOtherItem.length; i++){
        myItem.setData(myOtherItem[i].getData());
        saveMyItem(myItem);
    }
    return myItem;
}

在saveMyItem方法内部,我正在保留MyItem对象

Inside saveMyItem method I am persisting MyItem object

public void saveMyItem(MyItem myItem) {
    entityManager.persist(myItem);
}

entityManager是javax.persistence.EntityManager类的实例.

Here entityManager is an instance of javax.persistence.EntityManager class.

我的问题是,当保存方法中的循环运行一次以上时,我只在数据库中保存了一个条目.原因是它不是在创建MyItem的新实例,而只是覆盖旧实例的数据.有人知道我该如何解决这个问题?

My problem is I am getting only one entry saved in the database while the loop in save method runs for more than one time. The reason is it is not creating a new instance of MyItem and just overriding the data of old instance. Does anybody know how can I solve this problem?

推荐答案

我不明白为什么需要将MyItem传递给控制器​​方法我不明白您要返回哪个myItem,因为您似乎想保存其中的一堆...

I don't understand why you need to pass you MyItem to the controller methodI don't understand which myItem you are trying to return, since you seem to want to save a bunch of them...

也许您应该改用类似的方法

maybe you should use something like that instead:

public List<MyItem> save(MyOtherItem[] myOtherItem){
   List<MyItem> result = new ArrayList<MyItem>(); 
   for(int i=0; i<myOtherItem.length; i++){
        MyItem myItem = new MyItem();
        myItem.setData(myOtherItem[i].getData());
        saveMyItem(myItem);
        result.add(myItem);
    }
    return myItem;
}

这篇关于无法在Flowscope中创建输入集的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 23:05