The decimal type of mysq and the setResultTransformer of hibernate



 If the mysql database is of decimal type (powerdesigner is numeric(12,2))

 

The java entity class is automatically generated by the tool, and the corresponding type is double. There is no problem when using hqlQuery to query the entity, or when using the sqlQuery.addEntity (select * from method, it must be the query hibernate entity class) method, and the entity can be generated smoothly. kind.

However, if you use setResultTransformer, this time is to generate a VO non-hibernate entity class, which is equivalent to assembling the class yourself. At this time, when setContractAmt is used, what setter will not be reported, expectedtype=dobule, it should be that the found type cannot be transformed into dobule, you need to change the field type to BigDecimal at this time.

//	private double contractAmt;
	private BigDecimal contractAmt;

 

---Also, the addEntity method is only suitable for SQLQuery. At this time, SQL is also the method of select *, and it must be a hibernate entity class. At this time, the column to property will be automatically converted. And setResultTransformer is equivalent to assembling any VO, you must write an alias (in the way of as, see another blog of mine ), which can be sql or hql, assembled into a VO.

 

Mad, hibernate these type conversion problems are too annoying! ! Seriously affect business development

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326845506&siteId=291194637