我有两个实体:
@Entity
@Table(name = "ACCOUNT")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class MyCloudAccount implements Serializable {
...
@OneToMany(mappedBy = "account", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<ServerInstance> servers = new HashSet<ServerInstance>();
...
}
@Entity
@Table(name = "SERVER_INSTANCE")
public class ServerInstance implements Serializable {
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ACCOUNT_ID")
private MyCloudAccount account;
...
}
我通过此代码获取所有帐户:
StringBuilder sql = new StringBuilder();
sql.append("SELECT e FROM ");
sql.append(persistentClass.getName());
sql.append(" e");
return entityManager.createQuery(sql.toString()).getResultList();
这会产生一个帐户查询和 N 个服务器查询,而不是一个带有外部连接的查询。如何强制 JPA 以最佳方式进行查询?