我将继承用于JPA实体(RegisteredAction等)。
public class Feed {
@ManyToOne
@JoinColumn(name = "ACTION_ID", nullable = false)
private Action action;
@ManyToOne
@JoinColumn(name = "USER_ID", nullable = false)
private User user;
...
}
public abstract class Action {
@Id
private Integer id;
@Enumerated(EnumType.STRING)
private FanoutType fanout;
@Enumerated(EnumType.STRING)
private ActionType type;
...
}
public class RegisteredAction extends Action {
@ManyToOne
@JoinColumn(name = "USER_ID", nullable = false)
private User user;
...
}
对数据库执行命名查询:
select a from Feed as f join f.action as a where f.user.id = :userId order by f.creationDate desc
一切都很好,Hibernate加载有关实体的信息。但是如果我想添加setFirstResult / setMaxResults方法,则没有任何变化:
getEntityManager().createNamedQuery(Feed.FEED_BY_USER, Action.class)
.setParameter(USER_ID, userId)
.setHint(QueryHints.READ_ONLY, true)
.setFirstResult(offset)
.setMaxResults(count)
.getResultList();
Hibernate会忽略这些设置并返回所有行。