1

没有条件查询的手动方法

我已经使用此代码通过与另一个表 ThirdPartyHasOwner 使用左连接来获取包含第三方的结果集,该表具有两个主键并且本身就是外键。现在下面的代码检索正确的结果数据集

Query query = emManager.createQuery("SELECT c FROM ThirdParty c LEFT JOIN ThirdPartyHasOwner b ON  b.id.third_party_Id = c.id WHERE b.id.ownerId=1");
List<ThirdParty> thirdParties = query.getResultList();

使用 Criteria Builder 和 Criteria Query

但是当与标准生成器和查询一起使用时,结果集给出了错误的数据集。下面给出了代码所以为了检查上面的手动查询和条件查询是否给出了相同的查询,我添加了属性<property name="eclipselink.logging.level" value="FINE"/>,上面没有条件查询的代码和下面的有条件查询的代码都给出了相同的查询代码和控制台结果下面给出

    CriteriaBuilder cb = emManager.getCriteriaBuilder();
    CriteriaQuery<ThirdParty> cq = cb.createQuery(ThirdParty.class);
    Root<ThirdParty> a = cq.from(ThirdParty.class);
    Join<ThirdParty, ThirdPartyHasOwner> b = a.join("thirdPartyHasOwners", JoinType.LEFT);

ParameterExpression<Integer> balance = cb.parameter(Integer.class);

Path<Integer> path = b.get("id").get("ownerId");

cq.where(cb.gt(path, balance));

cq.select(a);

TypedQuery<ThirdParty> queryS = emManager.createQuery(cq);

List<ThirdParty> results = queryS.setParameter(balance, 1).getResultList();

