-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: one-to-many with xref tables
PostPosted: Mon Nov 07, 2005 3:32 pm 
Newbie

Joined: Tue Nov 01, 2005 5:39 pm
Posts: 11
I am having a terrible time understaning how to make this work. I have a table structure that represents and exam with many questions. Becsuse the qusetions can be created without an exam I have a lookup table between them with examid and questionid to connect questions to an exam.

I used the generator from myeclipseide's hibernate support and it created my xref table and mapping as a composit-id along with the exam and question objects. I created a java.util.Set in the exam class to hold the one-to-many questions with a getter/setter and where I am having difficulty is getting a mapping for the composit-id.

Below are the mappings. Does anyone else have an example of how to do a mapping involving an lookup table

Hibernate version: 2

Mapping documents:

Exam.hbm.xml
Code:

    <class name="Exam" table="exam">
        <id name="examid" column="examid" type="java.lang.Integer">
            <generator class="identity"/>
        </id>

        <property name="testType" column="test_type" type="java.lang.String" />
        <property name="name" column="name" type="java.lang.String" />
       
        <set name="Testquestions" lazy="true" cascade="all">
           <key column="examid"/>
           <many-to-many class="Testquestion">
              <column name="examid"/>
              <column name="questionid"/>
           </many-to-many>
        </set>
    </class>


Examquestionref.hbm.xml
Code:
    <class name="Examquesitonref" table="examquesitonref">
        <composite-id name="id" class="ExamquesitonrefKey">
            <key-property name="examid" column="ExamID" type="java.lang.Integer"/>
            <key-property name="quesionid" column="QuesionID" type="java.lang.Integer"/>
        </composite-id>

    </class>


Full stack trace of any exception that occurs:

11:42:29,118 WARN [JDBCExceptionReporter] SQL Error: 1054, SQLState: 42S22
11:42:29,118 ERROR [JDBCExceptionReporter] Unknown column 'examID' in 'field list'
11:42:29,133 INFO [STDOUT] net.sf.hibernate.exception.SQLGrammarException: could not insert: [com.campuspartners.webexit.dao.Testquestion]
at net.sf.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:69)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:30)
at net.sf.hibernate.persister.AbstractEntityPersister.convert(AbstractEntityPersister.java:1332)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:542)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:428)
at net.sf.hibernate.impl.ScheduledIdentityInsertion.execute(ScheduledIdentityInsertion.java:29)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:941)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:866)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:784)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:747)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1397)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:961)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:866)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:784)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:747)
at org.apache.jsp.test2_jsp._jspService(org.apache.jsp.test2_jsp:97)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
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.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:536)
Caused by: java.sql.SQLException: Unknown column 'examID' in 'field list'
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2926)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2978)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2902)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:933)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1162)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1079)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1064)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:520)
... 42 more
11:43:29,102 WARN [SessionImpl] afterTransactionCompletion() was never called


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 4:54 pm 
Newbie

Joined: Tue Nov 01, 2005 5:39 pm
Posts: 11
Ok, I am getting further. I changed the mapping a little and I have a new error.

changes to set in exam.hbm.xml
Code:

        <set name="Testquestions" lazy="true" table="childset">
            <key column="examid"/>
            <many-to-many
               class="com.campuspartners.webexit.dao.Testquestion"
               column="questionid"/>
        </set>


15:51:58,743 ERROR [SessionImpl] Could not synchronize database state with session
15:51:58,758 INFO [STDOUT] net.sf.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.campuspartners.webexit.dao.Testquestion


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 6:00 pm 
Newbie

Joined: Tue Nov 01, 2005 5:39 pm
Posts: 11
That second mapping worked, it is kind of a hassle that you have to save all of the pieces individually first before you can associate them together.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.