I want to find only the username field in my Registration collection
I am using the following query
db.Registration.find( {"username":"abcd"}, {username:1, _id:0} )
How will I write this query in hibernate?
I want to find only the username field in my Registration collection
I am using the following query
db.Registration.find( {"username":"abcd"}, {username:1, _id:0} )
How will I write this query in hibernate?
If your entity looks like this:
@Entity
class Registration {
@Id
String id;
String username:
}
This should work:
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()
or
Registration results = entityManager.createQuery( query )
.setParameter("username", "abcd")
.getSingleResult()
You can also use native queries but I will let you to the documentation: https://docs.jboss.org/hibernate/ogm/5.4/reference/en-US/html_single/#ogm-mongodb-queries-native
You'll need to annotate the (none PK) columns you want to retrieve in the Entity with:
@Column(name = "username")