をサブ属性I定義されたテーブルに、このようにマッピングエンティティがあります。のコレクション内の属性を検索するJPA criteriaBuilderを使用する方法
@Entity
@Table(name = "cmmn_calendar_evnt")
public class CommonCalendarEvent implements java.io.Serializable
{
private Integer cevId;
private Set<CommonCalendarEventPart> commonCalendarEventParts = new HashSet<CommonCalendarEventPart>(0)
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "CEV_ID", unique = true, nullable = false)
public Integer getCevId()
{
return this.cevId;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "commonCalendarEvent")
public Set<CommonCalendarEventPart> getCommonCalendarEventParts()
{
return this.commonCalendarEventParts;
}
}
とCommonCalendarEventPartは次のように定義されています。
@Entity
@Table(name = "cmmn_calendar_evnt_part")
public class CommonCalendarEventPart implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
private Integer ceeId;
private CommonCalendarEvent commonCalendarEvent;
private PartParticipant partParticipant;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "CEE_ID", unique = true, nullable = false)
public Integer getCeeId()
{
return this.ceeId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CEE_CEV_ID", nullable = false)
public CommonCalendarEvent getCommonCalendarEvent()
{
return this.commonCalendarEvent;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CEE_PPT_ID", nullable = false)
public PartParticipant getPartParticipant()
{
return this.partParticipant;
}
}
を
し、最終的に:
@Entity
@Table(name = "part_participant")
public class PartParticipant implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
private Integer pptId;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "PPT_ID", unique = true, nullable = false)
public Integer getPptId()
{
return this.pptId;
}
}
私はすべて見つけるクエリを生成するCriteriaBuilderを使用したいです特定の参加者IDのCommonCalendarEvent。私はcriteriaBuilderを書くことで、直感的な試みと思っていたもののいくつかのアプローチを試みた(私はこれHQLはどちらか正しいことを確認していないが)
"from commonCalendarEvent cce where :pptId in (cce.commonCalendarEventParts.partParticipant.pptId)"
:
HQLでは、次のようになります私の試みは、 "予期せぬサブツリーの終わり"から実装エラーだけに及ぶエラーにつながっています。
.....
CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<CommonCalendarEvent> criteria = builder.createQuery(CommonCalendarEvent.class);
Root<CommonCalendarEvent> root = criteria.from(CommonCalendarEvent.class);
Fetch<CommonCalendarEvent, CommonCalendarEventPart> evf = root.fetch(CommonCalendarEvent_.commonCalendarEventParts, JoinType.LEFT);
Join<CommonCalendarEvent, CommonCalendarEventPart> evj = (Join<CommonCalendarEvent, CommonCalendarEventPart>) evf;
Join<CommonCalendarEventPart, PartParticipant> evpj = evj.join(CommonCalendarEventPart_.partParticipant);
List<Predicate> pred = new ArrayList<Predicate>();
pred.add(builder.equal(evpj.get(PartParticipant_.pptId), pptId));
criteria.where(builder.and(pred.toArray(new Predicate[] {})));
return getEntityManager().createQuery(criteria).getResultList();
.............
上記の「予期しないサブツリーの終了」エラーが発生します。
ご了承ください。
投稿されたコードを確認してください: 'evf'とは何ですか? – perissf
申し訳ありません修正コード。 Fetch evf ...を追加するのを忘れました。 –
Mike
私の更新された回答を参照してください – perissf