2

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;
        }
    }

1 Answer 1

2

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

Struts2 hibernate integration via s2hibernate plugin

Sign up to request clarification or add additional context in comments.

5 Comments

after making these changes application is directly going to success page without making any database activity. can you put code for my Empaction.java class and Empdao.java class it will be more helpful.
what errors are in the log? What is hiberate.cfg.xml? Did you tested the database connection, schema creation, user creation, access privileges?
anyway these changes are worthless if you save an new or empty model. The rule is opposite, if you put nothing then nothing you get.
then how to put some thing on model what are the rules can you show me some likns
@tom Some links are added, a few changes to the configuration required if you will use hibernate4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.