当列与数据库列不匹配时,我们将在日志中获取异常,如下所示。但它并没有抛出异常。我甚至保留了试试。我怎么能抓住这个例外?
WARN 2015-11-04 17:25:44,155 [http-apr-8080-exec-4] [SqlExceptionHelper.java:144]:SQL错误:904,SQLState:42000 错误2015-11-04 17:25:44,455 [http-apr-8080-exec-4] [SqlExceptionHelper.java:146]:ORA-00904:" SH_RATE_DESCRIPTIO":无效识别 费里
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import com.core.model.entity.ShippingTable;
@Repository("sTableDAO")
public class STableDao {
@PersistenceContext(unitName = "somaUnit")
private EntityManager entityManager;
public STable save(STable sTable) {
try{
sTable = entityManager.merge(sTable);
}catch(Exception e){
e.printStackTrace();
}
return sTable;
}
}
答案 0 :(得分:1)
只需将此记录器的loglevel设置为致命。 在代码中:
private static Logger logger = org.apache.log4j.Logger.getLogger("org.hibernate.engine.jdbc.spi.SqlExceptionHelper");
logger.setLevel(Level.FATAL);
或者:
...
<!-- ADDED THE FOLLOWING -->
<logger category="org.hibernate.engine.jdbc.spi.SqlExceptionHelper">
<level name="FATAL"/>
</logger>
...
答案 1 :(得分:1)
尝试从DataIntegrityViolationException获取消息
catch (DataIntegrityViolationException ex) {
handleException(ex.getRootCause().getMessage()) /*get the message and handle it*/
}
答案 2 :(得分:0)
Level.FATAL
甚至吞下了错误日志消息。设置
org.apache.log4j.Logger.getLogger("org.hibernate.engine.jdbc.spi.SqlExceptionHelper")
.setLevel(Level.ALL);
在某个任意类的static
初始化程序块中做了诀窍!