v-ladynev / fluent-hibernate

Library to work with Hibernate by fluent API
Other
48 stars 12 forks source link

In request there is first() but no last() #20

Open Levvy055 opened 8 years ago

Levvy055 commented 8 years ago

There is implementation of: H.request(MyClass.class).first(); but no: H.request(MyClass.class).last(); I need to make list() call than get last element, but the rest is not needed and takes unnecesary memory banks. Can you implement it in HibernateRequest ?

v-ladynev commented 8 years ago

Yes, I can. But this last() method will work the same way, as the first() method does. Load all list in the memory and get the last element. It can be convenient, of course, but not more optimal than get a whole list.

Levvy055 commented 8 years ago

So maybe better make method that will call SQL for first and last?

v-ladynev commented 7 years ago

To use SQL to getting the last record, we need to use the DESC order and get the first one. So the first() method is enough in this situation. The first() method was designing as a convenience method, for situation when we know that we need only one record, but Hibernate always returns a list (uniqueResult() throws an exception for multiple records). We can just extend the first() method to get only one record from a database using setMaxResults() and uniqueResult().

Levvy055 commented 7 years ago

I see that first() uses CollectionUtils which returns first element so let's for now do last Method there that like you said returns last element

v-ladynev commented 7 years ago

Did you mean, that we should use something like CollectionUtils.last() for this last() method?