-->
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.  [ 2 posts ] 
Author Message
 Post subject: Insertion problem with null PK (backref)
PostPosted: Sat Jul 08, 2006 11:31 pm 
Newbie

Joined: Thu Jul 06, 2006 3:06 am
Posts: 6
Hi all :)

I'm having a problem with an insertion not generating a primary key value for an entity. This is the only class in my application where this problem is happening, and I'm not sure why. The session factory, on configuration, reports no errors or warnings. The class has no problems on creation or during interaction with other persistent entities.

Here's the class's mapping:
Code:
<hibernate-mapping>
   <class name="QuestionFeedback" table="users_trivia_game_questions_feedback">
      <id name="userTriviaGameQuestionFeedbackId" type="int">
         <column name="user_trivia_game_question_feedback_id"/>
         <generator class="native"/>
      </id>
      
      <many-to-one name="question" column="question_id" class="Questions" not-null="true"/>
      <many-to-one name="triviaGame" column="user_trivia_game_id" class="TriviaGame" not-null="true"/>
      <many-to-one name="generalFeedback" column="general_feedback" class="FeedbackType" insert="false" update="false"/>
      
      <property name="providedAnswer" type="int" column="provided_answer" not-null="true"/>
      <property name="detailedFeedback" type="string" column="detailed_feedback"/>
   </class>

</hibernate-mapping>


The relevant code that fails is this:
Code:
      Session session = HibernateSessionFactory.getSession();
      Transaction tx = session.beginTransaction();
      
      SinglePlayerGame triviaGame = (SinglePlayerGame)session.getNamedQuery("triviaGameById").setInteger("userTriviaGameId", m_TriviaGameId).uniqueResult();

      ...

      int correctQuestions = 0;
      Set<QuestionFeedback> gameFeedback = triviaGame.getQuestionFeedback();
      for(UsedQuestion uq : m_UsedQuestions.values())
      {
         Questions question = (Questions)session.getNamedQuery("questionById").setInteger("questionId", uq.getQuestionId()).uniqueResult();
         Set<QuestionFeedback> questionFeedback = question.getQuestionFeedback();
         
         QuestionFeedback qf = new QuestionFeedback(triviaGame, question, uq.getProvidedAnswer());
         questionFeedback.add(qf);
         gameFeedback.add(qf);

         session.save(qf);
         
         if(uq.isCorrect())
            ++correctQuestions;
      }
      
      triviaGame.setCorrectQuestions(correctQuestions);
      
      tx.commit();


The code fails on the call to session.save(). The error I get is:
Code:
Error returned from server: not-null property references a null or transient value: QuestionFeedback._questionFeedbackBackref


Tracing through the code, I get this failure from the Nullability class, when it checks the class's fields -- the field that fails is the ID field, which, to me, implies that the ID generator isn't working. The database I'm using is MySQL 5.1, and the column in question is marked as auto_increment.

As I mentioned, this is the only class in my app that has this problem. Any advice as to where to start looking for a solution would be greatly appreciated.

Thanks in advance.

C


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 09, 2006 2:03 am 
Newbie

Joined: Thu Jul 06, 2006 3:06 am
Posts: 6
Please disregard this post. I found the cause of the problem and fixed it.

For the curious, you didn't have enough information to help me to begin with: the QuestionFeedback class was mapped into two different collections in other entities (Questions and FeedbackType).

It appears that, as an accident of the main hibernate.cfg file, the Nullability checker was using FeedbackType's set declaration rather than Questions's -- FeedbackType was incorrectly setting the association to not-null, causing many problems and headaches.

Thanks if you made it this far -- sorry to bug yas.

C


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.