Search code examples
javahibernatespringcriteriajpa-2.0

how to construct a dynamic query for a many to one relationship using the CriteriaBuilder of JPA 2.0


I stucked constructing a dynamic query using the CriteriaBuilder in JPA 2.0. My application is Spring 3.0, Hibernate 3.6.0 + JPA 2.0 based. Actually I have two entities one is taUser and another one is taContact, in my taUser class has one property ,that has many to one relationship with taContact my pojo classes are (sample example)

public class TaUser implements java.io.Serializable {
    private int userId;
    private TaContact taContact;
    public int getUserId() {
        return this.userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }
    public TaContact getTaContact() {
        return taContact;
    }

    public void setTaContact(TaContact taContact) {
        this.taContact = taContact;
    }

    }


   public class TaContact implements java.io.Serializable {

    private int contactId;

    public int getContactId() {
        return this.contactId;
    }

    public void setContactId(int contactId) {
        this.contactId = contactId;
    }
   private int contactNumber;

    public int getContactNumber() {
        return contactNumber;
    }

    public void setContactNumber(int contactNumber) {
        this.contactNumber = contactNumber;
    }

   }

and my orm .xml

<entity class="com.common.model.TaUser" name="TaUser">
        <table name="ta_user" />
        <attributes>
            <id name="userId">
                <column name="USER_ID" />
                <generated-value strategy="AUTO" />
            </id>
            <many-to-one name="taContact"
                target-entity="TaContact">
                <join-column name="Contact_id" />
            </many-to-one>
</attributes>
</entity>

How can I create constructing a dynamic query using criteria actually this is my jpql query I want to change it into constructing a dynamic query using criterias.

String jpql = 
    "select * from Tauser user where user.userId = "1" and user.taContact.contactNumber="8971329902";

How can I check the second where condition?

user.taContact.contactNumber="8971329902"

Root<T> rootEntity;
        TypedQuery<T> typedQuery = null;
        EntityManagerFactory entityManagerFactory = this.getJpaTemplate()
                .getEntityManagerFactory();
        CriteriaBuilder criteriaBuilder = entityManagerFactory
                .getCriteriaBuilder();
        CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(TaUser.class);
                rootEntity = criteriaQuery.from(TaUser.class);
                criteriaQuery.where(criteriaBuilder.equal(rootEntity.get("userId"),
                "1"));
        criteriaQuery.where(criteriaBuilder.equal(rootEntity.get("taContact.contactNumber"),
        "8971329902")); --- here  i m getting error 
    at 

org.hibernate.ejb.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:110)
    at org.hibernate.ejb.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:218)
    at org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:189)
    at com.evolvus.core.common.dao.CommonDao.findByCriteria(CommonDao.java:155)

how can I solve this?


Solution

  • There are many wey to use that Dynamic query in a JPA EntityManagerFactory.

    1. If you use JPA Entity class and Use Hibernate 3 JPA annotations then you can define the query using the @NamedQuery Annotation.

    2. You can use javax.persistence.Query to create a Dynamic query.

      Query q1=em.createQuery("SELECT TaUser AS tu WHERE tu.userId = :USERID");
      //em is entityManager object
      q1.setInteger("USERID",34);
      //here 34 is the dynamic value or if there is any relationship with
      // another entity then set the object reference of the other entity.
      q1.getResultList(); //return list of data.
      
    3. You can use the Hibernate Criteria API.

    But the thing is that if u want to create criteria you need to initialize the session object. So, to get session object use your entity manager object.