Skip to main content

Posts

Struts 2 | ModelDriven to handle Form Data

In struts2, values of request parameters(HTML form's fields) are mapped to matching properties of the action class . Struts populates the values come in Http Request to the variables having same name of input fields of the HTML form. When we want to persist the data received form request, it needs to be in model object that can be persisted by ORM framework like JPA or Hibernate. So if we are using the Action class to handle the values from form, we have to mark it as Entity for JPA/Hibernate to persist its data. Therefore using the action class as Entity class is not a best practice. Struts2 provides another option to handle to form data. We can use separate Entity bean/Object to receive the form data rather than Action class. In terms of struts, it is called Model object. This is the same thing as Entity object in terms of JPA/Hibernate. In struts, we have to implement the ModelDriven interface that has the getModel method. This method returns the model object an...

Spring Framework : ApplicationContext instantiation for web applications in Spring Framework

In web application ApplicationContext is created using Context Loaders. there are two implementations of context loader. ContextLoaderListener : It is listener implementation that is added to web.xml file. ContextLoaderServlet : It is servlet implementation that is configured with load-on-startup tag in web.xml.   ContextLoaderListener is simple way to use the spring in web application. this listener accept contextConfigLocation parameter from context-parama. You have to enter following code in web.xml file define contextConfigLocation parameter in context-param tag. <context-param>   <param-name>contextConfigLocation</param-name>   <param-value>/WEB-INF/services.xml</param-value> </context-param>   services.xml is the Spring configuration file in which you define beans. Add the ContextLoaderListener listener. <listener>   <listener-class>org.springframework.web.context.ContextLoaderListener</li...

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(Defaul...

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 ()   {    ...

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...