Friday, March 22, 2019

JPA : Hibernate implementation raises exception when retrieving jdbc connection

For some cases, there a need to retrieve jdbc connection to execute some database operations such as batch works; tables loading from big files or others .....

When working with enitity manager

Connection conn = em.unwrap(Session.class).connection();  

raises an exception when working with an hibernate implementation (works fine with eclipselink)
One solution is :

public Connection hibernateConnection() {
  SessionImplementor si = (SessionImplementor) entityManager.unwrap(Session.class);
  try {
   return si.getJdbcConnectionAccess().obtainConnection();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return null;
  }
  
  
 }

Friday, March 1, 2019

JBoss 7 - JSF null value input become 0

this problem occures when binding an input in jsf to a numeric attribute in the bean.
Usually when we are running the app on apache tomcat we addthis jvm paramter in the tomcat VM
-Dorg.apache.el.parser.COERCE_TO_ZERO=false

For those who are running on JBoss Server 7.x
adding a context parameter


<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param> 
in standalone.xml file did not work for me.
One working solution is to create is to add a listener to your web app that adds this parameter dynamically
Here is the code

'Here i'm using WebListener annotation'

  
package com.cnss.ds.web.utils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;


@WebListener
public class AppWebLsistener  implements ServletContextListener {
 @Override
    public void contextInitialized(ServletContextEvent event) {

        System.setProperty("org.apache.el.parser.COERCE_TO_ZERO", "false");
    }

 @Override
 public void contextDestroyed(ServletContextEvent arg0) {
  // TODO Auto-generated method stub
  
 }

}