Not able to create multiple instances of an input set in flowscope

一世执手 提交于 2019-12-11 18:06:33

问题


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

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

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"/>

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;
}

Inside saveMyItem method I am persisting MyItem object

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

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

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?


回答1:


I don't understand why you need to pass you MyItem to the controller method I 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;
}


来源:https://stackoverflow.com/questions/14711101/not-able-to-create-multiple-instances-of-an-input-set-in-flowscope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!