Saturday, November 17, 2012

Java.lang.SecurityException: class - Mismatch in signer information


Problem details :
[Java.lang.SecurityException: class "org.hibernate.cfg.AnnotationConfiguration"'s signer information does not match signer information of other classes in the same package]
This error occured when I transfering my xml based hibernate in to annotation based.
I used the class org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean instead of local session factorybean class. 

Detailed stack trace:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestM
appingHandlerMapping#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating be
an with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessio
nFactoryBean]: Constructor threw exception; nested exception is java.lang.SecurityException: class "org.hibernate.cfg.AnnotationConfiguration"'s signe
r information does not match signer information of other classes in the same package
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:943)
        at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:778)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:504)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
        at org.apache.catalina.core.StandardService.start(StandardService.java:525)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource
[/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not i
nstantiate bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Constructor threw exception; nested exception is j

Trouble shooting:
While looking for the possible solution I came to know that this must be because of the jar version missmatch.


Solution
I removed the hibernate-3.3.2.GA_CP03.jar from library.


Added following jars in to the library

hibernate-3.2.3.ga
hibernate-annotations-3.3.0.ga
hibernate-commons-annotations-3.0.0.ga

So started working fine...
Share your thoughts

Thursday, November 15, 2012

Hibernate query with like operator


Like operator
Follwoing method shows how to use the like operator in the hibernate quey.

  • Setting the querystring to search along with query as a parameter
  • enforcing case sensitive for searching upper case also.
  • return type is generis
  • Hibernate is simple t use in the queries

public List<password> getPasswordList(String search) {
   Query query =getSession().createQuery("from Password where upper(source) like :search order by source asc ");
   query.setParameter("search", "%"+search.toUpperCase()+"%");
         List pw =query.list();
         return pw;
 }
Share your thoughts

Delete and Edit query in Hibernate Example

Delete functionality in hibernate

If you use HibernateDAOTemplate in your application you can refer the following code for the delete function this is a working example.

I am extending the HibernateDAOTemplate  so I will get the session object directly.
public class PasswordDaoImpl  extends HibernateDaoSupport implements PasswordDao

In this method the row id is passing as input. By creating a session object invoking the createQuery() method.



public int delPassword(int id) {
   Session session=getSession();
   String hql = "delete from Password where id= :id";
   session.createQuery(hql).setInteger("id", new Integer(id)).executeUpdate();
   return 0;
 }

  • Password is the domain class name.
  • Getting the session object in the initial of class.
 Edit Query using hibernate Example

 public void editPassword(Password pw) {
    Query query = getSession().createQuery("update Password  set source=:src," +
    "userName=:user,password=:pw,remarks=:remarks where id=:id");
    query.setParameter("src", pw.getSource());
    query.setParameter("user", pw.getUserName());
    query.setParameter("pw", pw.getPassword());
    query.setParameter("remarks", pw.getRemarks());
    query.setParameter("id", pw.getId());
    query.executeUpdate();
    logger.info("editing password for the source: " + pw.getSource());
 
             }
Share your thoughts

Sunday, November 11, 2012

Convert JDBC code in to Hibernate

Welcome to my new article on convert jdbc code to Hibernate.

If you want to convert your J2ee application developed using JDBC into hibernate you landed in the correct place. This practical example will help you to convert JDBC Code to use hibernate as the ORM package which benefits your application:
  • Helps to abstract the persistence of the object.
  • Powerful hibernate query language (HQL).
  • The developer free from the headaches of connection pooling, connection management etc.
in order to convert your application to use hibernate, follow the following steps,

1. Add the following jars to your library.

 hibernate-3.3.2.GA_CP03.jar
javaee-api-6.0.jar
dom4j-1.6.1.jar
jta-1.3.jar
 
2. Update your configuration files.

Update spring beans configuration file using apache commons for configurations. I used apache commons library for my project.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
 
Update jdbc transaction manager to use the hibernate transaction manager.
The following is the code my file has
 
<bean id="transactionManager" 
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

This we can replace using the below given code

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>passwordvault/domain/hibernate/Password.hbm.xml</value>
</list>
</property>
 
 
I am using postgresql database so the corresponding sql dialect is org.hibernate.dialect.PostgreSQLDialect, To create Hibernate template instance we need a Session Factory, for this purpose we injecting the sessionFactory property in the Spring bean configuration file.

    <bean id="passwordDao" class="passwordvault.repository.PasswordDaoImpl" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
 
 
3. Model files mapping

You have to create hibernate mapping file to use hibernate. This mapping file will maps your domain class and the tables. This will have the name ending with hbm.xml. For example if your domain class have the name Password.java your mapping file name you can keep as Password.hbm.xml You have the right to chose the right name for the mapping file.

See the Password.hbm.xml file here

     <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="passwordvault.domain.Password"
table="pw.password" >
<id name="id" type="integer">
<column name="id" />
<generator class="sequence">
<param name="sequence">pw.pw_seq</param>
</generator>
</id>
<property name="password" type="string">
<column name="PW_PASSWORD" length="100" not-null="true" />
</property>
<property name="source" type="string">
<column name="PW_SOURCE" not-null="true" />
</property>
<property name="userName" type="string">
<column name="PW_USERNAME" length="100" not-null="true" />
</property>
<property name="remarks" type="string">
<column name="remarks" length="500" not-null="true" />
</property>
</class>
</hibernate-mapping>
This corresponds to the model class Password.java
package passwordvault.domain;
import java.io.Serializable;
public class Password implements Serializable {
private int id;
private String password;
private String source;
private String userName;
private String remarks;
public int getId() {

return id;

}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public void setId(int id) {

this.id = id;

}
public String getPassword() {

return password;

}
public void setPassword(String password) {
this.password = password;
}
/**
* @return the source
*/
public String getSource() {
return source;
}
/**
* @param source the source to set
*/
public void setSource(String source) {
this.source = source;
}
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
}
It is common in a typical J2EE application will have a number of model classes so you need to have the corresponding mapping file as explained above.
You can follow the annotation base configuration I am explained here.
 
4. Changes in the DAO class 

In the DAO class we use Hibernate Template to access the database. Extend the HibernateDaoSupport calss in the DaoImpl class to make your DAO class to eligible for the hibernate usage.

public class PasswordDaoImpl  extends HibernateDaoSupport implements PasswordDao {


the method used by JDBC method need to change to use the hibernate template and you can use powerfu Hibernate SQL query language in your code,

/*Old method 1-*/
public List getPasswordListOld() {
List pw = this.jdbcTemplate.query(
"select id pwid, PW_PASSWORD,PW_SOURCE,PW_USERNAME,remarks from pw.PASSWORD order by PW_SOURCE asc",
new PasswordMapper());
return pw;
}
    /*this will be the new method*/
public List getPasswordList() {

return getHibernateTemplate().find("from pw.PASSWORD");
}
/*Old method 2-*/
public void savePasswordOld(Password pw) {

logger.info("Saving password for the source: " + pw.getSource());
int count = this.jdbcTemplate.update(
"insert into pw.password (id, pw_source, pw_username,pw_password,remarks) values( nextval('pw.pw_seq'),?,?,?,?)",
new Object[] {pw.getSource(),pw.getUserName(),pw.getPassword(),pw.getRemarks()});
}
        /*This will be the new method*/
public void savePassword(Password password) {

logger.info("Saving password for the source: " + password.getSource());
getHibernateTemplate().save(password);
}
--------------------------------
Trouble shooting

After the above errors I got the error related to jar or class not found error, I resolved by adding the above mentioned jar files, so its working perfectly

Thank you, Please post your comments below

Related Posts
Share your thoughts

Friday, November 9, 2012

java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration

If you ever encounterd this error while working with hibernate? This error can be occur while converting JDBC based code to hibernate


SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestM
appingHandlerMapping#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating be
an with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: C
onstructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
I found the following Solution solves this:

1. Add hibernate-3.3.2.GA_CP03 in the jar file
Share your thoughts

Thursday, November 1, 2012

Simple menu example using CSS


This simple and elegant example of menu will help you to generate a simple menu for your website or web application
This is generated using HTML and css. please see below for the source code and screen shot of the menu.
#cssmenu{ height:37px; display:block; padding:0; margin: 0; border:1px solid; border-radius:5px; } 
#cssmenu > ul {list-style:inside none; padding:0; margin:0;}
#cssmenu > ul > li {list-style:inside none; padding:0; margin:0; float:left; display:block; position:relative;}
#cssmenu > ul > li > a{ outline:none; display:block; position:relative; padding:12px 20px; font:bold 13px/100% Arial, Helvetica, sans-serif; text-align:center; text-decoration:none; text-shadow:1px 1px 0 rgba(0,0,0, 0.4); }
#cssmenu > ul > li:first-child > a{border-radius:5px 0 0 5px;}
#cssmenu > ul > li > a:after{ content:''; position:absolute; border-right:1px solid; top:-1px; bottom:-1px; right:-2px; z-index:99; }
#cssmenu ul li.has-sub:hover > a:after{top:0; bottom:0;}
#cssmenu > ul > li.has-sub > a:before{ content:''; position:absolute; top:18px; right:6px; border:5px solid transparent; border-top:5px solid #fff; }
#cssmenu > ul > li.has-sub:hover > a:before{top:19px;}
#cssmenu ul li.has-sub:hover > a{ background:#3f3f3f; border-color:#3f3f3f; padding-bottom:13px; padding-top:13px; top:-1px; z-index:999; }
#cssmenu ul li.has-sub:hover > ul, #cssmenu ul li.has-sub:hover > div{display:block;}
#cssmenu ul li.has-sub > a:hover{background:#3f3f3f; border-color:#3f3f3f;}
#cssmenu ul li > ul, #cssmenu ul li > div{ display:none; width:auto; position:absolute; top:38px; padding:10px 0; background:#3f3f3f; border-radius:0 0 5px 5px; z-index:999; }
#cssmenu ul li > ul{width:200px;}
#cssmenu ul li > ul li{display:block; list-style:inside none; padding:0; margin:0; position:relative;}
#cssmenu ul li > ul li a{ outline:none; display:block; position:relative; margin:0; padding:8px 20px; font:10pt Arial, Helvetica, sans-serif; color:#fff; text-decoration:none; text-shadow:1px 1px 0 rgba(0,0,0, 0.5); }
#cssmenu, #cssmenu > ul > li > ul > li a:hover{ background:#333333; background:-moz-linear-gradient(top, #333333 0%, #222222 100%); background:-webkit-gradient(linear, left top, left bottom, color-stop(0%,#333333), color-stop(100%,#222222)); background:-webkit-linear-gradient(top, #333333 0%,#222222 100%); background:-o-linear-gradient(top, #333333 0%,#222222 100%); background:-ms-linear-gradient(top, #333333 0%,#222222 100%); background:linear-gradient(top, #333333 0%,#222222 100%); filter:progid:DXImageTransform.Microsoft.gradient( startColorstr='#333333', endColorstr='#222222',GradientType=0 ); }
#cssmenu{border-color:#000;}
#cssmenu > ul > li > a{border-right:1px solid #000; color:#fff;}
#cssmenu > ul > li > a:after{border-color:#444;}
#cssmenu > ul > li > a:hover{background:#111;}

The HTML page will embedd this Menu by calling the div menu id in the way
 <div id="cssmenu" >
And then define the menu in the following ways
<ul>
 <li class="active "><a href="http://localhost:8080/mm/login.htm"><span>Home</span></a></li&gt;
 <li class="has-sub "><a href="http://www.blogger.com/blogger.g?blogID=390582628921840995#"><span>Products</span></a&gt;
      <ul>
  <li><a href="http://localhost:8080/mm/p.htm"><span>Master</span></a></li&gt;
       <ul>
   <li><a href="http://www.blogger.com/addnew.htm"><span>Add New</span></a></li>
        </ul>
  <li><a href="http://localhost:8080/JavaServerFaces/"><span>Contacts</span></a></li&gt;
      </ul>
 </li>
 <li><a href="http://www.blogger.com/blogger.g?blogID=390582628921840995#"><span>About</span></a></li&gt;
 <li><a href="http://www.blogger.com/blogger.g?blogID=390582628921840995#"><span>Locations</span></a></li&gt;
</ul>


  See the screen shot here..
Share your thoughts