Announcement Announcement Module
Collapse
No announcement yet.
How to use hibernate criteria API smartly Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use hibernate criteria API smartly

    Hi all, I am new to Spring + Hibernate and thinking about the architecture problem when using the Hibernate Criteria.

    In my existing small project, I am using 3-layers design (web tier, services and persistance layer). Here, I have a Person model class like this

    Code:
    public class Person {
        String name;
        Date birthDate;
    
        ...
    }
    In the web-layer I would like to let the user to search a list of Person that born before/after a user defined date and with name equals, for example, John. I would like to use the Criteria API in Hibernate, but where should I construct the Criteria ? It seems reasonable to perform it in DAO, however, how can I pass these searching criteria to DAO. If it is built in the services class, it violate the rules that services layer should not aware of persistance layer's implementation.

    Could someone can help ? Thanks in advance :roll:

  • #2
    Re: How to use hibernate criteria API smartly

    Originally posted by gaplo
    but where should I construct the Criteria ? It seems reasonable to perform it in DAO, however, how can I pass these searching criteria to DAO.
    The criteria should remain in the dao. You have to add a method to your person dao:
    findByNameAndDateRange(String name, Date fromDate, Date toDate);

    This method will use a criteria... but from the outside nobody needs to know how it is done...

    Comment


    • #3
      Thanks for your reply.

      Is that mean, I have to write a method for each combination ? for example I added a field nationality to the Person class that I need the followings ?

      Code:
      findByNationalityAndDateRange();
      findByNameAndNationality();
      If more fields are added, the DAO will full with this kind of findByXXX method ?

      Comment


      • #4
        Originally posted by gaplo
        Thanks for your reply.

        Is that mean, I have to write a method for each combination ? for example I added a field nationality to the Person class that I need the followings ?

        Code:
        findByNationalityAndDateRange();
        findByNameAndNationality();
        If more fields are added, the DAO will full with this kind of findByXXX method ?
        Yes... but if you want to combine a lot of methods, maybe you could better write a single find method with a lot of arguments. If arguments are null, then they are not used.

        eg:
        findBy(String name, Date birthDate, Nationality nationality, Color hairColor, etc etc etc).

        I haven`t had much need so far to create loads an loads of find methods... maybe 2/10 finders per dao.

        Comment


        • #5
          You can also search by example: http://www.hibernate.org/hib_docs/re...teria-examples

          Comment


          • #6
            Thanks all of you :P

            Comment

            Working...
            X