-->
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: OneToMany relationship returning only one row
PostPosted: Tue Jun 12, 2007 5:51 pm 
Newbie

Joined: Tue Jun 12, 2007 9:28 am
Posts: 8
Location: Ohio, Mississippi, Michigan
I am writing my first application using Hibernate and have run across an error that seems like it should be simple to fix, but has kept me busy for days.

I am writing a webapp that will be used to give surveys. I have a full schema already written in DDL and am trying to adapt it to work with Hibernate Annotations. I am trying to make a one-to-many relationship between Question and Choice. (A Question has many Choice's.) When I call getChoices() on Question, a List with only the first Choice is returned instead all of the Question's Choices. If I execute a raw HQL query on the Choice entity for all Choices that have the specified Question, then all desired Choices are returned.

I have noticed that the generated SQL filters the results on choice.choice_id instead of choice.question_id, which seems incorrect to me. Also, if you look at the foreign key constraints that Hibernate wants to generate if you run hbm2ddl, you get the constraints in 'pairs', in which one is the desired FK constraint and one is seemingly not valid:

Code:
-- not so good
alter table choice
    add constraint question_choice_fk
    foreign key (choice_id)
    references question

-- good
alter table choice
    add constraint question_choice_fk
    foreign key (question_id)
    references question


I am guessing that I am just doing something incorrect with OneToMany or ManyToOne since this seems like a rather simple task. I've dug through the CaveatEmptor (Native Hibernate and JPA) Demo and looked at the Hibernate Annotations test classes, but I can't seem to find anything that I do differently from them. I tried moving the annotations to the bean getters, but, when I did, the OneToMany and/or ManyToOne annotations threw errors upon initialization. (It also forced me to make setters for the id and version attributes, all though I have since ditched them upon moving the annotations back to the member variables.)

Thanks in advance,

Chris Lieb
Mississippi Department of Environmental Quality

BTW, sorry for the obscenely long post, but I wanted to make sure that I got in as much detail as possible.

-------------------------------------------------------------------------------------------------------------

Hibernate version: 3.2.4.sp1
Hibernate Annotations version: 3.3.0.ga

relevant classes:
Question.java (sans imports) wrote:
Code:
/**
* A Question in a Survey.  A Question can exist in multiple Surveys.
*/
@Entity
@Table(
   name = "question",
   uniqueConstraints = {
      @UniqueConstraint(columnNames = {"text"})
   }
)
public class Question implements Serializable {
   /** The maximum length of the text of a Question. **/
   public static final int MAX_QUESTION_LENGTH = 4000;
   
   /** The version of this class.  Used for serialization. **/
   private static final long serialVersionUID = 1L;
   
   /** The class logger. **/
   @Transient
   private final Log log = LogFactory.getLog(Question.class);
   
   /** The Question's unique id number. **/
   @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "question_id", nullable = false,
         columnDefinition = "integer")
   private Long id = null;
   
   /** The Question's version number. **/
   @Version
   @Column(name = "version", nullable = false)
   private int version = 0;
   
   /** The Question's text. **/
   @Column(name = "text", nullable = false,
         length = Question.MAX_QUESTION_LENGTH)
   private String text;
   
   /** The type of question that this Question is. **/
   @Enumerated
   @Column(name = "question_type_id", nullable = false)
   private QuestionType questionType;
   
   /**
    * The minimum number of results that are required to be provided by this
    * Question.  Only used for {@link QuestionType}.SelectMany.
    */
   @Column(name = "min_results")
   private Integer minResults = null;
   
   /** The list of Choices for this question. **/
   @OneToMany(mappedBy = "id")
   @ForeignKey(name = "question_choice_fk")
   @OrderBy("position")
   private List<Choice> choices = new LinkedList<Choice>();
   
   /** The set of SurveyQuestions that use this Question. **/
   @OneToMany(mappedBy = "id")
   @ForeignKey(name = "question_survey_question_fk")
   private Set<SurveyQuestion> surveyQuestions = new HashSet<SurveyQuestion>();
   
   public Question() {}
   
   public Question(String qText, QuestionType qType) {
      this.setText(qText);
      this.setQuestionType(qType);
   }
   
   public Question(String qText, QuestionType qType, List<Choice> qChoices) {
      this.setText(qText);
      this.setQuestionType(qType);
      this.setChoices(qChoices);
   }
   
   public Long getId() {
      return this.id;
   }
   
   public int getVersion() {
      return this.version;
   }
   
   public String getText() {
      return this.text;
   }
   
   public void setText(String qText) {
      this.text = qText;
   }
   
   public QuestionType getQuestionType() {
      return this.questionType;
   }
   
   public void setQuestionType(QuestionType qType) {
      this.questionType = qType;
   }
   
   public Integer getMinResults() {
      return this.minResults;
   }
   
   public void setMinResults(Integer qMinResults) {
      this.minResults = qMinResults;
   }
   
   public List<Choice> getChoices() {
      return this.choices;
   }
   
   public void setChoices(List<Choice> qChoices) {
      this.choices = qChoices;
   }
   
   public void addChoice(Choice qChoice) {
      if (qChoice == null) {
         throw new IllegalArgumentException("Null value passed");
      }
      
      // add the new choice
      if (qChoice.getPosition() == null) {
         this.getChoices().add(qChoice);
      } else {
         this.getChoices().add(qChoice.getPosition(), qChoice);
      }
      
      // reindex the choices list
      for (int i = qChoice.getPosition(); i < this.getChoices().size(); i++) {
         this.getChoices().get(i).setPosition(i);
      }
   }
   
   public void removeChoice(Choice qChoice) {
      if (qChoice == null) {
         throw new IllegalArgumentException("Null value passed");
      }
      
      // remove the choice
      this.getChoices().remove(qChoice);
      
      // reindex the choices list
      for (int i = qChoice.getPosition(); i < this.getChoices().size(); i++) {
         this.getChoices().get(i).setPosition(i);
      }
   }
   
   // removed methods for surveyQuestions (get, set, add, remove)
   
   @Override
   public String toString() {String out = "";
      out += "Question: (id: " + this.getId() + "; version: "
            + this.getVersion() + ")";
      return out;
   }
}


