Jpa 使用条件与仅创建查询时的不同结果集
无标准查询的手动方法Jpa 使用条件与仅创建查询时的不同结果集,jpa,eclipselink,javax.persistence,Jpa,Eclipselink,Javax.persistence,无标准查询的手动方法 /** * 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 Serial
/**
* 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;
}
}
我使用这段代码获得了一个结果集,该结果集通过使用一个左连接和另一个表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();
(结果变量的数据错误,这是使用条件查询的结果,第三方变量的数据集正确,这是手动方法的结果)
最后但并非最不重要的一点是,我使用的是eclipselink的javax.persistence.persistence api
第三方模型类
/**
* 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;
}
}
那么,我是否对条件查询做了一些错误,或者它是某种奇怪的错误?尽管日志很好,但我还是使用了不同的方法,给出了不同的数据集 代码之前
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 cq=cb.createQuery(ThirdParty.class);
根a=cq.from(第三方类);
Join b=a.Join(“第三方所有者”,JoinType.LEFT);
ParameterExpression balance=cb.parameter(Integer.class);
路径路径=b.get(“id”).get(“ownerId”);
cq.where(cb.gt(路径、平衡));
cq.选择(a);
TypedQuery queryS=emManager.createQuery(cq);
List 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();
CriteriaBuilder cb=emManager.getCriteriaBuilder();
CriteriaQuery cq=cb.createQuery(ThirdParty.class);
根a=cq.from(第三方类);
Join b=a.Join(“第三方所有者”,JoinType.LEFT);
ParameterExpression balance=cb.parameter(Integer.class);
cq.式中(cb.相等(b.获得(“id”).获得(“所有者”),余额);
cq.选择(a);
TypedQuery queryS=emManager.createQuery(cq);
List results=queryS.setParameter(balance,1.getResultList();
正在更改的代码是
cq.where(cb.gt(路径,平衡))代码>至cq.其中(cb.相等(b.获得(“id”).获得(“所有者”),余额)代码>此处使用的标准生成器equal等于其中b.owner_id=balance
balance是一个参数虽然日志很好,但我使用了不同的方法,给出了不同的数据集
代码之前
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 cq=cb.createQuery(ThirdParty.class);
根a=cq.from(第三方类);
Join b=a.Join(“第三方所有者”,JoinType.LEFT);
ParameterExpression balance=cb.parameter(Integer.class);
路径路径=b.get(“id”).get(“ownerId”);
cq.where(cb.gt(路径、平衡));
cq.选择(a);
TypedQuery queryS=emManager.createQuery(cq);
List 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();
CriteriaBuilder cb=emManager.getCriteriaBuilder();
CriteriaQuery cq=cb.createQuery(ThirdParty.class);
根a=cq.from(第三方类);
Join b=a.Join(“第三方所有者”,JoinType.LEFT);
ParameterExpression balance=cb.parameter(Integer.class);
cq.式中(cb.相等(b.获得(“id”).获得(“所有者”),余额);
cq.选择(a);
TypedQuery queryS=emManager.createQuery(cq);
List results=queryS.setParameter(balance,1.getResultList();
正在更改的代码是cq.where(cb.gt(路径,平衡))代码>至cq.其中(cb.相等(b.获得(“id”).获得(“所有者”),余额)代码>此处使用的标准生成器等于,其中b.owner_id=balance
balance是一个参数