鉴于以下两个实体:
public class Performer {
@Id
private Long id;
private String name;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "performers_characters",
joinColumns = @JoinColumn(name = "performer_id", nullable = false, updatable = false),
inverseJoinColumns = @JoinColumn(name = "character_id", nullable = false, updatable = false))
private Set<Character> characters = Sets.newHashSet();
}
和
public class Character {
@Id
@Column(nullable = false)
private Long id;
@Column(nullable = false)
private String name;
private Boolean alive;
private String placeOfBirth;
@ManyToMany(mappedBy = "characters")
private Set<Performer> performers = Sets.newHashSet();
}
我想使用Criteria Builder创建查询,它将生成等效的SQL:
SELECT performer0_.id AS id1_11_0_,
character2_.id AS id1_0_1_,
performer0_.name AS name7_11_0_,
character2_.name AS name12_0_1_,
characters1_.character_id AS character_id2_12_0__
FROM performer performer0_
INNER JOIN performers_characters characters1_ ON performer0_.id=characters1_.performer_id
INNER JOIN character character2_ ON characters1_.character_id=character2_.id
基本上,应该选择Performer中的所有内容,但是从Character中,只应选择id和name。
目前我遇到以下情况:
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Performer> baseCriteriaQuery = criteriaBuilder.createQuery(Performer.class);
Root<Performer> baseRoot = baseCriteriaQuery.from(Performer.class);
baseCriteriaQuery.select(baseRoot);
baseRoot.fetch("characters");
TypedQuery<Performer> baseTypedQuery = entityManager.createQuery(baseCriteriaQuery);
List<Performer> baseEntityList = baseTypedQuery.getResultList();
这项工作很好,除了来自Character和Performer之外,所有列都被选中。
答案 0 :(得分:0)
使用JPA,只有在您指定不想加载为懒惰的字段时才可以这样做。
@Basic(fetch=FetchType.LAZY)
private String placeOfBirth;
@Basic(fetch=FetchType.LAZY)
private Boolean alive;
但问题是你为什么要这样做?这两个字段都是小项目,而LAZY则有更多SQL调用来获取数据。