zwwcn

Just another WordPress.com site

Monthly Archives: October 2015

Json mapping exception:

Exception: Can not construct instance of abc.bcd.def.entity from String value ‘NONE’: value not one of declared Enum instance names

Solution: add @JsonCreator to the enum getter method.

	    @JsonCreator
	    public static InvestigatedBy fromName(String optionName) {
			if (optionName == null) {
				return null;
			}
			for (InvestigatedBy entry: InvestigatedBy.values()) {
				if (entry.getName().equals(optionName)) {
					return entry;
				}
			}
			return null;
		}
Advertisements

Concat in JPA native query

I am writing some native query to record customized info.

SELECT COUNT(DISTINCT i.OBJECT_ID),CONCAT(YEAR(i.DATE) ,'-',MONTH(i.DATE)) as yearDate FROM OP_INCIDENT i GROUP BY YEAR(i.DATE),MONTH(i.DATE)

The above query is supposed to generate data in format like [2,’2015-09′], but what I got is [2,[54,43,41,49,50]]. Instead giving a string year-month value, it gives me a data array.

An explicit type cast sovle the problem:

SELECT COUNT(DISTINCT i.OBJECT_ID),CONCAT(CAST(YEAR(i.DATE) AS CHAR),'-',CAST(MONTH(i.DATE) AS CHAR)) as yearDate FROM OP_INCIDENT i GROUP BY YEAR(i.DATE),MONTH(i.DATE)