问题
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