aspose file tools*
The moose likes Object Relational Mapping and the fly likes hibernate many to one question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate many to one question" Watch "hibernate many to one question" New topic
Author

hibernate many to one question

Fun Learning
Greenhorn

Joined: Jun 25, 2008
Posts: 3
Hi everyone,

I am developing a administrators web page to insert data into tables. I could insert data into the advisor_details table. I have another page to insert data into students table which has a foreign key(primary key from advisor_details table). The student insert page has a drop down menu which am populating it with the list of advisors from the advisor_details table. On entering all the information and selecting a value for advisor from drop down menu, clicin submit has to insert all the values into the student_details table. But I am confused as to how to insert that particular id of advisor into the student_details table. Below is the details of how I have been doing.

It would be great if anyone suggests me some tips...

Hibernate version: 3.2.4

Mapping documents:
AdvisorDetails.hbm.xml

<hibernate-mapping>
<class name="com.msee.model.entity.AdvisorDetails" table="advisor_details">
<id name="advisorId" type="java.lang.Integer">
<column name="advisor_id" />
<generator class="increment" />
</id>
<property name="advisorLoginId" type="java.lang.String">
<column name="advisor_login_id" length="20" />
</property>
<property name="advisorLoginPassword" type="java.lang.String">
<column name="advisor_login_password" length="20" />
</property>
<property name="advisorFirstName" type="java.lang.String">
<column name="advisor_first_name" length="30" />
</property>
<property name="advisorLasstName" type="java.lang.String">
<column name="advisor_lasst_name" length="30" />
</property>
<property name="advisorEmail" type="java.lang.String">
<column name="advisor_email" length="30" />
</property>
<set name="studentDetailses" inverse="true">
<key>
<column name="advisor_id" />
</key>
<one-to-many class="com.msee.model.entity.StudentDetails" />
</set>
</class>
</hibernate-mapping>

StudentDetails.hbm.xml:

<hibernate-mapping default-lazy="false">
<class name="com.msee.model.entity.StudentDetails" table="student_details">
<id name="studentId" type="java.lang.Integer">
<column name="student_id" />
<generator class="increment" />
</id>
<many-to-one name="advisorDetails" class="com.msee.model.entity.AdvisorDetails" fetch="select" insert="false">
<column name="advisor_id" />
</many-to-one>

<property name="studentLoginId" type="java.lang.String">
<column name="student_login_id" length="20" />
</property>
<property name="studentLoginPassword" type="java.lang.String">
<column name="student_login_password" length="20" />
</property>
<property name="studentFname" type="java.lang.String">
<column name="student_fname" length="30" />
</property>
<property name="studentLname" type="java.lang.String">
<column name="student_lname" length="30" />
</property>
<property name="studentEmail" type="java.lang.String">
<column name="student_email" length="40" />
</property>
<set name="coursePlans" inverse="true">
<key>
<column name="STUDENT_ID" />
</key>
<one-to-many class="com.msee.model.entity.CoursePlan" />
</set>
</class>
</hibernate-mapping>




Full stack trace of any exception that occurs:
SEVERE: Field 'advisor_id' doesn't have a default value
Jun 25, 2008 1:43:48 PM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
Error is in Hibernate DAO Support
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at com.msee.common.base.dao.BaseHibernateDaoSupport.updateDatabase(BaseHibernateDaoSupport.java:78)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at com.msee.common.base.dao.BaseHibernateDaoSupport.add(BaseHibernateDaoSupport.java:27)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.msee.model.dao.impl.StudentDetailsDaoImpl.saveStudent(StudentDetailsDaoImpl.java:86)
at com.msee.common.base.dao.BaseHibernateDaoSupport.updateDatabase(BaseHibernateDaoSupport.java:78)
at com.msee.model.service.impl.StudentDetailsServiceImpl.saveStudent(StudentDetailsServiceImpl.java:57)
at com.msee.common.base.dao.BaseHibernateDaoSupport.add(BaseHibernateDaoSupport.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at com.msee.model.dao.impl.StudentDetailsDaoImpl.saveStudent(StudentDetailsDaoImpl.java:86)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.msee.model.service.impl.StudentDetailsServiceImpl.saveStudent(StudentDetailsServiceImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:585)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:287)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:181)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:148)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:287)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:181)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:148)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at $Proxy6.saveStudent(Unknown Source)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at com.msee.view.bean.StudentAddBean.saveStudentPlan(StudentAddBean.java:69)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
at com.msee.view.bean.StudentAddBean.saveStudent(StudentAddBean.java:60)
at $Proxy6.saveStudent(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at com.msee.view.bean.StudentAddBean.saveStudentPlan(StudentAddBean.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.msee.view.bean.StudentAddBean.saveStudent(StudentAddBean.java:60)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:585)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:126)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)
at java.lang.reflect.Method.invoke(Method.java:585)
at javax.faces.component.UICommand.broadcast(UICommand.java:312)
at com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:126)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:381)
at javax.faces.component.UICommand.broadcast(UICommand.java:312)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:75)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:381)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:75)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at java.lang.Thread.run(Thread.java:595)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
Caused by: java.sql.BatchUpdateException: Field 'advisor_id' doesn't have a default value
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1669)
at java.lang.Thread.run(Thread.java:595)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1085)
Caused by: java.sql.BatchUpdateException: Field 'advisor_id' doesn't have a default value
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeBatch(DelegatingPreparedStatement.java:231)
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1669)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1085)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeBatch(DelegatingPreparedStatement.java:231)
... 54 more
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)



Name and version of the database you are using: MySql 5.0

The generated SQL (show_sql=true):

Hibernate: select advisordet0_.advisor_id as advisor1_7_, advisordet0_.advisor_login_id as advisor2_7_, advisordet0_.advisor_login_password as advisor3_7_, advisordet0_.advisor_first_name as advisor4_7_, advisordet0_.advisor_lasst_name as advisor5_7_, advisordet0_.advisor_email as advisor6_7_ from advisor_details advisordet0_ where advisordet0_.advisor_id<>5
Exit---Query--------------
get The Service Locator:::false
GetQuery FOr List::::::
session:::::::::::::::false
Hibernate: select advisordet0_.advisor_id as advisor1_7_, advisordet0_.advisor_login_id as advisor2_7_, advisordet0_.advisor_login_password as advisor3_7_, advisordet0_.advisor_first_name as advisor4_7_, advisordet0_.advisor_lasst_name as advisor5_7_, advisordet0_.advisor_email as advisor6_7_ from advisor_details advisordet0_ where advisordet0_.advisor_id<>5
Exit---Query--------------
Inserting student
get The Service Locator:::false
BaseHibernateDAOSupport adding
Adding ...
Hibernate: select max(student_id) from student_details
Hibernate: insert into student_details (student_login_id, student_login_password, student_fname, student_lname, student_email, student_id) values (?, ?, ?, ?, ?, ?)
Jun 25, 2008 1:43:48 PM org.hibernate.util.JDBCExceptionReporter logExceptions

Thank you.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

funand learning, please check your private messages.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate many to one question