加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – 如何使用JPA 2.0的CriteriaBuilder为多对一关系构造动

发布时间:2020-12-15 01:46:45 所属栏目:大数据 来源:网络整理
导读:我坚持使用JPA 2.0中的CriteriaBuilder构建动态查询.我的应用程序是Spring 3.0,基于Hibernate 3.6.0 JPA 2.0.其实我有两个实体,一个是taUser,另一个是taContact,在我的taUser类中有一个属性,与taContact有多对一关系我的pojo类是(示例) public class TaUser

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


如何使用条件创建构建动态查询实际上这是我的jpql查询我想将其更改为使用标准构建动态查询.

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

我怎样才能检查第二个条件?

user.taContact.contactNumber=”8971329902″

Root

我该怎么解决这个问题?

最佳答案
有许多人在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.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读