Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching hibernate exception SqlExceptionHelper

Tags:

hibernate

jpa

We are getting the exception in the log as below line, when the column is not matched with database column. But it is not throwing the exception. I have even kept the try catch. How can i catch the exception?

WARN 2015-11-04 17:25:44,055 [http-apr-8080-exec-4][SqlExceptionHelper.java:144] : SQL Error: 904, SQLState: 42000 ERROR 2015-11-04 17:25:44,055 [http-apr-8080-exec-4][SqlExceptionHelper.java:146] : ORA-00904: "SH_RATE_DESCRIPTIO": invalid identi fier

 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;      
 }  
}
like image 774
user3157090 Avatar asked Nov 04 '15 12:11

user3157090


3 Answers

Try to get message from DataIntegrityViolationException

catch (DataIntegrityViolationException ex) {
   handleException(ex.getRootCause().getMessage()) /*get the message and handle it*/
}
like image 166
wanz Avatar answered Oct 14 '22 05:10

wanz


Just set the loglevel for this logger to fatal. In the code:

private static Logger logger = org.apache.log4j.Logger.getLogger("org.hibernate.engine.jdbc.spi.SqlExceptionHelper");
logger.setLevel(Level.FATAL);

Or:

...
<!-- ADDED THE FOLLOWING -->
<logger category="org.hibernate.engine.jdbc.spi.SqlExceptionHelper">
<level name="FATAL"/>
</logger>
...
like image 39
rko_cri Avatar answered Oct 14 '22 05:10

rko_cri


Level.FATAL even swallowed the error log message. Setting

org.apache.log4j.Logger.getLogger("org.hibernate.engine.jdbc.spi.SqlExceptionHelper")
    .setLevel(Level.ALL);

In a static initializer block of some arbitrary class did the trick!

like image 21
Monkey Supersonic Avatar answered Oct 14 '22 05:10

Monkey Supersonic