Criteria criteria = getSessionFactory().getCurrentSession()
.createCriteria(CustomerManagementDTO.class);
criteria.add(Restrictions.gt("id", id)).setProjection(
Projections.property("email"));
List<CustomerManagementDTO> list = criteria.list();
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Dave Tolls wrote:That's selecting where the db id > the given id.
Dave Tolls wrote:And I'm not entirely sure what the Projection is for?
Why have you put that in?
Roel De Nijs wrote:Hibernate Projections are used in order to query only a subset of the attributes of an entity or group of entities you're querying with Criteria. So it's probably added because the OP only wants to select the email. You can also use Projections to specify distinct clauses and aggregate functions like max, sum and so on. But that's not the case in this example
Dave Tolls wrote:So the rest of the data in those returned DTOs is null then? If not then I don't see what's gained by telling Hibernate "I'm only interested in the email".
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
Roel De Nijs wrote:Hibernate Projections are used in order to query only a subset of the attributes of an entity or group of entities you're querying with Criteria. So it's probably added because the OP only wants to select the email. You can also use Projections to specify distinct clauses and aggregate functions like max, sum and so on. But that's not the case in this example
Tim Holloway wrote:Hibernate JPA can retrieve a single column or selected set of columns, but they are returned as scalar column mappings. Check out the @SqlResultSetMapping annotation.
Note that this is a feature of JPA. If you're using old-style (non-JPA) Hibernate then you'd have to look for a comparable mechanism.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Partheban Udayakumar wrote:
PS: For those who are confused why I am retrieving email, I am using it to check for duplicate users in my db.
Partheban Udayakumar wrote:So this means what I have used is correct? But I am getting the empty list for the existing email. I tried unique result too, it too returns null.
Partheban Udayakumar wrote:PS: For those who are confused why I am retrieving email, I am using it to check for duplicate users in my db.
Roel De Nijs wrote:2/ I wonder how the query from your initial post (Select email from CustomerManagementDTO WHERE id=id;) will help you to determine duplicate emails (users) in the database as it just returns the email for a given user.
Paul Clapham wrote:
Roel De Nijs wrote:2/ I wonder how the query from your initial post (Select email from CustomerManagementDTO WHERE id=id;) will help you to determine duplicate emails (users) in the database as it just returns the email for a given user.
I keep getting the feeling I'm missing something. Because to me it looks like that query returns the e-mail addresses of all users. Am I wrong? If so why?
Dave Tolls wrote:If the email column is UNIQUE then you won't have duplicate users.
Roel De Nijs wrote:If the email column should only contain unique values, you should add a UNIQUE constraint to this column.
Roel De Nijs wrote:I wonder how the query from your initial post (Select email from CustomerManagementDTO WHERE id=id;) will help you to determine duplicate emails (users) in the database as it just returns the email for a given user
Roel De Nijs wrote:You should go back to basics and first create a Criteria object which returns a list of all users in your database. When that's working, you add the necessary changes to return only the user with a given id. And when that's working you can make any other changes. Using such an approach it's much easier to pinpoint the possible issue you are facing.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Partheban Udayakumar wrote:I feel like a dumbo now. I am actually using email retrieval for checking whether email exists in the server. I am using UNIQUE in database.
Partheban Udayakumar wrote:Initially, before knowing about UNIQUE, what I used to do is get the email and check the entered and retrieved emails in my java file.
Roel De Nijs wrote: If you want you can use a query to check if the entered email is unique or not. But you should not be comparing the email with all retrieved emails in Java. Your database can count all occurences of this email address in the user table. If the count is greater than 0, the email address is not unique and you should show an error message. And also don't forget that upper and lower case could matter
This query will be executed very quickly, so you can give feedback to the user almost immediately (if needed).
But you still need the UNIQUE constraint to protect data integrity! Between running the (very fast) email verification query and actually inserting the new user, another user might already be inserted with this email address.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Roel De Nijs wrote:Depending on the return value you'll have different options. This link, this one, and this one provide excellent explanation and code snippets about what's possible using JPQL.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Partheban Udayakumar wrote:but I need to do it in hibernate using criteria etc.
Roel De Nijs wrote:There's a specific article available on the Criteria API as well. You could have found this link yourself as it was very easy to find from the first link I provided...
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |