使用QUeryDSL加入时遇到问题。我试图收到一份" Clube"与#34; Federacao"。
有一个ManyToOne关系有人可以帮助我吗?
感谢。
关系是在下面的类之间。
@Entity
@Table(name = "federacoes")
@SequenceGenerator(name = "idgen", sequenceName = "federacoes_id_federacao_seq", allocationSize = 1)
@AttributeOverride(name = "id", column = @Column(name = "id_federacao"))
public class Federacao extends AbstractEntity{
private String sigla;
private String uf;
private String nome;
...
}
@Entity
@Table(name = "clubes")
@SequenceGenerator(name = "idgen", sequenceName = "clubes_id_clube_seq", allocationSize = 1)
@AttributeOverride(name = "id", column = @Column(name = "id_clube"))
public class Clube extends AbstractEntity{
private Federacao federacao;
...
@ManyToOne
@JoinColumn(name = "id_federacao")
@Column(name = "federacao")
public Federacao getFederacao() {
return federacao;
}
...
}
我使用QueryDSL并在扩展QueryDslRepositorySupport的存储库类中执行此操作:
...
@PersistenceContext
private EntityManager em;
private static final QClube qClube = QClube.clube;
private static final QFederacao qFederacao = QFederacao.federacao;
@Override
public List<Clube> findAll(FilterClubeDTO filterClubeDTO) {
JPAQuery query = new JPAQuery(em);
return query.from(qClube)
.innerJoin(qFederacao).on(qClube.federacao.eq(qFederacao))
.orderBy(qClube.id.desc())
.list(qClube);
}
我收到此例外:
2016-04-12 12:32:38.485 ERROR 2853 --- [ qtp36627152-15] o.h.hql.internal.ast.ErrorCounter : Path expected for join!
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [select clube
from br.com.cbfm.core.models.Clube clube
inner join Federacao federacao with clube.federacao = federacao
order by clube.id desc]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [select clube
from br.com.cbfm.core.models.Clube clube
inner join Federacao federacao with clube.federacao = federacao
order by clube.id desc]
答案 0 :(得分:0)
这应该按预期工作:
JPAQuery query = new JPAQuery(em);
return query.from(qClube)
.innerJoin(qClube.federacao, qFederacao)
.orderBy(qClube.id.desc())
.list(qClube);
答案 1 :(得分:0)
Meiko提供的查询是对的。我已下载了您的项目,并发现了与模型关系相关的一些问题。
请勿使用以下代码
@ManyToOne
@JoinColumn(name = "id_federacao")
@Column(name = "federacao")
public Federacao getFederacao() {
return federacao;
}
但是使用,
@ManyToOne
@JoinColumn(name = "federacao")
private Federacao federacao;
这应该解决你的问题。我也修改了一些小东西,但这不会影响代码的工作。
答案 2 :(得分:0)
问题在于我的映射。我刚刚解决了这个问题:
Class Clube
@ManyToOne
@JoinColumn(name="id_federacao", nullable=false)
private Federacao federacao;
和类Federacao
@OneToMany(mappedBy="federacao")
private Set<Clube> clubes;
我现在有另一个问题,我现在就发布了。
谢谢大家。