【发布时间】:2018-12-16 03:58:05
【问题描述】:
我只想在我的注册集合中查找用户名字段
我正在使用以下查询
db.Registration.find( {"username":"abcd"}, {username:1, _id:0} )
我将如何在 hibernate 中编写此查询?
【问题讨论】:
标签: mongodb hibernate-ogm
我只想在我的注册集合中查找用户名字段
我正在使用以下查询
db.Registration.find( {"username":"abcd"}, {username:1, _id:0} )
我将如何在 hibernate 中编写此查询?
【问题讨论】:
标签: mongodb hibernate-ogm
如果您的实体如下所示:
@Entity
class Registration {
@Id
String id;
String username:
}
这应该可行:
String query = "FROM Registration r WHERE r.username = :username ORDER by r.username ASC, r.id DESC"
List<Registration> results = entityManager.createQuery( query )
.setParameter("username", "abcd")
.getResultList()
或
Registration results = entityManager.createQuery( query )
.setParameter("username", "abcd")
.getSingleResult()
您也可以使用本机查询,但我会让您查看文档:https://docs.jboss.org/hibernate/ogm/5.4/reference/en-US/html_single/#ogm-mongodb-queries-native
【讨论】:
您需要注释要在实体中检索的(无 PK)列:
@Column(name = "username")
【讨论】: