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.  [ 4 posts ] 
Author Message
 Post subject: Collection mapping is only return 1 result
PostPosted: Tue May 12, 2009 6:12 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
HI, I was wondering if you can help me with a collection mapping problem. Right now, my collections are only returning one result. Does that sound like a common problem people run into? In both cases (hints and answers collections), there are 2 and 3 results. However, the query only gets 1 result. It's weird because individual queries on those id's of hints and answers work fine - they are not mapped properly, or stored in the list properly.

Here's my mapping
Code:
    <class name="jobprep.domain.Question" table="question">
        <id name="id" column="question_id" type="long">
            <generator class="sequence">
                <param name="sequence">question_id_sequence</param>
            </generator>
        </id>
        <property name="text" column="text"/>
       <bag name="answers" inverse="true" cascade="all-delete-orphan">
           <key column="answer_id" not-null="true"/>
           <one-to-many class="jobprep.domain.Answer" />
       </bag>
        <bag name="hints" inverse="true" cascade="all-delete-orphan">
            <key column="hint_id" not-null="true"/>
            <one-to-many class="jobprep.domain.Hint"/>
        </bag>
        <property name="points" column="points"/>
    </class>


Here's my object:

Code:
public class Question extends DomainObject {

   /* Members */
   private String text;
   List<Answer> answers = new ArrayList<Answer>();
   List<Hint> hints = new ArrayList<Hint>();
   private int points;

   /* Constructors */
   public Question() {

   }

   public Question( String text, Answer answer, int points ) {
      this.text = text;
      this.addAnswer( answer );
      this.points = points;
   }

   /* Services */
   public void addAnswer( Answer answer ) {
      Util.checkNullArgument( answer, "answer" );

      answers.add( answer );
   }

   public void removeAnswer( Answer answer ) {
      answers.remove( answer );
   }

   public boolean supplyAnswer( String answerText ) {
      for( Answer answer : answers ) {
         if( answer.matches( answerText ) ) {
            return true;
         }
      }

      return false;
   }

   public void addHint( Hint hint ) {
      Util.checkNullArgument( hint, "hint" );

      hints.add( hint );
   }

   public void removeHint( Hint hint ) {
      hints.remove( hint );
   }

   public Hint findHintByAttemptsNeeded( int attempts ) {
      sortHints();

      for( Hint hint : hints ) {
         if( attempts >= hint.getAttemptsNeeded() ) {
            return hint;
         }
      }

      return null;
   }

   void sortHints() {
      sort( hints, new Comparator<Hint>() {
         public int compare( Hint hint1, Hint hint2 ) {
            return hint2.getAttemptsNeeded() - hint1.getAttemptsNeeded();
         }

      } );
   }

   public int getHintThreshold() {
      int hintThreshold = Integer.MAX_VALUE;

      for( Hint hint : hints ) {
         if( hint.getAttemptsNeeded() < hintThreshold ) {
            hintThreshold = hint.getAttemptsNeeded();
         }
      }

      return hintThreshold;
   }

   /* Properties */
   public String getText() {
      return text;
   }

   public void setText( String text ) {
      this.text = text;
   }

   public List<Answer> getAnswers() {
      return answers;
   }

   public void setAnswers( List<Answer> answers ) {
      this.answers = answers;
   }

   public List<Hint> getHints() {
      return hints;
   }

   public void setHints( List<Hint> hints ) {
      this.hints = hints;
   }

   public int getPoints() {
      return points;
   }

   public void setPoints( int points ) {
      this.points = points;
   }

}


Domain Object is as follows:
Code:
public class DomainObject implements Serializable {

   /* Members */
   private long id;

   /* Constructors */

   /* Services */

   @Override
   public boolean equals( Object o ) {
      if( this == o ) return true;
      if( !( o instanceof DomainObject ) ) return false;

      DomainObject that = ( DomainObject ) o;

      if( id != that.id ) return false;

      return true;
   }

   @Override
   public int hashCode() {
      return ( int ) ( id ^ ( id >>> 32 ) );
   }

   /* Properties */
   public long getId() {
      return id;
   }

   public void setId( long id ) {
      this.id = id;
   }
}


Here is the query code in a Spring Dao:

Code:
   public Question find( long id ) {
      Question question = ( Question ) sessionFactory.getCurrentSession().createQuery(
          "from Question question where question.id = :id" )
         .setParameter( "id", id )
         .uniqueResult();

      if( question != null ) {
         Hibernate.initialize( question.getHints() );
         Hibernate.initialize( question.getAnswers() );
      }

      return question;
   }


Please help. Thank you.


Top
 Profile  
 
 Post subject: Re: Collection mapping is only return 1 result
PostPosted: Tue May 12, 2009 6:15 pm 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
Anyone? Please help. I'm really confused about this one. I used Hibernate before, and I don't recall this problem. I've tried no equals/hashcode, adding it into hint/answer... I've tried everything. Everything else in hibernate works great - just this doesn't work.


Top
 Profile  
 
 Post subject: Re: Collection mapping is only return 1 result
PostPosted: Wed May 13, 2009 1:39 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
Why do you use hint_id and answer_id as key? If these are the primary keys of Hint and Answer your mapping is wrong. You should use e.g. question_id as a reference to the primary key of Question.
Please post the mappings of hint and answer.


Top
 Profile  
 
 Post subject: Re: Collection mapping is only return 1 result
PostPosted: Wed May 13, 2009 3:37 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
That was totally the problem. Thank you! I actually understand why too... I just got into copy/paste mode and wasn't thinking. LOL. Thanks for catching that.


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