EclipseLink JPA ManyToMany正在获取

时间:2018-07-31 17:42:43

标签: jpa many-to-many eclipselink

我有很多关系:

public class Activity {

    @Id
    @Column(name="ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ACTIVITIES")
    private Long id;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
        name = "ACTIVITY_TOPICS",
        joinColumns = @JoinColumn(name = "ACTIVITY_ID"),
        inverseJoinColumns = @JoinColumn(name = "TOPIC_ID"))
    private List<Topic> topics;

}

还有一个查询(我使用Spring DATA和QueryDSL):

    QActivity activity = QActivity.activity;
    JPAQuery<Activity> query = (JPAQuery<Activity>)helper.getQueryFactory().from(activity);

    query.setHint("eclipselink.batch", "coreActivity.priorities");
    query.setHint("eclipselink.batch", "coreActivity.topics");
    query.setHint("eclipselink.batch", "coreActivity.socialMediaActivities");
    query.setHint("eclipselink.batch.type", "JOIN");

    query.offset(pageRequest.getOffset());
    query.limit(pageRequest.getPageSize());

    return (List<Activity>)query.fetch();

问题在于主题是分别获取的。

我也尝试过:

query.leftJoin(activity.topics, QTopic.topic)

但是,没有主题的活动不会被获取。生成的SQL是:

SELECT * FROM ACTIVITIES a
LEFT OUTER JOIN T_ACTIVITY_TOPICS b ON (b.ACTIVITY_ID = a.ID)
JOIN TOPICS c ON (c.ID = b.TOPIC_ID)

0 个答案:

没有答案