I am using Struts 2 and Hibernate integration application to insert values in database. But after inserting values from a form filed only null
value is saving in database.
This is my form JSP file:
employee.jsp
:
<%@ taglib uri ="/struts-tags" prefix="s"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <s:form action="employee" method="post"> <s:textfield name ="name" label="ENTER your name"/> <s:textfield name="address" label="Enter Address"/> <s:submit label="submit">submit</s:submit> </s:form> </body> </html>
Entity class Empmodel.java
:
package model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Empmodel { @Id @GeneratedValue private int serialno; private String name; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
Connectionfactory.java
:
import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; public class ConnectionFactory { private static SessionFactory sessionfactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { Configuration config =null; SessionFactory sf =null; try{ config= new Configuration().configure(); ServiceRegistry servicereg = (ServiceRegistry) new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); sf= config.buildSessionFactory(servicereg); } catch(Throwable e){ System.out.println("Initial session factory creation failed"+ e); throw new ExceptionInInitializerError(e); } return sf; } public static SessionFactory getSessionFactory(){ return sessionfactory; }}
Empdao
is:
import model.Empmodel; import org.hibernate.Session; public class Empdao { public Empmodel add(){ Empmodel model = new Empmodel(); Session session=ConnectionFactory.getSessionFactory().getCurrentSession(); session.beginTransaction(); session.save(model); session.getTransaction().commit(); return model; } }
Action
class is:
import model.Empmodel; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import dao.Empdao; public class Empaction extends ActionSupport implements ModelDriven<Empmodel> { private static final long serialVersionUID = 1L; private Empmodel model; public String execute() throws Exception{ Empdao empdao = new Empdao(); Empmodel queryresult = empdao.add(); if(queryresult != null) { return SUCCESS; } else return ERROR; } @Override public Empmodel getModel() { // TODO Auto-generated method stub return model; } }
Answer
if you want to get something first you need to put something. This rule works everywhere. In your code you need to put the model object to DAO. Then DAO will save it to the db using the values populated in the model via the interceptor. For example
Empmodel queryresult = empdao.add(model);
For this to work you need to change the method signature to add a parameter for model
.
The thing seems trivial but you need to remove the statement that recreate the model
in the method implementation, it is not working fine. Also make sure that the model is not empty before you start transaction.
The save
method in DAO should check if the new object is created that has null
value for id
.
if (model.getId() == null) session.save(model); else session.update(model); }
How to implement a ModelDriven
with integration hibernate example