Choice.java (sans imports) wrote:
Code:
/**
* A choice for a Question.  A question can have between 0 and inf.
* choices.  Note that choices belong to Question's, not
* SurveyQuestion's.
*/
@Entity
@Table(
   name = "choice",
   uniqueConstraints = {
      @UniqueConstraint(columnNames = {"question_id", "text"}),
      @UniqueConstraint(columnNames = {"question_id", "position"})
   }
)
public class Choice implements Serializable {
   /** The maximum length of the text of a Choice. **/
   public static final int MAX_TEXT_LENGTH = 2000;
   
   /** The version of this class.  Used for serialization. **/
   private static final long serialVersionUID = 1L;
   
   /** The class logger. **/
   @Transient
   private final Log log = LogFactory.getLog(Choice.class);
   
   /** The Choice's unique id number. **/
   @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "choice_id", columnDefinition = "integer", nullable = false)
   private Long id = null;
   
   /** The Choice's version number. **/
   @Version
   @Column(name = "version", nullable = false)
   private int version = 0;
   
   /** The Question that this Choice belongs to. **/
   @ManyToOne
   @JoinColumn(name = "question_id", nullable = false,
         columnDefinition = "integer")
   @ForeignKey(name = "question_choice_fk")
   private Question question;
   
   /** The text to display for this Choice. **/
   @Column(name = "text", nullable = false, length = Choice.MAX_TEXT_LENGTH)
   private String text;
   
   /** The position in the list of Choices to display this Choice. **/
   @IndexColumn(name = "position")
   private Integer position;
   
   /** Whether this is an 'Other' choice. **/
   @Column(name = "is_other", nullable = false)
   private boolean isOther = false;
   
   /** The results that have been submitted that used this Choice. **/
   @OneToMany(mappedBy = "id")
   @ForeignKey(name = "choice_result_fk")
   private Set<Result> results = new HashSet<Result>();
   
   public Choice() {}
   
   public Choice(Question cQuestion, String cText) {
      this.setQuestion(cQuestion);
      this.setText(cText);
   }
   
   public Choice(Question cQuestion, String cText, Boolean cOther) {
      this.setQuestion(cQuestion);
      this.setText(cText);
      this.setOther(cOther);
   }
   
   public Long getId() {
      return this.id;
   }
   
   public int getVersion() {
      return this.version;
   }
   
   public Question getQuestion() {
      return this.question;
   }
   
   public void setQuestion(Question cQuestion) {
      this.question = cQuestion;
   }
   
   public String getText() {
      return this.text;
   }
   
   public void setText(String cText) {
      this.text = cText;
   }
   
   public Integer getPosition() {
      return this.position;
   }
   
   public void setPosition(Integer cPosition) {
      this.position = cPosition;
   }
   
   public boolean isOther() {
      return this.isOther;
   }
   
   public void setOther(boolean cOther) {
      this.isOther = cOther;
   }
   
   // removed methods for results (get, set, add, remove)
   
   @Override
   public String toString() {
      String out = "";
      out += "Choice: (id: " + this.id + "; version: " + this.version + ")";
      return out;
   }
}

