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.