JPA criteria predicate query combining Integer and String arguments
我正在使用 JPA(EclipseLink 风格),框架 JSF 2.0,查询包含 4 个字段的 MySQL 联系人表:
如果仅使用字符串参数,则以下代码可以正常工作,以便用户可以根据需要输入尽可能多或尽可能少的搜索字符串(即:如果搜索框留空,则相当于选择 * 并且整个数据集是回)。
问题在于组合一个整数参数 (countryId),它是将国家添加到搜索条件的外键。经过一些研究,我仍然无法理解正确的方法来做到这一点。
是否需要在 searchContacts 方法中将整数转换为字符串表示形式?我收集到 JPA Criteria API 提供类型转换但不提供类型转换方法?如果是这样,在下面的方法中包含一个整数并将这个整数传递给 predicateArray 的最佳方法是什么?
提前致谢!
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
public ListDataModel<Contacts> searchContacts(String firstname, String surName, int countryId) {
CriteriaBuilder cb = em.getCriteriaBuilder(); List<Predicate> predicateList = new ArrayList<Predicate>();
if ((firstname != null) && (!(firstname.isEmpty()))) { if ((surName != null) && (!(surName.isEmpty()))) { return contactList; |
}
上述导致以下 EJB 异常:
1
2 3 4 5 6 7 8 9 10 |
Caused by: Exception [EclipseLink–6078] (Eclipse Persistence Services – 2.5.0.v20130507–3faac2b): org.eclipse.persistence.exceptions.QueryException
Exception Description: The class of the argument for the object comparison is incorrect. Expression: [ Base com.manaar.domains.Contacts] Mapping: [org.eclipse.persistence.mappings.ManyToOneMapping[countryid]] Argument: [1] Query: ReadAllQuery(referenceClass=Contacts ) at org.eclipse.persistence.exceptions.QueryException.incorrectClassForObjectComparison(QueryException.java:595) at org.eclipse.persistence.mappings.OneToOneMapping.buildObjectJoinExpression(OneToOneMapping.java:287) at org.eclipse.persistence.internal.expressions.RelationExpression.normalize(RelationExpression.java:803) |
countryid 是一个整数,在联系人实体到国家实体中具有 manyToOne 映射。联系人实体是:
1
2 3 4 5 6 7 8 9 10 11 12 13 |
@Entity
@Table(name =“contacts”) @XmlRootElement public class Contacts implements Serializable { private static final long serialVersionUID = 1L; @Id @Basic(optional = false) @NotNull @Column(name =“CONTACTSID”) private Integer contactsid; @JoinColumn(name =“countryid”, referencedColumnName =“COUNTRYID”) @ManyToOne private Country countryid; |
国家实体是:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@Entity
@Table(name =“country”) @XmlRootElement public class Country implements Serializable { |
- 嗨,Jay,你能分享你的联系人实体吗?它来查找您的 CountryId 是整数还是 Country 表的实体。另外也共享异常。
- 嗨,阿杰。感谢您的回复。我在正文中包括了联系人和国家实体,并解释了它们之间的关系。我没有包括任何例外,因为我什至没有尝试任何正确的方法,所以我需要首先了解正确的工作概念。如果您需要任何进一步的信息,请告诉我。
谓词的解决方案
Predicate 类没有类型(通用),因此您不需要以任何不同的方式收集不同的谓词。这应该可以正常工作:
1
2 3 4 |
if (countryId != null) {
countryPredicate = cb.equal(cont.<Integer>get(“countryid”), countryId); predicateList.add(countryPredicate); } |
JPA 规范元模型
这只是一个建议,但您也可以考虑为您的实体构建规范元模型(例如使用 Apache Dali),这样您就可以以类型安全的方式 get 您的字段。
代替
1
|
cont.<MyType>get(“typeName”)
|
你可以写:
1
|
cont.get(_MyType.typeName)
|
如果您正在重构实体,这尤其有用,因为您可能会忘记更新字符串,但您不能忘记更新元模型,因为代码甚至无法编译。
更新
我遗漏了一些与您的实体有关的设计问题。有两种可能的解决方案。
创建一个新的实体字段
第一个是在 Contacts 中创建一个实际的 Integer 字段并将其映射到与 countryId 相同的字段。
所以你应该将 Country 字段重命名为 Country,并创建一个新字段。
1
2 3 4 5 6 |
@Column(name =“countryid”, insertable = false, updatable = false)
private Integer countryId; @JoinColumn(name =“countryid”, referencedColumnName =“countryid”) |
完成此操作后,您就可以使用我原来的解决方案了。
使用连接
第二种解决方案是在查询中使用 Join。
1
2 3 4 5 |
if (countryId != null) {
Join<Contacts, Country> country = cont.join(“countryid”); countryPredicate = cb.equal(country.<Integer>get(“countryId”), countryId); predicateList.add(countryPredicate); } |
- 感谢您提供有用的建议mesko。根据您的第一个建议,我尝试了 if(countryId !=0),因为 null 不适用于整数。不幸的是,它产生了一个 EJB 异常,我将在问题的主体中发布。至于规范元模型,我首先想根据您的第一个建议掌握一种添加整数参数的基本方法。
- 当然,它适用于 Integer。那是一个对象,不要将它与 int 原语混淆。
- 我也很惊讶如果我尝试添加 if(countryId !=null) 会出现编译时错误,但如果我声明 (countryId != 0) 则没有错误。我之前使用过整数和 null 并且它可以工作,但由于某种原因在这种情况下不是
- 我现在看到了,方法不对,第三个参数应该是Integer。
- 好的,对不起我的疏忽。我修复了第三个参数并且编译错误消失了,但我仍然有相同的 EjbException
- 哦,我明白了原因,不幸的是,您的变量名非常糟糕。 countryid 实际上是一个 Country 实体,而不是 ID。
- 是的。不幸的是,它是 Netbeans IDE 自动生成的代码(应该可以节省我的时间)。因此,国家/地区 ID 是联系人数据库中的外键。所以诀窍是如何在 CountryPredicate 的 get 方法中引用这个来获取 countryid.countryid,其中第二个 countryid 是整数。这可能是使用正确 SQL 的问题。或者我应该把 countryid 改成 country
- 好的,我不能感谢你!第二种方法有效,Join 成功。我也将尝试第一种方法,但我知道这将涉及更多配置来更改映射。再次感谢!
- 是的,第一个在编码方面更繁琐,但它节省了连接,这是一项昂贵的操作。
- 那么它可能是一个更好的选择,因为我有大约 2 个外键,用于公司和部门。所以我对第一种方法的问题是,如果映射被 countryId 整数更改,原始 Country countryid 实体会发生什么情况?仍然需要在 ORM 映射中保留 Country 实体吗?
- 让我们在聊天中继续这个讨论。
来源:https://www.codenong.com/28357505/