Code between sessionFactory.openSession() and session.close():

Code:
// put the survey question into the request
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();

survey = (Survey)session.get(Survey.class, survey.getId());

req.setAttribute("sq", survey.getSurveyQuestions().get(sqNum));

session.getTransaction().commit();


Name and version of the database you are using: Apache Derby 10.1.1.0

The generated SQL (show_sql=true):
Code:
select
   surveyques0_.survey_question_id as survey1_3_,
   surveyques0_.survey_question_id as survey1_2_2_,
   surveyques0_.alt_text as alt2_2_2_,
   surveyques0_.position as position2_2_,
   surveyques0_.question_id as question6_2_2_,
   surveyques0_.survey_id as survey5_2_2_,
   surveyques0_.version as version2_2_,
   question1_.question_id as question1_1_0_,
   question1_.min_results as min2_1_0_,
   question1_.question_type_id as question3_1_0_,
   question1_.text as text1_0_,
   question1_.version as version1_0_,
   survey2_.survey_id as survey1_0_1_,
   survey2_.name as name0_1_,
   survey2_.version as version0_1_
from
   survey_question surveyques0_
inner join
   question question1_
      on surveyques0_.question_id=question1_.question_id
inner join
   survey survey2_
      on surveyques0_.survey_id=survey2_.survey_id
where
   surveyques0_.survey_question_id=?
order by
   surveyques0_.position asc

select
   choices0_.choice_id as choice1_2_,
   choices0_.choice_id as choice1_3_1_,
   choices0_.is_other as is2_3_1_,
   choices0_.position as position3_1_,
   choices0_.question_id as question6_3_1_,
   choices0_.text as text3_1_,
   choices0_.version as version3_1_,
   question1_.question_id as question1_1_0_,
   question1_.min_results as min2_1_0_,
   question1_.question_type_id as question3_1_0_,
   question1_.text as text1_0_,
   question1_.version as version1_0_
from
   choice choices0_
inner join
   question question1_
      on choices0_.question_id=question1_.question_id
where
   choices0_.choice_id=?
order by
   choices0_.position asc


relevant Schema:
Code:
CREATE TABLE question (
   question_id integer generated by default as identity,
   question_type_id integer not null,
   text varchar(4000) not null,
   min_results integer,
   version integer not null default 0,
   CONSTRAINT question_pk
      PRIMARY KEY (question_id),
   CONSTRAINT question_u
      UNIQUE (text)
);
CREATE TABLE choice (
   choice_id integer generated by default as identity,
   question_id integer not null,
   text varchar(2000) not null,
   position integer not null,
   is_other smallint not null,
   version integer not null default 0,
   CONSTRAINT choice_pk
      PRIMARY KEY (choice_id),
   CONSTRAINT choice_question_text_u
      UNIQUE (question_id, text),
   CONSTRAINT choice_question_position_u
      UNIQUE (question_id, position),
   CONSTRAINT question_choice_fk
      FOREIGN KEY (question_id)
      REFERENCES question (question_id)
);


