0

Here is the method I wrote

        public static <T> List selectOnCriteria(SessionFactory sessionFactory, int maxResults, T model)
            {

            List resultList=null;


            session=sessionFactory.openSession();

            Criteria criteria= session.createCriteria(model.getClass());

            criteria.add(Example.create(model).ignoreCase());// only for exact search, uppercase and lowercase are both included in ignorecase
            if(maxResults>0)
            criteria.setMaxResults(maxResults);

            resultList=criteria.list();

            return resultList;
            }

Here is one of my models

        public class UserModel
        {

        @Id
        private String username;
        private String password;
        private String company;
                  //getters and setters are also defined
                   }

Now Suppose That there is an entry in the table like this

   Username: Chris
   Password: Nolan
   Company : Syncopy

If I populate my model with all these values then the resultList I obtain has 1 record. However if only populate my model with username, ie Chris in this case, the resultlist I receive is always an empty ArrayList.

My Projects JPA Specification is 2.0, but if that is the issue, why does it return me the object in the first case and not the second.

Secondly , if that's the issue : is there any other way I can design a generic class that can take any model (of any type, with any number of fields populated)?

The last thing I want to do is to create a search method for all of my models.

Thanks and have a great day!

Dipanshu Verma
  • 459
  • 3
  • 8
  • 22
  • If you are using JPA, you should avoid using Hibernate specific Session, and SessionFactory. Instead you should use EntityManager, which is defined in JPA standard. – thegeko Mar 15 '15 at 11:56
  • You're not using JPA API, and you're not using JPA Criteria API either, hence not a JPA question - this is Hibernate specific – Neil Stockton Mar 15 '15 at 14:13
  • There is no way that criteria.list() returns null. Post your code, and tell us what it REALLY returns. Post the code displaying that result. – JB Nizet Mar 15 '15 at 14:15
  • I am sorry if I said it returned null, what I really meant was that it returned an empty arraylist. This is not what I want , I want that it shoud return the object model. Editing the question. – Dipanshu Verma Mar 15 '15 at 15:06

0 Answers0