Error: for SELECT DISTINCT, ORDER BY expressions must appear in select list

688 views
Skip to first unread message

Freewind

unread,
Oct 7, 2010, 9:21:55 AM10/7/10
to Ebean ORM
Database: postgres 8.x
Ebean: 2.6.1

I have 3 class, "Question", "Tag", "QuestionTagR".

Question and Tag are ManyToMany, but they are not using "ManyToMany"
directly, instead, using many "OneToMany" through "QuestionTagR"

@Entity
public class Question {
@OneToMany
public List<QuestionTagR> tagsR;
}

@Entity
public class Tag {
@OneToMany
public List<QuestionTagR> questionsR;

@Column
public int quesitonCount;
}

@Entity
public class QuestionTagR {
@ManyToOne
public Question question;

@ManyToOne
public Tag tag;
}

Now I want to query all the tags of a question, so I write the code:

Question question = getQuestion();
Ebean.query(Tag.class).where().eq("questionsR.question",
question).orderBy("questionCount desc").findList();

But there is an error reported:

PersistenceException occured :
Query threw SQLException:
ERROR:
for SELECT DISTINCT, ORDER BY expressions must appear in select list
Query was:
select distinct t.id as c0, t.name as c1 from tags t join
questions_tags_r xq on xq.tag_id = t.id where xq.question_id = ? order
by t.question_count desc

I don't understand why there is a "distinct" in the query. If there is
no "distinct", or no "order by", then everything is OK.

I don't know how to fix this. I have spent 2 hours on debuging, but
not find the reason.

Freewind

unread,
Oct 7, 2010, 11:33:23 AM10/7/10
to Ebean ORM
I know what's the reason now. The QuestionTagR should have a "@Id"
too. When I add one, everything is OK now.
Reply all
Reply to author
Forward
0 new messages