[英]How can I cast a DataTable, to a List or ArrayList of another type of object
I have a DataTable
object which I need to cast to List<ScParametrics>
and save the list to a database using Hibernate. 我有一个
DataTable
对象,需要将其List<ScParametrics>
为List<ScParametrics>
并使用Hibernate将列表保存到数据库中。
The warning is DataTable cannot be cast to java.util.List
警告是
DataTable cannot be cast to java.util.List
public static addParamDataTable(DataTable listDataTable) {
Session session = HibernateUtil.getSessionFactory();
Transaction tx = session.beginTransaction();
List<ScParametrics> list = (List) listDataTable;
for (Iterator<ScParametrics> it = list.iterator(); it.hasNext();) {
ScParametrics scParametrics = it.next();
session.saveOrUpdate(scParametrics);
}
}
If you are casting something into a list like that you need to be casting the typeof the list not the list. 如果要将某些内容投射到类似的列表中,则需要转换列表的类型而不是列表。
List list = (ScParametrics) listDataTable; 列表列表=(ScParametrics)listDataTable;
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.