Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 211
unknown Avatar asked Jan 28 '11 06:01

unknown


1 Answers

I guess this is the way to do it:

public TaUser getUserByIdAndContactNumber(
    final long userId,
    final long contactNumber){

    final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    final CriteriaQuery<TaUser> query = cb.createQuery(TaUser.class);
    final Root<TaUser> root = query.from(TaUser.class);
    query
        .where(cb.and(
            cb.equal(root.get("userId"), userId),
            cb.equal(root.get("taContact").get("contactNumber"), contactNumber)
        ));
    return entityManager.createQuery(query).getSingleResult();
}

BTW, 8971329902 is way to large for an int field. Set the field type to long.

like image 194
Sean Patrick Floyd Avatar answered Sep 28 '22 16:09

Sean Patrick Floyd