-->
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.  [ 1 post ] 
Author Message
 Post subject: Problem: Getting multiple identical rows back from a query
PostPosted: Wed Jun 06, 2007 11:36 am 
Newbie

Joined: Wed Jun 06, 2007 11:03 am
Posts: 1
I need to get a list of all the CampaignQuestions related to a certain Campaign ID from our db. The following sql does the trick – it’s really simple:

Code:
SELECT cq.QUESTION_TEXT, cq.CAMPAIGN_QUESTION_NO, cq.QUESTION_TYPE FROM CAMPAIGN_QUESTION cq
WHERE cq.CAMPAIGN_ID = ? ORDER BY cq.CAMPAIGN_QUESTION_NO


When I get the data back via Hibernate, rather than getting each (distinct) question, I get the (one) same question back X times, where X is the actual number of questions I expect to get back. However, if I copy the SQL Hibernate generates and query the db directly using that it works as expected which presumably means I've done my mapping incorrectly. I've tried various collections and methods of querying, but they all behaive in the same way.

My Java classes are simple beans with properties to match those in each respective table along with appropriate getters and setters in each. There's a class for Campaign and one for CampaignQuestion - The issue seems to be with CampaignQuestion:

Code:
package my.package;

public class CampaignQuestion {
   
    private Integer campaignId;
    private Integer campaignQuestionNo;
    private String questionText;
    private Integer questionType;
   
    public Integer getCampaignId() {
        return campaignId;
    }
    public void setCampaignId(Integer campaignId) {
        this.campaignId = campaignId;
    }
    public Integer getCampaignQuestionNo() {
        return campaignQuestionNo;
    }
    public void setCampaignQuestionNo(Integer campaignQuestionNo) {
        this.campaignQuestionNo = campaignQuestionNo;
    }
    public String getQuestionText() {
        return questionText;
    }
    public void setQuestionText(String questionText) {
        this.questionText = questionText;
    }
    public Integer getQuestionType() {
        return questionType;
    }
    public void setQuestionType(Integer questionType) {
        this.questionType = questionType;
    }
}


My mapping file is as follows:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="my.package">
<class name="CampaignQuestion" table="CAMPAIGN_QUESTION">
<id name="campaignId" column="CAMPAIGN_ID" type="integer" unsaved-value="null">
<generator class="native"/>
</id>
<property name="campaignQuestionNo" column="CAMPAIGN_QUESTION_NO" type="integer"/>
<property name="questionText" column="QUESTION_TEXT" type="string"/>
<property name="questionType" column="QUESTION_TYPE" type="integer"/>
</class>
</hibernate-mapping>[/code]

Finally, I'm running the query from a JSP scriptlet (for testing only I hasten to add):

String cid = request.getParameter("id");
if(cid.equals(null)) cid = "0";

Integer id = new Integer(cid);

org.hibernate.Session ses = HibernateFactory.openSession();

Query query = ses.createQuery("from CampaignQuestion cq where cq.campaignId = 121 order by cq.campaignQuestionNo");
// id is hardcoded for now

List list = query.list();

out.println(list.size() + " items in list<br><br>");

for(int i = 0; i < list.size(); i++) {

CampaignQuestion cq = (CampaignQuestion) list.get(i);

out.println("Campaign ID: " + cq.getCampaignId());
out.println("Question Number: " + cq.getCampaignQuestionNo());
out.println("Question Text: " + cq.getQuestionText());
out.println("Question Type: " + cq.getQuestionType());
out.println("<hr>");
}

HibernateFactory.closeSession();


Presumably, I have made a fundamental error somewhere. Help very much appreciated!

Thanks


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

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.