我想在 Postgres 9.4 数据库系统上通过 JPA 2.0 和版本 4.2.21 中的 Hibernate 执行 native SQL 查询。
基本上根据我最新的post在 stackoverflow 上,我尝试将大量对象/记录放入“时间”桶中。
设置可以简化为以下设置,其中包含一个带有 id 字段和给定时间戳的表“MyObject”:
CREATE TABLE myobject
(
id bigint NOT NULL,
lastseen timestamp without time zone,
)
我的一段代码,应该执行查询是这个:
Query q = getEntityManager().createNativeQuery(
"select count(id),date_part('day', :startDate - c.lastseen) AS " +
"difference from myobject c " +
"group by date_part('day', :startDate - c.lastseen) order by difference asc");
q.setParameter("startDate", startDate);
List<Object[]> rawResults = q.getResultList();
//process the reuslts
通过 pgAdmin3 使用示例日期执行此查询会返回预期的结果。
但是,如果我尝试通过 Hibernate 执行与 native 查询相同的查询,它会失败并出现以下异常:
Caused by: org.postgresql.util.PSQLException: FEHLER: column „myobject.lastseen“ must appear in the group by clause or be used in an aggregate function Position: 40 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2270) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1998) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:570) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:420) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:305) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79) ... 94 more
此异常似乎有效且不言自明,但为什么我可以通过 PgAdmin3 执行相同的查询? Hibernate SQL 解析器是否比 pgAdmin3 更严格,或者它是否弥补了一些错误?
那么如何制定我的 SQL 查询以使其通过 Hibernate 可执行?
编辑:
出于某种原因,以下 SQL 语句(带有显式子选择)可以通过 PgAdmin3 和 Hibernate 工作:
select count(id), difference
from (select c.id,c.lastseen,date_part('day', :startDate - c.lastseen) AS difference
from myobject c) AS temporalBucket
group by difference
order by difference asc
但这仍然没有回答给定代码片段中先前查询的问题。
最佳答案
这个查询应该也能工作,没有子查询:
SELECT count(id) -- or even better: count(*) AS ct
, date_part('day', :startDate - c.lastseen) AS difference
FROM myobject c
GROUP BY <b>difference</b>
ORDER BY difference;
我怀疑的原因:Hibernate 使用prepared statements :startDate 的两次出现作为两个 参数传递。这样,Postgres 就不能假定两个表达式(在 SELECT 列表和 GROUP BY 中)是相同的......
用等效的 SQL command PREPARE 进行演示,这有效:
PREPARE test1 AS
SELECT count(*) AS ct
, date_part('day', $1 - c.lastseen) AS difference
FROM myobject c
GROUP BY date_part('day', <b>$1</b> - c.lastseen)
ORDER BY difference;
虽然这不是:
PREPARE test2 AS
SELECT count(*) AS ct
, date_part('day', $1 - c.lastseen) AS difference
FROM myobject c
GROUP BY date_part('day', <b>$2</b> - c.lastseen)
ORDER BY difference;
.. 并引发与您显示的相同的异常。
您可以使用我建议的查询先验地避免该问题。
相关:
关于java - 通过导致 PSQLException 的 Hibernate 和 PostgreSQL 执行查询的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34251063/
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我正在用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.
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m