控制台结果(第一个用于标准查询第二个用于手动方法

[EL Fine]: sql: 2017-01-04 06:42:44.026--ServerSession(514728045)--Connection(1288428548)--Thread(Thread[http-bio-8080-exec-3,5,main])--SELECT t1.Id, t1.ADDRESS, t1.CONTACTNO, t1.CREATEDDATE, t1.EMAIL, t1.NAME FROM third_party t1 LEFT OUTER JOIN third_party_has_owner t0 ON (t0.THIRD_PARTY_ID = t1.Id) WHERE (t0.owner_id > ?)
    bind => [1]
[EL Fine]: sql: 2017-01-04 06:48:26.109--ServerSession(514728045)--Connection(1288428548)--Thread(Thread[http-bio-8080-exec-3,5,main])--SELECT t1.Id, t1.ADDRESS, t1.CONTACTNO, t1.CREATEDDATE, t1.EMAIL, t1.NAME FROM third_party t1 LEFT OUTER JOIN third_party_has_owner t0 ON (t0.THIRD_PARTY_ID = t1.Id) WHERE (t0.owner_id = ?)
    bind => [1]

手动和标准查询的结果(结果变量具有错误数据,这是使用标准查询的结果,第三方变量具有正确的数据集,这是手动方法的结果)

最后但并非最不重要的是,我使用javax.persistence.persistence-api的是 eclipselink

这是数据库表的概述

第三方模型类

/**
 * The persistent class for the third_party database table.
 * 
 */
    @Entity
    @Table(name="third_party")
    @NamedQuery(name="ThirdParty.findAll", query="SELECT t FROM ThirdParty t")
    public class ThirdParty implements Serializable {
        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        @Column(name="Id")
        private int id;

        private String address;

        private String contactNo;

        @Temporal(TemporalType.TIMESTAMP)
        private Date createdDate;

        private String email;

        private String name;

        //bi-directional many-to-one association to ThirdPartyHasOwner
        @OneToMany(mappedBy="thirdParty")
        private List<ThirdPartyHasOwner> thirdPartyHasOwners;

        //bi-directional many-to-one association to ThirdSeatAllocation
        @OneToMany(mappedBy="thirdParty")
        private List<ThirdSeatAllocation> thirdSeatAllocations;

        public ThirdParty() {
        }

        public int getId() {
            return this.id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getAddress() {
            return this.address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getContactNo() {
            return this.contactNo;
        }

        public void setContactNo(String contactNo) {
            this.contactNo = contactNo;
        }

        public Date getCreatedDate() {
            return this.createdDate;
        }

        public void setCreatedDate(Date createdDate) {
            this.createdDate = createdDate;
        }

        public String getEmail() {
            return this.email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getName() {
            return this.name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<ThirdPartyHasOwner> getThirdPartyHasOwners() {
            return this.thirdPartyHasOwners;
        }

        public void setThirdPartyHasOwners(List<ThirdPartyHasOwner> thirdPartyHasOwners) {
            this.thirdPartyHasOwners = thirdPartyHasOwners;
        }

        public ThirdPartyHasOwner addThirdPartyHasOwner(ThirdPartyHasOwner thirdPartyHasOwner) {
            getThirdPartyHasOwners().add(thirdPartyHasOwner);
            thirdPartyHasOwner.setThirdParty(this);

            return thirdPartyHasOwner;
        }

        public ThirdPartyHasOwner removeThirdPartyHasOwner(ThirdPartyHasOwner thirdPartyHasOwner) {
            getThirdPartyHasOwners().remove(thirdPartyHasOwner);
            thirdPartyHasOwner.setThirdParty(null);

            return thirdPartyHasOwner;
        }

        public List<ThirdSeatAllocation> getThirdSeatAllocations() {
            return this.thirdSeatAllocations;
        }

        public void setThirdSeatAllocations(List<ThirdSeatAllocation> thirdSeatAllocations) {
            this.thirdSeatAllocations = thirdSeatAllocations;
        }

        public ThirdSeatAllocation addThirdSeatAllocation(ThirdSeatAllocation thirdSeatAllocation) {
            getThirdSeatAllocations().add(thirdSeatAllocation);
            thirdSeatAllocation.setThirdParty(this);

            return thirdSeatAllocation;
        }

        public ThirdSeatAllocation removeThirdSeatAllocation(ThirdSeatAllocation thirdSeatAllocation) {
            getThirdSeatAllocations().remove(thirdSeatAllocation);
            thirdSeatAllocation.setThirdParty(null);

            return thirdSeatAllocation;
        }

    }

第三方模型类有所有者

/**
 * The persistent class for the third_party_has_owner database table.
 * 
 */
@Entity
@Table(name="third_party_has_owner")
@NamedQuery(name="ThirdPartyHasOwner.findAll", query="SELECT t FROM ThirdPartyHasOwner t")
public class ThirdPartyHasOwner implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private ThirdPartyHasOwnerPK id;

    @Temporal(TemporalType.DATE)
    private Date createdDate;

    //bi-directional many-to-one association to Owner
    @ManyToOne
    private Owner owner;

    //bi-directional many-to-one association to ThirdParty
    @ManyToOne
    @JoinColumn(name="third_party_Id")
    private ThirdParty thirdParty;

    public ThirdPartyHasOwner() {
    }

    public ThirdPartyHasOwnerPK getId() {
        return this.id;
    }

    public void setId(ThirdPartyHasOwnerPK id) {
        this.id = id;
    }

    public Date getCreatedDate() {
        return this.createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public Owner getOwner() {
        return this.owner;
    }

    public void setOwner(Owner owner) {
        this.owner = owner;
    }

    public ThirdParty getThirdParty() {
        return this.thirdParty;
    }

    public void setThirdParty(ThirdParty thirdParty) {
        this.thirdParty = thirdParty;
    }

}

那么我对条件查询做错了什么还是一些奇怪的错误?

4

1 回答 1

0

虽然日志很好,但我使用了不同的方法来提供不同的数据集。

代码之前

CriteriaBuilder cb = emManager.getCriteriaBuilder();
CriteriaQuery<ThirdParty> cq = cb.createQuery(ThirdParty.class);
Root<ThirdParty> a = cq.from(ThirdParty.class);
Join<ThirdParty, ThirdPartyHasOwner> b = a.join("thirdPartyHasOwners", JoinType.LEFT);

ParameterExpression<Integer> balance = cb.parameter(Integer.class);

Path<Integer> path = b.get("id").get("ownerId");

cq.where(cb.gt(path, balance));

cq.select(a);

TypedQuery<ThirdParty> queryS = emManager.createQuery(cq);

List<ThirdParty> results = queryS.setParameter(balance, 1).getResultList();

代码后

CriteriaBuilder cb = emManager.getCriteriaBuilder();
CriteriaQuery<ThirdParty> cq = cb.createQuery(ThirdParty.class);
Root<ThirdParty> a = cq.from(ThirdParty.class);
Join<ThirdParty, ThirdPartyHasOwner> b = a.join("thirdPartyHasOwners", JoinType.LEFT);

ParameterExpression<Integer> balance = cb.parameter(Integer.class);

cq.where(cb.equal( b.get("id").get("ownerId"),balance));

cq.select(a);

TypedQuery<ThirdParty> queryS = emManager.createQuery(cq);

List<ThirdParty> results = queryS.setParameter(balance, 1).getResultList();

正在更改的代码是cq.where(cb.gt(path, balance));用于cq.where(cb.equal( b.get("id").get("ownerId"),balance));标准构建器 equal 在这里使用等于Where b.owner_id = balancebalance 是一个参数

于 2017-01-04T10:18:09.863 回答