Test data:
Code:
INSERT INTO question (
   text,
   question_type_id,
   min_results
) VALUES (
   'What industry are you associated with?',
   0,
   null
);

INSERT INTO choice (
   question_id,
   text,
   position,
   is_other
) VALUES (
   (SELECT question_id FROM question WHERE text = 'What industry are you associated with?'),
   'Energy and Transportation',
   0,
   0
);

INSERT INTO choice (
   question_id,
   text,
   position,
   is_other
) VALUES (
   (SELECT question_id FROM question WHERE text = 'What industry are you associated with?'),
   'Metal &amp; Metal Manufacturing',
   1,
   0
);

INSERT INTO choice (
   question_id,
   text,
   position,
   is_other
) VALUES (
   (SELECT question_id FROM question WHERE text = 'What industry are you associated with?'),
   'Agricultural',
   2,
   0
);

INSERT INTO choice (
   question_id,
   text,
   position,
   is_other
) VALUES (
   (SELECT question_id FROM question WHERE text = 'What industry are you associated with?'),
   'Construction &amp; Building',
   3,
   0
);

INSERT INTO choice (
   question_id,
   text,
   position,
   is_other
) VALUES (
   (SELECT question_id FROM question WHERE text = 'What industry are you associated with?'),
   'Municipal',
   4,
   0
);

INSERT INTO choice (
   question_id,
   text,
   position,
   is_other
) VALUES (
   (SELECT question_id FROM question WHERE text = 'What industry are you associated with?'),
   'Solid Waste',
   5,
   0
);

INSERT INTO choice (
   question_id,
   text,
   position,
   is_other
) VALUES (
   (SELECT question_id FROM question WHERE text = 'What industry are you associated with?'),
   'Mining',
   6,
   0
);

INSERT INTO choice (
   question_id,
   text,
   position,
   is_other
) VALUES (
   (SELECT question_id FROM question WHERE text = 'What industry are you associated with?'),
   'Timber &amp; Wood Products',
   7,
   0
);

INSERT INTO choice (
   question_id,
   text,
   position,
   is_other
) VALUES (
   (SELECT question_id FROM question WHERE text = 'What industry are you associated with?'),
   'Service Industries',
   8,
   0
);


