java - jpa/hibernate one-to-one mapping -
package com.sow.dao; import java.io.serializable; import javax.persistence.cascadetype; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.joincolumn; import javax.persistence.onetoone; import javax.persistence.table; import javax.xml.bind.annotation.xmlrootelement; @xmlrootelement @entity @table(name = "empcre") public class empauth implements serializable { @id @generatedvalue @column(name = "id", unique = true, nullable = false) int id; @column(name = "username") string username; @column(name = "userpwd") string pwd; @onetoone(mappedby = "empcre") @joincolumn(name = "id") private empdetails empdetails; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getpwd() { return pwd; } public void setpwd(string pwd) { pwd = pwd; } public empdetails getempdetails() { return empdetails; } public void setempdetails(empdetails empdetails) { this.empdetails = empdetails; } } package com.sow.dao; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.joincolumn; import javax.persistence.onetoone; import javax.persistence.table; import org.hibernate.annotations.genericgenerator; import org.hibernate.annotations.parameter; @entity @table(name = "empdetails") @genericgenerator(name = "person-primarykey", strategy = "foreign", parameters = { @parameter(name = "property", value = "empcre") }) public class empdetails { @id @generatedvalue(generator = "person-primarykey") // @genericgenerator(name = "gen", strategy = "foreign", parameters = // @parameter(name = "property", value = "empcre")) @column(name = "id", unique = true, nullable = false) int id; @column(name = "empname") string empname; @column(name = "address") string address; @column(name = "empsal") string empsal; @onetoone() @joincolumn(name = "id") private empauth empcre; public empauth getempcre() { return empcre; } public void setempcre(empauth empcre) { this.empcre = empcre; } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getempname() { return empname; } public void setempname(string empname) { this.empname = empname; } public string getaddress() { return address; } public void setaddress(string address) { this.address = address; } public string getempsal() { return empsal; } public void setempsal(string empsal) { this.empsal = empsal; } }
error
severe: servlet.service() servlet [jersey-serlvet] in context path [/restdemo] threw exception org.hibernate.id.identifiergenerationexception: attempted assign id null one-to-one property [com.sow.dao.empdetails.empcre] @ org.hibernate.id.foreigngenerator.generate(foreigngenerator.java:101) @ org.hibernate.event.internal.abstractsaveeventlistener.savewithgeneratedid(abstractsaveeventlistener.java:120) @ org.hibernate.event.internal.defaultmergeeventlistener.savetransiententity(defaultmergeeventlistener.java:422) @ org.hibernate.event.internal.defaultmergeeventlistener.mergetransiententity(defaultmergeeventlistener.java:342) @ org.hibernate.event.internal.defaultmergeeventlistener.entityistransient(defaultmergeeventlistener.java:304) @ org.hibernate.event.internal.defaultmergeeventlistener.onmerge(defaultmergeeventlistener.java:259) @ org.hibernate.event.internal.defaultmergeeventlistener.onmerge(defaultmergeeventlistener.java:86) @ org.hibernate.internal.sessionimpl.firemerge(sessionimpl.java:781) @ org.hibernate.internal.sessionimpl.merge(sessionimpl.java:766) @ org.hibernate.internal.sessionimpl.merge(sessionimpl.java:770) @ com.sow.userservice.newemp(userservice.java:100) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ com.sun.jersey.spi.container.javamethodinvokerfactory$1.invoke(javamethodinvokerfactory.java:60) @ com.sun.jersey.server.impl.model.method.dispatch.abstractresourcemethoddispatchprovider$typeoutinvoker._dispatch(abstractresourcemethoddispatchprovider.java:185) @ com.sun.jersey.server.impl.model.method.dispatch.resourcejavamethoddispatcher.dispatch(resourcejavamethoddispatcher.java:75) @ com.sun.jersey.server.impl.uri.rules.httpmethodrule.accept(httpmethodrule.java:288) @ com.sun.jersey.server.impl.uri.rules.righthandpathrule.accept(righthandpathrule.java:147) @ com.sun.jersey.server.impl.uri.rules.resourceclassrule.accept(resourceclassrule.java:108) @ com.sun.jersey.server.impl.uri.rules.righthandpathrule.accept(righthandpathrule.java:147) @ com.sun.jersey.server.impl.uri.rules.rootresourceclassesrule.accept(rootresourceclassesrule.java:84) @ com.sun.jersey.server.impl.application.webapplicationimpl._handlerequest(webapplicationimpl.java:1469) @ com.sun.jersey.server.impl.application.webapplicationimpl._handlerequest(webapplicationimpl.java:1400) @ com.sun.jersey.server.impl.application.webapplicationimpl.handlerequest(webapplicationimpl.java:1349) @ com.sun.jersey.server.impl.application.webapplicationimpl.handlerequest(webapplicationimpl.java:1339) @ com.sun.jersey.spi.container.servlet.webcomponent.service(webcomponent.java:416) @ com.sun.jersey.spi.container.servlet.servletcontainer.service(servletcontainer.java:537) @ com.sun.jersey.spi.container.servlet.servletcontainer.service(servletcontainer.java:699) @ javax.servlet.http.httpservlet.service(httpservlet.java:727) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:303) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:220) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:122) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:503) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:170) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:103) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:950) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:116) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:421) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1070) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:611) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:316) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(thread.java:744)
i trying achieve one-to-one relationship unidirectional using foreign key in associated table. while inserting date, inserting data in primary key table giving error insert above error.
you using primary key join column.
in @onetoone
, joincolumn
column references other side of relationship. since id
primary key column, shouldn't use foreign key column (it's name of referencing column, not referenced column).
change joincolumn
emp_id
(or remove default value.)
the exception caused way hibernate persists relationships, first inserts both sides of relationship, , update create relationship. here, update on primary key column, fails.
Comments
Post a Comment