【发布时间】:2014-07-15 12:39:58
【问题描述】:
我将模型中的问题减少到 3 个实体:
- 车站。
- 位置(每个站点多个)。
- 位置组(每个站点多个)。
Location 类:
@Entity
@Table(name = "***")
public class Location {
@ManyToOne
@JoinColumn(name = "s_id")
private Station s;
@ManyToOne
@JoinColumn(name = "g_id")
private GroupOfLocations group;
}
还有GroupOfLocation 的类:
@Entity
@Table(name = "***")
public class GroupOfLocation {
@ManyToOne(fetch = FetchType.LAZY) //I do not want the Station to be loaded
@JoinColumn(name = "s_id")
private Station s;
}
当我通过 ID 获得位置时:
- 其站点已加载
- 位置组已加载
- 群组的站已加载但我不需要它。
问题:该组包含该站,但我不希望它被加载。我预计 fetch = FetchType.LAZY 会阻止 Station 完全加载,但它不起作用。
我在 SO 上进行了搜索,有时,问题来自声明为 final 的类,但此模型中没有最终类。
有什么想法吗?
这是通过 ID 搜索实体 id 的方式:
public Location getById(Integer id) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Location> query = cb.createQuery(Location.class);
Root<Location> entity = query.from(Location.class);
Predicate whereClause = cb.equal(entity.get(Location_.id), id);
query.where(whereClause);
return em.createQuery(query).getSingleResult();
}
【问题讨论】:
标签: java hibernate jpa orm hibernate-mapping