Reputation:
I'm trying to add ArrayList
to List
. getSalesOrderitm()
is Salesorder item list.
Criteria cr = session.createCriteria(SalesOrder.class);
cr.add(Restrictions.ge("date", new SimpleDateFormat("yyyy/MM/dd").parse(fdate)));
cr.add(Restrictions.le("date", new SimpleDateFormat("yyyy/MM/dd").parse(tdate)));
List<SalesOrder> orderList = cr.list();
List<SalesOrderItem> itemList = Collections.EMPTY_LIST;
double totalAmount = 0.00;
for (SalesOrder salesOrder : orderList) {
itemList.addAll(salesOrder.getSalesorderyitm());
}
Below you can see hibernate mapping in Salesorder class
@OneToMany(targetEntity=SalesOrderItem.class,mappedBy="salesorder",fetch=FetchType.LAZY)
public List<SalesOrderItem> getSalesorderyitm() {
return salesorderyitm;
}
public void setSalesorderyitm(List<SalesOrderItem> salesorderyitm) {
this.salesorderyitm = salesorderyitm;
}
Below you can see hibernate mapping in salesOrderItem class
@ManyToOne
@JoinColumn(name="SOI_SalesOrderId")
public SalesOrder getSalesorder() {
return salesorder;
}
public void setSalesorder(SalesOrder salesorder) {
this.salesorder = salesorder;
}
I got Below error
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
at java.util.AbstractCollection.addAll(AbstractCollection.java:342)
Upvotes: 2
Views: 3362
Reputation: 10964
As @yassadi already pointed out in one of the comments the problematic part of your code is this line:
List<SalesOrderItem> itemList = Collections.EMPTY_LIST;
Simply change this line to
List<SalesOrderItem> itemList = new ArrayList<>();
and you're done.
As stated in the JavaDocs Collections.EMPTY_LIST
and Collections.emptyList()
are/return immutable lists. That is you must not remove (ok, this is not really the problem with an empty list) or add elements to this list. These helper methods provided by the Collections
class are more to be used as default return values where you want to avoid (memory) overhead by allocating new instances, if only the fact that the collection is empty is of interest.
The immutable characteristic is actually valid for a bunch of methods/members in the Collections
class (emptyList
, singletonList
,emptyMap
, singletonMap
, ...). This always needs to be kept in mind, when using these helper methods.
When using the static members of Collections
, such as EMPTY_LIST
, EMPTY_MAP
, etc. you might notice a compiler warning
Type safety: The expression of type List needs unchecked conversion to conform to List
This is due to the missing type-inference for the static members. By using the corresponding methods such as Collections.emptyList()
the correct generic types will be inferred automatically (emptyList
simply casts to the wanted type) and you will get rid of this annoying warning without adding tons of boilerplate casts to your code. That is the helper methods should always be preferred over the members unless you want pre Java 1.5 compatibility, but these times are hopefully over by now.
See What is the difference between Collections.emptyList() and Collections.EMPTY_LIST
Upvotes: 9