我确信这是一个简单的问题,但是在尝试使其工作时遇到了问题。简而言之,我在Web应用程序中使用了搜索功能(使用JPA 2.1,Java 1.7.0 JDK,Primefaces 4.0,Oracle 10g db,JBoss EAP 6.2,NetBeans 8.0 IDE),它应该搜索数据库中的2个表( “父母”和“孩子”)。更具体地说,我想从子表中获取2个值,并且与父表中的数据一起使用结果填充表单。
实体:
“父”实体
`@Entity
@Table(name = "FOREIGN_PARTY")
@NamedQueries({@NamedQuery(name = "ForeignParty.findbyPolicy1Num", query = "SELECT f FROM ForeignParty f JOIN f.policyPaymentCollection pp WHERE pp.policy1Num = :policy1Num")})
@OneToMany(cascade = CascadeType.ALL, mappedBy = "foreignPartyId")
private Collection<PolicyPayment> policyPaymentCollection;
// getter and setter for Collection field`
“儿童”实体
@Entity
@Table(name = "POLICY_PAYMENT")
@JoinColumn(name = "FOREIGN_PARTY_ID", referencedColumnName = "FOREIGN_PARTY_ID")
@ManyToOne
private ForeignParty foreignPartyId;
// getter and setter for foreignPartyId field
根据用户在primefaces搜索表单上输入的条件,调用以下逻辑:
public void findSearchCriteriaItems() {
if (!selected.getSourceClientId().equals("") && !selected.getSourceSystem().equals("")
&& !this.policyPaymentController.getSelected().getPolicy1Num().equals("")) {
listSearchItemsEJB = ejbFacade.findRecordsBySourceSystemClientIdPolicyNum(selected.getSourceClientId(),
selected.getSourceSystem(), this.policyPaymentController.getSelected().getPolicy1Num());
} else if (!selected.getSourceClientId().equals("") && this.policyPaymentController.getSelected().getPolicy1Num().equals("")
&& selected.getSourceSystem().equals("")) {
listSearchItemsEJB = ejbFacade.findRecordsBySourceClientId(selected.getSourceClientId());
} else if (selected.getSourceClientId().equals("") && this.policyPaymentController.getSelected().getPolicy1Num().equals("")
&& !selected.getSourceSystem().equals("")) {
listSearchItemsEJB = ejbFacade.findRecordsBySourceSystem(selected.getSourceSystem());
} else if (selected.getSourceSystem().equals("") && selected.getSourceClientId().equals("")
&& !this.policyPaymentController.getSelected().getPolicy1Num().equals("")) {
listSearchItemsEJB = ejbFacade.findRecordsByPolicyNum(this.policyPaymentController.getSelected().getPolicy1Num());
} else {
listSearchItemsEJB = null;
}
for (int i = 0; i < listSearchItemsEJB.size(); i++) {
ForeignParty searchRecords = listSearchItemsEJB.get(i);
items.add((ForeignParty) searchRecords);
}
}
查询db:
的查询@PersistenceContext(unitName = "FatcaPU")
private EntityManager em;
private Query query;
@Override
protected EntityManager getEntityManager() {
return em;
}
public List<ForeignParty> findRecordsByPolicyNum(String policyNum) {
query = em.createNamedQuery("ForeignParty.findbyPolicy1Num");
query.setParameter("policy1Num", policyNum);
List<ForeignParty> searchCriteriaList = (List<ForeignParty>) query.getResultList();
return searchCriteriaList;
}
问题是并非所有数据都是从“子”表返回的,因此不会在表单中填充。所以,基本上,“子”表中只有2个值需要检索并放入表单中。如果上面的代码看起来令人困惑,我很抱歉。我离开了很多,因为这些课程相当大。任何帮助将不胜感激,如果需要更多信息,我将很乐意提供。谢谢!