我正在使用 JPA(EclipseLink 风格),框架 JSF 2.0,查询包含 4 个字段的 MySQL 联系人表:
如果仅使用字符串参数,则以下代码可以正常工作,以便用户可以根据需要输入尽可能多或尽可能少的搜索字符串(即:如果搜索框留空,则相当于选择 * 并且整个数据集是回)。
问题在于组合一个整数参数 (countryId),它是将国家添加到搜索条件的外键。经过一些研究,我仍然无法理解正确的方法来做到这一点。
是否需要在 searchContacts 方法中将整数转换为字符串表示形式?我收集到 JPA Criteria API 提供类型转换但不提供类型转换方法?如果是这样,在下面的方法中包含一个整数并将这个整数传递给 predicateArray 的最佳方法是什么?
提前致谢!
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 | CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Contacts> query = cb.createQuery(Contacts.class); Root<Contacts> cont = query.from(Contacts.class); query.select(cont); List<Predicate> predicateList = new ArrayList<Predicate>(); Predicate firstnamePredicate, surnamePredicate, countryPredicate; if ((firstname != null) && (!(firstname.isEmpty()))) { firstnamePredicate = cb.like(cb.upper(cont.<String>get("firstname")),"%" + firstname.toUpperCase() +"%"); predicateList.add(firstnamePredicate); } if ((surName != null) && (!(surName.isEmpty()))) { surnamePredicate = cb.like(cb.upper(cont.<String>get("surname")),"%" + surName.toUpperCase() +"%"); predicateList.add(surnamePredicate); } // here is where I am stuck and trying the solution suggested by meskobalazs, except I changed null to 0 since countryId is an integer if (countryId != 0) { countryPredicate = cb.equal(cont.<Integer>get("countryid"), countryId); predicateList.add(countryPredicate); } Predicate[] predicateArray = new Predicate[predicateList.size()]; predicateList.toArray(predicateArray); query.where(predicateArray); ListDataModel<Contacts> contactList = new ListDataModel<Contacts>(em.createQuery(query).getResultList()); return contactList; } |
}
上述导致以下 EJB 异常:
2 3 4 5 6 7 8 9 10 | 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 映射。联系人实体是:
2 3 4 5 6 7 8 9 10 11 12 13 | @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; |
国家实体是:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @Table(name ="country") @XmlRootElement public class Country implements Serializable { private static final long serialVersionUID = 1L; @Id @Basic(optional = false) @NotNull @Column(name ="COUNTRYID") private Integer countryid; @Size(max = 255) @Column(name ="COUNTRYNAME") private String countryname; ..... |
2 3 4 | countryPredicate = cb.equal(cont.<Integer>get("countryid"), countryId); predicateList.add(countryPredicate); } |
JPA 规范元模型
这只是一个建议,但您也可以考虑为您的实体构建规范元模型(例如使用 Apache Dali),这样您就可以以类型安全的方式
代替
你可以写:
如果您正在重构实体,这尤其有用,因为您可能会忘记更新字符串,但您不能忘记更新元模型,因为代码甚至无法编译。
更新
我遗漏了一些与您的实体有关的设计问题。有两种可能的解决方案。
创建一个新的实体字段
第一个是在
所以你应该将
2 3 4 5 6 | private Integer countryId; @JoinColumn(name ="countryid", referencedColumnName ="countryid") @ManyToOne private Country country; |
完成此操作后,您就可以使用我原来的解决方案了。
使用连接
第二种解决方案是在查询中使用
2 3 4 5 | Join<Contacts, Country> country = cont.join("countryid"); countryPredicate = cb.equal(country.<Integer>get("countryId"), countryId); predicateList.add(countryPredicate); } |
我在开发的Rails3网站的一些搜索功能上遇到了一个小问题。我有一个简单的Post模型,如下所示:classPost我正在使用acts_as_taggable_on来更轻松地向我的帖子添加标签。当我有一个标记为“rails”的帖子并执行以下操作时,一切正常:@posts=Post.tagged_with("rails")问题是,我还想搜索帖子的标题。当我有一篇标题为“Helloworld”并标记为“rails”的帖子时,我希望能够通过搜索“hello”或“rails”来找到这篇帖子。因此,我希望标题列的LIKE语句与acts_as_taggable_on提供的tagged_with方法
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr