aspose file tools*
The moose likes Struts and the fly likes unable to store values into table using hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "unable to store values into table using hibernate" Watch "unable to store values into table using hibernate" New topic
Author

unable to store values into table using hibernate

raghavendra raghu
Greenhorn

Joined: Sep 01, 2012
Posts: 16
hello...
Iam trying to create a small register application using struts and hibernate which creates a customer table in mysql DB and stores values in it using hibernate.but iam unable to store values into the table.
Error is:java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity.
.problem is that my session is not getting saved in my custimpl.java.
full stack trace is:
java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity
at org.hibernate.event.SaveOrUpdateEvent.<init>(SaveOrUpdateEvent.java:63)
at org.hibernate.event.SaveOrUpdateEvent.<init>(SaveOrUpdateEvent.java:46)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:551)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
at com.dao.CustomerImpl.addCustomer(CustomerImpl.java:23)
at com.action.Register.execute(Register.java:22)
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 com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source).
my programs one by one:
index.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Online Banking</title>
</head>
<body bgcolor="grey">
<h1 align="center">Welcome to Online Banking</h1>
<a href="registerpage"><h4>REGISTER</h4></a><br>
</body>
</html>

struts.xml:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.custom.i18n.resources" value="package"/>

<package name="default" extends="struts-default">

<action name="index">
<result>/index.html</result>
</action>
<action name="registerpage">
<result>/pages/register.jsp</result>
</action>
<action name="register"
class="com.action.Register"
method="execute">
<result name="success">/pages/success.jsp</result>
<result name="input">/pages/register.jsp</result>
</action>
</package>
</struts>

web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>OnlineBanking</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
my com.action package:
Customer.java:
package com.action;

import com.dao.CustomerImpl;
import com.opensymphony.xwork2.ActionSupport;

public class Customer extends ActionSupport {

@Override
public String toString() {
return "Customer [fname=" + fname + ", lname=" + lname + ", password="
+ password + ", email=" + email + ", gender=" + gender
+ ", phone=" + phone + "]";
}


String fname,lname,password,email,gender,phone;


public String getFname() {
return fname;
}


public Customer() {
super();
}


public Customer(String fname, String lname, String password, String email,
String gender, String phone) {
super();
this.fname = fname;
this.lname = lname;
this.password = password;
this.email = email;
this.gender = gender;
this.phone = phone;
}


public void setFname(String fname) {
this.fname = fname;
}


public String getLname() {
return lname;
}


public void setLname(String lname) {
this.lname = lname;
}


public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}


public String getEmail() {
return email;
}


public void setEmail(String email) {
this.email = email;
}


public String getGender() {
return gender;
}


public void setGender(String gender) {
this.gender = gender;
}


public String getPhone() {
return phone;
}


public void setPhone(String phone) {
this.phone = phone;
}
}

Register.java:

package com.action;

import com.action.Customer;
import com.dao.CustomerDAO;
import com.dao.CustomerImpl;


public class Register {
Customer customer;

public Customer getCustomer() {
return customer;
}

public void setCustomer(Customer customer) {
this.customer = customer;
}
public String execute(){
CustomerDAO ref = new CustomerImpl();
ref.addCustomer(getCustomer());
return "success";
}
}
Customer.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 1, 2013 10:33:44 AM by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
<class name="com.action.Customer" table="CUSTOMER">
<id name="fname" type="java.lang.String">
<column name="FNAME" />
<generator class="assigned" />
</id>
<property name="lname" type="java.lang.String">
<column name="LNAME" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" />
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" />
</property>
<property name="gender" type="java.lang.String">
<column name="GENDER" />
</property>
<property name="phone" type="java.lang.String">
<column name="PHONE" />
</property>
</class>
</hibernate-mapping>
my com.dao package:
CustomerDAO.java:

package com.dao;

import com.action.Customer;


public interface CustomerDAO {
void addCustomer(Customer c);

}
CustomerImpl.java:

package com.dao;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.Session;

import com.dao.CustomerDAO;
import com.action.Customer;
import com.dao.HibernateUtil;

public class CustomerImpl implements CustomerDAO{
SessionFactory factory;
@Override
public void addCustomer(Customer c) {
try{

factory = HibernateUtil.getSessionfactory();
Session session =factory.openSession();
Transaction t = session.beginTransaction();
session.save(c);//----------------------------->actual problem is here.hibernate doesnt fill my customer details which are entered through register.jsp.instead it returns c as null... session is not getting saved
t.commit();
session.close();
}
catch(Exception e){
e.printStackTrace();
}
}

}

HibernateUtil.java:
package com.dao;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
private static SessionFactory sessionfactory;
static{
sessionfactory=new Configuration().configure().buildSessionFactory();
}

public static SessionFactory getSessionfactory() {
return sessionfactory;
}
}

My Hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mysql</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="com.action.Customer" resource="com/action/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>

success.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Welcome
<s:property value="fname" />
</body>
</html>


Iam struggling with this problem since 3 days but every time i check in mysql DB through select command,values are not getting stored ...but Iam getting success page after registration is done!!
raghavendra raghu
Greenhorn

Joined: Sep 01, 2012
Posts: 16
sorry for too much coding...can anyone help me out?Im dying for the solution?
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Hello raghavendra raghu ,

First of all I request you to use the code block to disaplay coding. It is very difficult to read code in text format. Secondly I want you to paste your class com.action.Register ( this is the class which recieves the user model and attempts to save it , right ? ) .
Also I went throrugh your struts-config file and your action class, and it seems that you are using struts2 ( correct me if I am wrong ) . Now you said that you are getting your customer object is null ( i.e you are unable to get the customer model in your action class ) . You can change your Register action to a Model Driven action to recieve the whole User object and pass it to the DAO class. For this you just have to make some changes in your Register Action class. Just implement the Model interface and override the getModel() method. Should work fine



Do post back for any further queries.


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
raghavendra raghu
Greenhorn

Joined: Sep 01, 2012
Posts: 16
Thank you Saif asif for your input...
I have changed my Register action class to model driven class and I also included a printing statement but my problem is not solved...Iam getting a null value at my console which means Iam still unable to receive my customer object...same error save or update failed in CustImpl.java...any help regarding this?why is my session not saving?
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Hey wait , I just noticed something , your Customer class is not declared Serializable . How you expect hibernate to send this object in stream if its not declared serializable. This can be the reason why you are gettting a null entity. First change this and make sure your model class ( Customer class ) extends java.io.Serializable . This is important for hibernate.

Now if that doesnt solve the problem then we need to go in deeper details. Paste me the following.
1) register.jsp . This is the file where you input all the fields required to save a user ? If not , then paste that file.
2) Your Customer class ( I just noticed that you extended ActionSupport here , this is not required here. )
3) Your Register class . This is the one that is supposed to be extended with ActionSupport ( although not necessary )
4) Also make sure that your hibernate mapping matches with the schema of the table in your database.
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

You customer class should be like this

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unable to store values into table using hibernate