Spring MVC + Hibernate + one to one mapping : null pointer exception while session.save -
problem definition:
user_table , user_detail_table onetoone mapped userid field. facing 2 problems!
encounter nullpointer exception while saving userdetailtable.
- first setting usertable , saving userdetailtable.
userid value generated 0, though use nullable=false. -also sometime see userid value user_table 1 , userid user_detail_table 0. specially when have inserted 1 row 'admin' while creating tables.
i want have default 'admin' userid inserted while creating database.
i using layout given following site http://websystique.com/springmvc/spring-4-mvc-and-hibernate4-integration-example-using-annotations/
using appinitializer, appconfig , hibernate configuration above example.
i update appconfig include tiles, , jsp pages, 2 viewresolvers 1 , 2 ordering respectively.
to keep things short, giving code snippets
user.class
@entity @table(name="user_table") public class user{ @id @generatedvalue(strategy = generationtype.identity) @column(name="userid", nullable=false) private int userid; ... ... }
userdetail.class
@entity @table(name="user_detail_table") public class userdetail { //@id @column(name="userid", nullable=false) @id @generatedvalue(generator = "genuserid") @genericgenerator(name = "genuserid", strategy="foreign", parameters = @parameter(value="user_table", name = "property") ) private int userid; private string firstname; private string lastname; //@primarykeyjoincolumn @onetoone(cascade = cascadetype.all) @joincolumn(name="userid") private user user; public user getuser() { return user; } public void setuser(user user) { this.user = user; }
userdetaildao class
@repository("userdetaildao") public class userdetaildaoimpl extends abstractdao implements userdetaildao { @override public void saveuserdetail( userdetail userdetail, user user) { // time userdetail.userid 0!! // , user.userid 1 !! // setting user table info userdetail.setuser(user); //persist(userdetail); //getsession().begintransaction( getsession().save(userdetail); //getsession().gettransaction().commit(); }
userdetailservice class
@service("userdetailservice") @transactional public class userdetailserviceimpl implements userdetailservice{ @autowired private userdetaildao userdetaildao; @override public void saveuserdetail(userdetail userdetail,user user) { userdetaildao.saveuserdetail(userdetail, user); }
controller class
@controller public class webmaincontroller { @autowired userdetailservice userdetailservice; //for add , update person both @requestmapping(value= "useradd", method = requestmethod.post) public string addperson( @modelattribute("userdetail") userdetail userdetail,bindingresult resultuser, @modelattribute("user") user user,bindingresult resultuserdetail ) { //userdetail userdetail = new userdetail(); //userdetail.setuser(user); userdetailservice.saveuserdetail(userdetail, user); return "redirect:/useradd"; }
using mysql: sql script
create table user_table ( userid int(10) not null auto_increment, loginname varchar(16) not null , password varchar(32) not null , primary key (userid) )engine=innodb default charset=utf8; create table user_detail_table( userid int(10) not null, firstname varchar(16) , primary key (userid), foreign key (userid) references user_table (userid) on update cascade on delete cascade )engine=innodb default charset=utf8; insert user_table(userid, loginname,password) values (1,'admin','admin'); insert user_detail_table(userid, firstname) values (1,'administrator');
one more question
- can manually insert row in 'user' , 'user_detail' table have 'admin' field? annotation @generatedvalue check database , increment (generate) value userid, right?
exception error follows
message request processing failed; nested exception java.lang.nullpointerexception description server encountered internal error prevented fulfilling request. exception org.springframework.web.util.nestedservletexception: request processing failed; nested exception java.lang.nullpointerexception org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:978) org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:868) javax.servlet.http.httpservlet.service(httpservlet.java:644) org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:842) javax.servlet.http.httpservlet.service(httpservlet.java:725) org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) root cause java.lang.nullpointerexception org.hibernate.tuple.entity.abstractentitytuplizer.getpropertyvalue(abstractentitytuplizer.java:650) org.hibernate.persister.entity.abstractentitypersister.getpropertyvalue(abstractentitypersister.java:4736) org.hibernate.id.foreigngenerator.generate(foreigngenerator.java:96) org.hibernate.event.internal.abstractsaveeventlistener.savewithgeneratedid(abstractsaveeventlistener.java:117) org.hibernate.event.internal.defaultsaveorupdateeventlistener.savewithgeneratedorrequestedid(defaultsaveorupdateeventlistener.java:209) org.hibernate.event.internal.defaultsaveeventlistener.savewithgeneratedorrequestedid(defaultsaveeventlistener.java:55) org.hibernate.event.internal.defaultsaveorupdateeventlistener.entityistransient(defaultsaveorupdateeventlistener.java:194) org.hibernate.event.internal.defaultsaveeventlistener.performsaveorupdate(defaultsaveeventlistener.java:49) org.hibernate.event.internal.defaultsaveorupdateeventlistener.onsaveorupdate(defaultsaveorupdateeventlistener.java:90) org.hibernate.internal.sessionimpl.firesave(sessionimpl.java:715) org.hibernate.internal.sessionimpl.save(sessionimpl.java:707) org.hibernate.internal.sessionimpl.save(sessionimpl.java:702) com.cms.sweb.dao.userdetaildaoimpl.saveuserdetail(userdetaildaoimpl.java:49) com.cms.sweb.service.userdetailserviceimpl.saveuserdetail(userdetailserviceimpl.java:23) sun.reflect.nativemethodaccessorimpl.invoke0(native method) sun.reflect.nativemethodaccessorimpl.invoke(unknown source) sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) java.lang.reflect.method.invoke(unknown source) org.springframework.aop.support.aoputils.invokejoinpointusingreflection(aoputils.java:317) org.springframework.aop.framework.reflectivemethodinvocation.invokejoinpoint(reflectivemethodinvocation.java:190) org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:157) org.springframework.transaction.interceptor.transactioninterceptor$1.proceedwithinvocation(transactioninterceptor.java:99) org.springframework.transaction.interceptor.transactionaspectsupport.invokewithintransaction(transactionaspectsupport.java:281) org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor.java:96) org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:179) org.springframework.aop.framework.jdkdynamicaopproxy.invoke(jdkdynamicaopproxy.java:207) com.sun.proxy.$proxy40.saveuserdetail(unknown source) com.cms.sweb.controller.webmaincontroller.addperson(webmaincontroller.java:87) sun.reflect.nativemethodaccessorimpl.invoke0(native method) sun.reflect.nativemethodaccessorimpl.invoke(unknown source) sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) java.lang.reflect.method.invoke(unknown source) org.springframework.web.method.support.invocablehandlermethod.doinvoke(invocablehandlermethod.java:221) org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod.java:137) org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:110) org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlemethod(requestmappinghandleradapter.java:776) org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:705) org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:85) org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:959) org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:893) org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:966) org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:868) javax.servlet.http.httpservlet.service(httpservlet.java:644) org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:842) javax.servlet.http.httpservlet.service(httpservlet.java:725) org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) note full stack trace of root cause available in apache tomcat/8.0.20 logs.
if miss anything, let me know add it. in advance!
i think problem ids of rows added on tables, maybe should add row in table user_table
@ first , it's id , add new row in tabel user_detail_table
id of new row added in first table, have same issue , thinking solution, try , tell me if works fine
Comments
Post a Comment