Debug level Hibernate log excerpt:
Code:
Jun-12 16:01:03 DEBUG [SessionImpl] - opened session at timestamp: 11816820631
Jun-12 16:01:03 DEBUG [JDBCTransaction] - begin
Jun-12 16:01:03 DEBUG [ConnectionManager] - opening JDBC connection
Jun-12 16:01:03 DEBUG [JDBCTransaction] - current autocommit status: true
Jun-12 16:01:03 DEBUG [JDBCTransaction] - disabling autocommit
Jun-12 16:01:03 DEBUG [Loader] - loading entity: [us.ms.state.deq.surveyor.model.Survey#1]
Jun-12 16:01:03 DEBUG [AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
Jun-12 16:01:03 DEBUG [SQL] -
   select
      survey0_.survey_id as survey1_0_0_,
      survey0_.name as name0_0_,
      survey0_.version as version0_0_
   from
      survey survey0_
   where
      survey0_.survey_id=?
Hibernate:
   select
      survey0_.survey_id as survey1_0_0_,
      survey0_.name as name0_0_,
      survey0_.version as version0_0_
   from
      survey survey0_
   where
      survey0_.survey_id=?
Jun-12 16:01:03 DEBUG [AbstractBatcher] - about to open ResultSet (open ResultSets: 0, globally: 0)
Jun-12 16:01:03 DEBUG [Loader] - result row: EntityKey[us.ms.state.deq.surveyor.model.Survey#1]
Jun-12 16:01:03 DEBUG [AbstractBatcher] - about to close ResultSet (open ResultSets: 1, globally: 1)
Jun-12 16:01:03 DEBUG [AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
Jun-12 16:01:03 DEBUG [TwoPhaseLoad] - resolving associations for [us.ms.state.deq.surveyor.model.Survey#1]
Jun-12 16:01:03 DEBUG [TwoPhaseLoad] - done materializing entity [us.ms.state.deq.surveyor.model.Survey#1]
Jun-12 16:01:03 DEBUG [ulPersistenceContext] - initializing non-lazy collections
Jun-12 16:01:03 DEBUG [Loader] - done entity load
Jun-12 16:01:03 DEBUG [Loader] - loading collection: [us.ms.state.deq.surveyor.model.Survey.surveyQuestions#1]
Jun-12 16:01:03 DEBUG [AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
Jun-12 16:01:03 DEBUG [SQL] -
   select
      surveyques0_.survey_question_id as survey1_3_,
      surveyques0_.survey_question_id as survey1_2_2_,
      surveyques0_.alt_text as alt2_2_2_,
      surveyques0_.position as position2_2_,
      surveyques0_.question_id as question6_2_2_,
      surveyques0_.survey_id as survey5_2_2_,
      surveyques0_.version as version2_2_,
      question1_.question_id as question1_1_0_,
      question1_.min_results as min2_1_0_,
      question1_.question_type_id as question3_1_0_,
      question1_.text as text1_0_,
      question1_.version as version1_0_,
      survey2_.survey_id as survey1_0_1_,
      survey2_.name as name0_1_,
      survey2_.version as version0_1_
   from
      survey_question surveyques0_
   inner join
      question question1_
         on surveyques0_.question_id=question1_.question_id
   inner join
      survey survey2_
         on surveyques0_.survey_id=survey2_.survey_id
   where
      surveyques0_.survey_question_id=?
   order by
      surveyques0_.position asc
Hibernate:
   select
      surveyques0_.survey_question_id as survey1_3_,
      surveyques0_.survey_question_id as survey1_2_2_,
      surveyques0_.alt_text as alt2_2_2_,
      surveyques0_.position as position2_2_,
      surveyques0_.question_id as question6_2_2_,
      surveyques0_.survey_id as survey5_2_2_,
      surveyques0_.version as version2_2_,
      question1_.question_id as question1_1_0_,
      question1_.min_results as min2_1_0_,
      question1_.question_type_id as question3_1_0_,
      question1_.text as text1_0_,
      question1_.version as version1_0_,
      survey2_.survey_id as survey1_0_1_,
      survey2_.name as name0_1_,
      survey2_.version as version0_1_
   from
      survey_question surveyques0_
   inner join
      question question1_
         on surveyques0_.question_id=question1_.question_id
   inner join
      survey survey2_
         on surveyques0_.survey_id=survey2_.survey_id
   where
      surveyques0_.survey_question_id=?
   order by
      surveyques0_.position asc
Jun-12 16:01:03 DEBUG [AbstractBatcher] - about to open ResultSet (open ResultSets: 0, globally: 0)
Jun-12 16:01:03 DEBUG [Loader] - result set contains (possibly empty) collection: [us.ms.state.deq.surveyor.model.Survey.surveyQuestions#1]
Jun-12 16:01:03 DEBUG [Loader] - result row: EntityKey[us.ms.state.deq.surveyor.model.Question#1], EntityKey[us.ms.state.deq.surveyor.model.Survey#1], EntityKey[us.ms.state.deq.surveyor.model.SurveyQuestion#1]
Jun-12 16:01:03 DEBUG [Loader] - found row of collection: [us.ms.state.deq.surveyor.model.Survey.surveyQuestions#1]
Jun-12 16:01:03 DEBUG [AbstractBatcher] - about to close ResultSet (open ResultSets: 1, globally: 1)
Jun-12 16:01:03 DEBUG [AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
Jun-12 16:01:03 DEBUG [TwoPhaseLoad] - resolving associations for [us.ms.state.deq.surveyor.model.Question#1]
Jun-12 16:01:03 DEBUG [TwoPhaseLoad] - done materializing entity [us.ms.state.deq.surveyor.model.Question#1]
Jun-12 16:01:03 DEBUG [TwoPhaseLoad] - resolving associations for [us.ms.state.deq.surveyor.model.SurveyQuestion#1]
Jun-12 16:01:03 DEBUG [TwoPhaseLoad] - done materializing entity [us.ms.state.deq.surveyor.model.SurveyQuestion#1]
Jun-12 16:01:03 DEBUG [ollectionLoadContext] - 1 collections were found in result set for role: us.ms.state.deq.surveyor.model.Survey.surveyQuestions
Jun-12 16:01:03 DEBUG [ollectionLoadContext] - collection fully initialized: [us.ms.state.deq.surveyor.model.Survey.surveyQuestions#1]
Jun-12 16:01:03 DEBUG [ollectionLoadContext] - 1 collections initialized for role: us.ms.state.deq.surveyor.model.Survey.surveyQuestions
Jun-12 16:01:03 DEBUG [ulPersistenceContext] - initializing non-lazy collections
Jun-12 16:01:03 DEBUG [Loader] - done loading collection
Jun-12 16:01:03 DEBUG [JDBCTransaction] - commit
Jun-12 16:01:03 DEBUG [lushingEventListener] - processing flush-time cascades
Jun-12 16:01:03 DEBUG [lushingEventListener] - dirty checking collections
Jun-12 16:01:03 DEBUG [Collections] - Collection found: [us.ms.state.deq.surveyor.model.Survey.surveyQuestions#1], was: [us.ms.state.deq.surveyor.model.Survey.surveyQuestions#1] (initialized)
Jun-12 16:01:03 DEBUG [Collections] - Collection found: [us.ms.state.deq.surveyor.model.Question.choices#1], was: [us.ms.state.deq.surveyor.model.Question.choices#1] (uninitialized)
Jun-12 16:01:03 DEBUG [Collections] - Collection found: [us.ms.state.deq.surveyor.model.Question.surveyQuestions#1], was: [us.ms.state.deq.surveyor.model.Question.surveyQuestions#1] (uninitialized)
Jun-12 16:01:03 DEBUG [Collections] - Collection found: [us.ms.state.deq.surveyor.model.SurveyQuestion.results#1], was: [us.ms.state.deq.surveyor.model.SurveyQuestion.results#1] (uninitialized)
Jun-12 16:01:03 DEBUG [lushingEventListener] - Flushed: 0 insertions, 0 updates, 0 deletions to 3 objects
Jun-12 16:01:03 DEBUG [lushingEventListener] - Flushed: 0 (re)creations, 0 updates, 0 removals to 4 collections
Jun-12 16:01:03 DEBUG [Printer] - listing entities:
Jun-12 16:01:03 DEBUG [Printer] - us.ms.state.deq.surveyor.model.Question{minResults=null, text=What industry are you associated with?, surveyQuestions=<uninitialized>, questionType=0, choices=<uninitialized>, id=1, version=0}
Jun-12 16:01:03 DEBUG [Printer] - us.ms.state.deq.surveyor.model.Survey{surveyQuestions=[us.ms.state.deq.surveyor.model.SurveyQuestion#1], name=MMA, id=1, version=0}
Jun-12 16:01:03 DEBUG [Printer] - us.ms.state.deq.surveyor.model.SurveyQuestion{altText=null, survey=us.ms.state.deq.surveyor.model.Survey#1, position=0, results=<uninitialized>, question=us.ms.state.deq.surveyor.model.Question#1, id=1, version=0}
Jun-12 16:01:03 DEBUG [JDBCTransaction] - re-enabling autocommit
Jun-12 16:01:03 DEBUG [JDBCTransaction] - committed JDBC Connection
Jun-12 16:01:03 DEBUG [ConnectionManager] - aggressively releasing JDBC connection
Jun-12 16:01:03 DEBUG [ConnectionManager] - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
Jun-12 16:01:03 DEBUG [Loader] - loading collection: [us.ms.state.deq.surveyor.model.Question.choices#1]
Jun-12 16:01:03 DEBUG [AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
Jun-12 16:01:03 DEBUG [ConnectionManager] - opening JDBC connection
Jun-12 16:01:03 DEBUG [SQL] -
   select
      choices0_.choice_id as choice1_2_,
      choices0_.choice_id as choice1_3_1_,
      choices0_.is_other as is2_3_1_,
      choices0_.position as position3_1_,
      choices0_.question_id as question6_3_1_,
      choices0_.text as text3_1_,
      choices0_.version as version3_1_,
      question1_.question_id as question1_1_0_,
      question1_.min_results as min2_1_0_,
      question1_.question_type_id as question3_1_0_,
      question1_.text as text1_0_,
      question1_.version as version1_0_
   from
      choice choices0_
   inner join
      question question1_
         on choices0_.question_id=question1_.question_id
   where
      choices0_.choice_id=?
   order by
      choices0_.position asc
Hibernate:
   select
      choices0_.choice_id as choice1_2_,
      choices0_.choice_id as choice1_3_1_,
      choices0_.is_other as is2_3_1_,
      choices0_.position as position3_1_,
      choices0_.question_id as question6_3_1_,
      choices0_.text as text3_1_,
      choices0_.version as version3_1_,
      question1_.question_id as question1_1_0_,
      question1_.min_results as min2_1_0_,
      question1_.question_type_id as question3_1_0_,
      question1_.text as text1_0_,
      question1_.version as version1_0_
   from
      choice choices0_
   inner join
      question question1_
         on choices0_.question_id=question1_.question_id
   where
      choices0_.choice_id=?
   order by
      choices0_.position asc
Jun-12 16:01:03 DEBUG [AbstractBatcher] - about to open ResultSet (open ResultSets: 0, globally: 0)
Jun-12 16:01:03 DEBUG [Loader] - result set contains (possibly empty) collection: [us.ms.state.deq.surveyor.model.Question.choices#1]
Jun-12 16:01:03 DEBUG [Loader] - result row: EntityKey[us.ms.state.deq.surveyor.model.Question#1], EntityKey[us.ms.state.deq.surveyor.model.Choice#1]
Jun-12 16:01:03 DEBUG [Loader] - found row of collection: [us.ms.state.deq.surveyor.model.Question.choices#1]
Jun-12 16:01:03 DEBUG [AbstractBatcher] - about to close ResultSet (open ResultSets: 1, globally: 1)
Jun-12 16:01:03 DEBUG [AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
Jun-12 16:01:03 DEBUG [ConnectionManager] - aggressively releasing JDBC connection
Jun-12 16:01:03 DEBUG [ConnectionManager] - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
Jun-12 16:01:03 DEBUG [TwoPhaseLoad] - resolving associations for [us.ms.state.deq.surveyor.model.Choice#1]
Jun-12 16:01:03 DEBUG [TwoPhaseLoad] - done materializing entity [us.ms.state.deq.surveyor.model.Choice#1]
Jun-12 16:01:03 DEBUG [ollectionLoadContext] - 1 collections were found in result set for role: us.ms.state.deq.surveyor.model.Question.choices
Jun-12 16:01:03 DEBUG [ollectionLoadContext] - collection fully initialized: [us.ms.state.deq.surveyor.model.Question.choices#1]
Jun-12 16:01:03 DEBUG [ollectionLoadContext] - 1 collections initialized for role: us.ms.state.deq.surveyor.model.Question.choices
Jun-12 16:01:03 DEBUG [ulPersistenceContext] - initializing non-lazy collections
Jun-12 16:01:03 DEBUG [Loader] - done loading collection


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 14, 2007 10:01 am 
Newbie

Joined: Tue Jun 12, 2007 9:28 am
Posts: 8
Location: Ohio, Mississippi, Michigan
I figured out what I was doing wrong. In my @OneToMany annotations, I was specifying the mappedBy as the id field of the mapped class. These should have been the field that was doing the mapping. (Duh!)

Question.java
Code:
@OneToMany(mappedBy = "question")
private List<Choice> choices = new LinkedList<Choice>();


Choice.java
Code:
@ManyToOne
@JoinColumn(name = "question_id", nullable = false,
      columnDefinition = "integer")
@ForeignKey(name = "question_choice_fk")
private Question question;


Chalk that one up to being a noob.


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.