R
R
Romanchitoz2019-01-12 15:08:01
css
Romanchitoz, 2019-01-12 15:08:01

How to search Hibernate session not by id?

For example, I have a user that has an ID and a Name.
To find a user by id - session.get(User.class, id);
But how to search for a user by name, because if cltkfnm nfr session.get(User.class, name) , it will throw an error.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kravchenko, 2016-04-17
@Faha1998

svg

V
Vasily Rybin, 2019-01-12
@VasilyRybin

Can be done via Criteria API or HQL
1) hql

String name = "userName";
Query query = session.createQuery("FROM User u where u.name = :name");
query,setParameter("name", name);

2) through the criteria api, the request will already be dynamically created, and for a simple request, there will be more code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question