如何使用JPA 2.0的CriteriaBuilder构建多对一关系的动态查询

我坚持使用JPA 2.0中的CriteriaBuilder构建动态查询。我的应用程序是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";
我怎样才能检查第二个条件?   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)
我该怎么解决这个问题?     
已邀请:
有许多人在JPA EntityManagerFactory中使用该动态查询。 如果您使用JPA Entity类和Use Hibernate 3 JPA注释,则可以使用
@NamedQuery
Annotation定义查询。 您可以使用
javax.persistence.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.
您可以使用Hibernate Criteria API。 但问题是,如果你想创建标准,你需要初始化会话对象。因此,要获取会话对象使用您的实体管理器对象。     
我想这是做到这一点的方法:
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是
int
领域的大型公路。将字段类型设置为
long
。     

要回复问题请先登录注册