我正在使用 Spring MVC + Hibernate
通用道
// getAll
@SuppressWarnings("unchecked")
public <T> List<T> getAll(Class<T> entityClass) throws DataAccessException {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(entityClass);
return criteria.list();
}
@控制器
@RequestMapping(value = "/genCompanyInfoUpdate", method = RequestMethod.POST)
public String genCompanyInfoUpdate(Model model) {
List<GenCountryModel> countryList=pt.getAll(GenCountryModel.class);
List<GenCurrencyModel> currencyList=pt.getAll(GenCurrencyModel.class);
GenCompanyInfoModel companyInfo=pt.getById(GenCompanyInfoModel.class, 1);
model.addAttribute("countryList", countryList);
model.addAttribute("currencyList", currencyList);
model.addAttribute("companyInfo", companyInfo);
return "gen/genCompanyInfoUpdate";
}
JSP
<c:if test="${not empty currencyList}">
<c:forEach items="${currencyList}" var="get" varStatus="counter">
<ct:Options setValue="${get.id}" setName="${get.isoCode}" selected="${companyInfo.genCurrencyModel.id}" setState="1" />
</c:forEach>
</c:if>
一切正常,但是当我如下更改和使用方法中的投影时,它会给出异常
java.lang.numberformatexception for input string id
java.lang.numberformatexception for input string isoCode
更改:ProjectionList 在方法中的使用
@SuppressWarnings("unchecked")
public <T> List<T> getAll(Class<T> entityClass, String[] nameList) throws DataAccessException {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(entityClass);
ProjectionList pl = Projections.projectionList();
for (int i=0; i<nameList.length; i++) {
pl.add(Projections.property(nameList[i].toString()));
}
criteria.setProjection(pl);
return criteria.list();
}
@Controller 中的更改传递列表 [GenCurrencyModel]
@RequestMapping(value = "/genCompanyInfoUpdate", method = RequestMethod.POST)
public String genCompanyInfoUpdate(Model model) {
String []list={"id","isoCode"};
List<GenCountryModel> countryList=pt.getAll(GenCountryModel.class);
List<GenCurrencyModel> currencyList=pt.getAll(GenCurrencyModel.class,list);
GenCompanyInfoModel companyInfo=pt.getById(GenCompanyInfoModel.class, 1);
model.addAttribute("countryList", countryList);
model.addAttribute("currencyList", currencyList);
model.addAttribute("companyInfo", companyInfo);
return "gen/genCompanyInfoUpdate";
}
相同的 JSP
<c:if test="${not empty currencyList}">
<c:forEach items="${currencyList}" var="get" varStatus="counter">
<ct:Options setValue="${get.id}" setName="${get.isoCode}" selected="${companyInfo.genCurrencyModel.id}" setState="1" />
</c:forEach>
</c:if>
代币模型
public class GenCurrencyModel implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "CURRENCYID")
@GeneratedValue
private long id;
@Column(name = "CODE")
private String currencyCode;
@Column(name = "DESCRIPTION")
private String currencyDesc;
@Column(name = "ISACTIVE")
private int isActive;
@Column(name = "MADELETE")
private int markAsDelete;
@Column(name = "ISOCODE")
private String isoCode;
@Column(name = "CURRENCYUNIT")
private String currencyUnit;
@Column(name = "CONNECTOR")
private String connector;
@Column(name = "SUBUNIT")
private String subUnit;
@Column(name = "RECENTUSERID")
private long recentUserId;
@Column(name = "RECENTUSERIP")
private String recentUserIp;
@Column(name = "DATETIME")
private Date dateTime;
@Column(name = "ISUPDATED")
private int isUpdated;
private GenCompanyInfoModel genCompanyInfoModel;
public GenCurrencyModel() {
super();
}
//Getter Setter
}
我从日志文件中检查查询。它成功执行
当我从jsp页面中删除以下行时,没有任何异常
<ct:Options setValue="${get.id}" setName="${get.isoCode}"
注意:ct:Options 是一个自定义的 JSP 标签,它只是打印值,没什么特别的
投影后查询结果如下
Hibernate: select this_.CURRENCYID as y0_, this_.ISOCODE as y1_ from GENCURRENCY this_
并且都返回列表,并且我检查了两个 size(),大小也相同!
让我知道 !