Skip to main content

Posts

Showing posts from 2011

org.hibernate.HibernateException: Javassist Enhancement failed

[main] INFO org.hibernate.event.def.DefaultLoadEventListener - Error performing load command org.hibernate.HibernateException: Javassist Enhancement failed: entities.Employee     at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxy(JavassistLazyInitializer.java:143)     at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:72)     at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:634)     at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3713)     at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:360)     at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:281)     at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)     at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)    

Using HQL in Hibernate

HQL stands for Hibernate Query Language that works on persistent objects and their properties. It is similar to SQL but we use the class and Properties in syntax of query instead of table and columns names. It is case sensitive while we write the name of java classes and properties. You have to user Class name to be mapped to table instead of database table name. SQL Table package  domain; import  javax.persistence. * ; @ Entity public   class  Employee {      private   int  empId;      private   String  name;      private   String  email;      private   String  designation;      private   long  salary;      private   String  department;     @Id @GeneratedValue      public   int   getEmpId ()   {          return  empId;      }      public   void   setEmpId ( int  empId )   {          this .empId  =  empId;      }      public   String   getName ()   {          return  name;      }      public   void   setName ( String  name )   {          this .name  =  name;

Inheritance mapping in hibernate :Table per class

This mapping is not known as good mapping for projects. In this approach all of the fields of each type in the inheritance hierarchy are stored in distinct tables. It has many drawbacks while dealing with polymorphic and association. This strategy supports bidirectional one-to-many association. package  domain; import  javax.persistence. * ; @ Entity @ Inheritance ( strategy = InheritanceType.TABLE_PER_CLASS ) public   class  Employee {      private   int  empId;      private   String  name;      private   String  email;     @Id       public   int   getEmpId ()   {          return  empId;      }      public   void   setEmpId ( int  empId )   {          this .empId  =  empId;      }      public   String   getName ()   {          return  name;      }      public   void   setName ( String  name )   {          this .name  =  name;      }      public   String   getEmail ()   {          return  email;      }      public   void   setEmail ( String  email )   {

Inheritance mapping in hibernate :JOINED strategy

We can map each class in individual table in database using JOINED strategy. using @Inheritance(strategy=InheritanceType.JOINED) annotation, we can define this strategy. In this approach, primary key of the super class works as foreign key in sub class.So at the time of dealing with data, Hibernate Joins the tables for getting consolidated values for Subclass Object. you can use @PrimaryKeyJoinColumn and @PrimaryKeyJoinColumns annotations to define the primary key(s) of the joined subclass table. This approach is better than single table strategy.   package  domain; import  javax.persistence. * ; @ Entity @ Inheritance ( strategy = InheritanceType.JOINED ) public   class  Employee {      private   int  empId;      private   String  name;      private   String  email;     @Id @GeneratedValue      public   int   getEmpId ()   {          return  empId;      }      public   void   setEmpId ( int  empId )   {          this .empId  =  empId;      }