【问题标题】:how to construct a dynamic query for a many to one relationship using the CriteriaBuilder of JPA 2.0如何使用 JPA 2.0 的 CriteriaBuilder 为多对一关系构建动态查询
【发布时间】:2011-01-28 06:18:59
【问题描述】:

我坚持使用 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";

如何检查第二个 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)

我该如何解决这个问题?

【问题讨论】:

    标签: java hibernate spring criteria jpa-2.0


    【解决方案1】:

    我想这是这样做的方法:

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

    顺便说一句,8971329902 对于int 字段来说太大了。将字段类型设置为long

    【讨论】:

      【解决方案2】:

      在 JPA EntityManagerFactory 中有很多可以使用动态查询的方法。

      1. 如果您使用 JPA Entity class 和 Use Hibernate 3 JPA 注释,那么您可以使用 @NamedQuery Annotation 定义查询。

      2. 您可以使用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.
        
      3. 您可以使用 Hibernate Criteria API。

      但问题是,如果你想创建标准,你需要初始化会话对象。因此,要获取会话对象,请使用您的实体管理器对象。

      【讨论】:

      • 请:如果英语不是您的母语,没关系。但是尽量不要通过写 LOL-speak like 'u' 而不是 'you' 来让一切变得更糟。此外,尽量不要拼错您所指的类、包和技术。您的帖子很难按原样阅读。
      • 我试图修复您的帖子,但即便如此,您的回答也无关紧要,因为问题是关于 JPA 2 Criteria API,而不是 Hibernate Criteria API。
      • 嗨,肖恩·帕特里克·弗洛伊德,您的第二条评论是正确的。为什么你不能给我任何建议?
      • @Sean Patrick Floyd :在我的回答中,我使用了 JPA 2 Criteria API,这就是为什么我使用这个 javax.persistence.Query 而不是 org.hibernate.Criteria,感谢您的提示..:)跨度>
      • 问题是关于“使用JPA 2.0的CriteriaBuilder”,为什么提出其他查询方式?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 1970-01-01
      • 2012-08-19
      • 2016-10-06
      • 1970-01-01
      • 2014-03-28
      • 1970-01-01
      相关资源
      最近更新 更多