Using JPA (with Hibernate), I was inserting a record in my database. In my table, one column is defined with a DEFAULT Value. But when I insert a save, persist my record, JPA put it as NULL. So using JPA, the solution was to use insertable = false in the column definition. JPA will ignore then that column while inserting in the Database and the default value will be used.
public class ExceptionAdvice {
private static Logger log = LoggerFactory.getLogger(ExceptionAdvice.class);
public Object exceptionHandler(ProceedingJoinPoint proceedingJoinPoint) {
Object value = null;
try {
value = proceedingJoinPoint.proceed();
} catch (Throwable e) {
log.info(proceedingJoinPoint.getSignature().getName() + " error");
String errorMsg = "內部錯誤:" + e.getMessage();
String errorCode = "500";
Map resultJsonMap = new HashMap();
resultJsonMap.put("successful", false);
resultJsonMap.put("errorCode", errorCode);
resultJsonMap.put("errorMsg", errorMsg );
value = resultJsonMap;
log.info(value.toString());
}
return value;
}
com.test.service.TestService
public class TestService {
private static ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
public String runTest() {
throw new IllegalArgumentException("這是測試");
}
public static void main(String[] args) {
TestService ts = ac.getBean("testService", TestService.class);
ts.runTest();
}
}