2013-07-31 2 views
1

У меня есть запрос, в котором я хочу получить только объекты Message, у которых есть один или несколько дочерних объектов Link.MySQLSyntaxErrorException, сгенерированный Hibernate при использовании подсчета дочерних элементов в запросе

Мой класс сообщения:

package zzz.community.domain; 

/** 
* Entity implementation class for Entity: Message. 
*/ 
@Entity 
@Table(name = "MESSAGE") 
public class Message implements Serializable { 

private static final long serialVersionUID = 2252587276028343749L; 

public static final int MAX_LENGTH_TITLE = 255; 

public static final int MAX_LENGTH_TEXT = 200000; 

@Id @GeneratedValue 
private Long id; 

private String title; 

@Column(columnDefinition = "TEXT") 
private String text; 

/** 
* The date at which the message was posted. 
*/ 
private Date creationDate; 

/** 
* The profile id of the user that posted this message. 
*/ 
@OneToOne 
private Profile creator; 

/** 
* The id of the circle the message was posted to. 
*/ 
@OneToOne 
private Circle circle; 

/** 
* When true, only members of the circle may see this message. 
*/ 
private Boolean membersOnly; 

@OneToMany(mappedBy="message") 
@OrderBy("id DESC") 
private Set<Link> links; 

public Long getId() { 
    return id; 
} 

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

public String getText() { 
    return text; 
} 

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

public String getTitle() { 
    return title; 
} 

public void setTitle(final String title) { 
    this.title = title; 
} 

public Date getCreationDate() { 
    return creationDate; 
} 

public void setCreationDate(final Date creationDate) { 
    this.creationDate = creationDate; 
} 

public Profile getCreator() { 
    return creator; 
} 

public void setCreator(final Profile creator) { 
    this.creator = creator; 
} 

public Circle getCircle() { 
    return circle; 
} 

public void setCircle(final Circle circle) { 
    this.circle = circle; 
} 

public Boolean getMembersOnly() { 
    return membersOnly; 
} 

public void setMembersOnly(final Boolean membersOnly) { 
    this.membersOnly = membersOnly; 
} 

public Set<Link> getLinks() { 
    return this.links; 
} 

public void setLinks(final Set<Link> links) { 
    this.links = links; 
} 

public void addLink(final Link link) { 
    this.links.add(link); 
} 

} 

Запрос JPA я написал есть:

select m from Message m 
where m.circle = :circle 
and count(m.links) > 0 
order by m.creationDate DESC 

Hibernate разбирает этот код правильно (Нет AST Exception), но в результате MySQL заявление не является правильным и броски a MySQLSyntaxErrorException

 
09:01:22.153 [http-apr-8080-exec-3] DEBUG org.hibernate.hql.ast.ErrorCounter - throwQueryException() : no errors 
09:01:22.154 [http-apr-8080-exec-3] DEBUG o.h.hql.ast.QueryTranslatorImpl - HQL: select m from zzz.domain.Message m where m.circle = :circle and count(m.links) > 0 order by m.creationDate DESC 
09:01:22.154 [http-apr-8080-exec-3] DEBUG o.h.hql.ast.QueryTranslatorImpl - SQL: select message0_.id as id3_, message0_.circle_id as circle6_3_, message0_.creationDate as creation2_3_, message0_.creator_id as creator7_3_, message0_.membersOnly as membersO3_3_, message0_.text as text3_, message0_.title as title3_ from MESSAGE message0_ cross join LINK links1_ where message0_.id=links1_.message_id and message0_.circle_id=? and count(.)>0 order by message0_.creationDate DESC 

Опять же, полученный MySQL-запрос:

select 
    message0_.id as id3_ 
, message0_.circle_id as circle6_3_ 
, message0_.creationDate as creation2_3_ 
, message0_.creator_id as creator7_3_ 
, message0_.membersOnly as membersO3_3_ 
, message0_.text as text3_ 
, message0_.title as title3_ 
from MESSAGE message0_ 
cross join LINK links1_ 
where message0_.id=links1_.message_id 
and message0_.circle_id=? 
and count(.)>0 
order by message0_.creationDate DESC 

Обходной, конечно, удалить критерий выбора

count(m.links) > 0 

и заменить его какой-то код в Java. Но я надеюсь получить лучшее предложение одного из вас.
Спасибо заранее,

Henk

+0

Дайте нам класс сообщений. – Elbek

ответ

1

В JPQL это можно сделать, например, с одним из следующих конструкций:

  • Пустое выражение сравнения коллекция [NOT] EMPTY
  • функция РАЗМЕР

С первым, следующим:

count(m.links) > 0 

заменяется:

m.links IS NOT EMPTY 

И второй подход является:

SIZE(m.links) > 0 
+0

Работы! Спасибо! –

Смежные вопросы