我有一张 table :
User: id, name, age, role_key
其中 Id 是唯一标识符。 我想检索具有特定 role_key 的所有用户。
我可以做这样的事情:
entityManager.createQuery(
"SELECT u From user WHERE user.role_key LIKE :roleKey")
.setParameter("roleKey", roleKey)
.getResultList();
但我想知道是否有更好的方法来编写查询。
例如,如果我想仅使用 Id 进行检索,我可以使用 find
。但是,由于我使用的 role_key 不是唯一的,有没有办法让我不需要在这里编写完整的选择查询。
谢谢
最佳答案
除了 JonK 的评论之外,我还建议您使用 @NamedQuery 来提高性能,因为您的查询是参数化查询。 很抱歉这应该作为评论出现,但我的代表。不允许我。
关于java - 不使用JPA中的id进行检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44592454/