как построить динамический запрос для отношения «многие к одному» с использованием CriteriaBuilder из JPA 2.0

Я застрял в создании динамического запроса с помощью CriteriaBuilder в JPA 2.0. Мое приложение - Spring 3.0, Hibernate 3.6.0 + JPA 2.0. На самом деле у меня есть две сущности: одна - taUser , а другая - taContact , в моем классе taUser есть одно свойство, который имеет отношение "многие к одному" с taContact мои классы pojo (примерный пример)

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;
    }

   }

и мой 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>

Как я могу создать построение динамического запроса с использованием критериев на самом деле это мой запрос jpql Я хотите изменить его на построение динамического запроса с использованием критериев.

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

Как я могу проверить второе условие where?

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)

как я могу решить эту проблему?

5
задан Hash 21 March 2017 в 13:29
поделиться