要使用的Hibernate或JPA注释(Hibernate or JPA annotations to use)
我在Hibernate Domain Pojo Objects的项目和基于注释的配置中使用Hibernate。 对于基于注释的配置,我们有两个选项
- 使用
javax.persistence.*
基于JPA的注释javax.persistence.*
- 使用Hibernate Native Annotations
org.hibernate.annotations.*
目前,我们为POJO文件和Hibernate本机API(如SessionFactory,Session等)使用基于JPA的注释配置来打开会话并执行数据库操作。
在下面的事情需要你的帮助,
- 混合JPA注释和使用Hibernate本机API有什么问题吗?
- 这个链接解释了一个这样的问题(cascade-jpa-hibernate-annotation-common-wrong)
- 请提供您的专业知识,使用哪种注释
- JPA
- Hibernate原生
- 混合他们两个?
I am using Hibernate in our projects and annotation based configuration for Hibernate Domain Pojo Objects. For Annotations based configuration we have two options
- JPA based annotations using
javax.persistence.*
- Use Hibernate Native Annotations
org.hibernate.annotations.*
Currently we use JPA based annotation configuration for our POJO files and Hibernate native API like SessionFactory, Session, etc to open session and perform DB operations.
I have these questions:
- Is there any problem mixing both JPA annotations and use Hibernate native API?
- This link explains one such issue (cascade-jpa-hibernate-annotation-common-mistake)
- Please provide your expertise, which type of annotations to use
- JPA
- Hibernate native
- Mix both of them?
原文:https://stackoverflow.com/questions/16052347
最满意答案
没有办法,因为
conn.exec_params
是本地C实现,并且返回的对象PG::Resul
t也无法显示SQL。 如果你真的想看到这个查询,你可以在你的postgress配置中打开语句日志记录并查看它然而,您可能会尝试查看是否有一些信息可用于错误常量中,但必须强制给出错误信息(请注意,我
alpha_non_existent
查询中的表名改为alpha_non_existent
以强制错误):begin resclose = conn.exec_params( %q{ SELECT * FROM alpha_non_existent WHERE pname ILIKE $1 ORDER BY id}, [submitted] ) rescue PG::Error => err p [ err.result.error_field( PG::Result::PG_DIAG_SEVERITY ), err.result.error_field( PG::Result::PG_DIAG_SQLSTATE ), err.result.error_field( PG::Result::PG_DIAG_MESSAGE_PRIMARY ), err.result.error_field( PG::Result::PG_DIAG_MESSAGE_DETAIL ), err.result.error_field( PG::Result::PG_DIAG_MESSAGE_HINT ), err.result.error_field( PG::Result::PG_DIAG_STATEMENT_POSITION ), err.result.error_field( PG::Result::PG_DIAG_INTERNAL_POSITION ), err.result.error_field( PG::Result::PG_DIAG_INTERNAL_QUERY ), err.result.error_field( PG::Result::PG_DIAG_CONTEXT ), err.result.error_field( PG::Result::PG_DIAG_SOURCE_FILE ), err.result.error_field( PG::Result::PG_DIAG_SOURCE_LINE ), err.result.error_field( PG::Result::PG_DIAG_SOURCE_FUNCTION ), ] end
There is no way because the
conn.exec_params
is a native C implementation, and the returned objectPG::Resul
t also has no way to show the SQL. If you really want to see the query you can turn on statement logging in your postgress configuration and have a look at itYou might however try to see if there is some information you might use in the error constants, but you'll have to force an error to get it (note that I changed the name of the table in the query to
alpha_non_existent
to force an error):begin resclose = conn.exec_params( %q{ SELECT * FROM alpha_non_existent WHERE pname ILIKE $1 ORDER BY id}, [submitted] ) rescue PG::Error => err p [ err.result.error_field( PG::Result::PG_DIAG_SEVERITY ), err.result.error_field( PG::Result::PG_DIAG_SQLSTATE ), err.result.error_field( PG::Result::PG_DIAG_MESSAGE_PRIMARY ), err.result.error_field( PG::Result::PG_DIAG_MESSAGE_DETAIL ), err.result.error_field( PG::Result::PG_DIAG_MESSAGE_HINT ), err.result.error_field( PG::Result::PG_DIAG_STATEMENT_POSITION ), err.result.error_field( PG::Result::PG_DIAG_INTERNAL_POSITION ), err.result.error_field( PG::Result::PG_DIAG_INTERNAL_QUERY ), err.result.error_field( PG::Result::PG_DIAG_CONTEXT ), err.result.error_field( PG::Result::PG_DIAG_SOURCE_FILE ), err.result.error_field( PG::Result::PG_DIAG_SOURCE_LINE ), err.result.error_field( PG::Result::PG_DIAG_SOURCE_FUNCTION ), ] end
相关问答
更多-
gem pristine pg错误失败(gem pristine pg error fails)[2022-05-09]
$ gem pristine pg --version 0.14.1 pg --version 0.12.2 -- --with-pg-config=/Library/PostgreSQL/9.1/bin/pg_config Restoring gems to pristine condition... Building native extensions. This could take a while... Restored pg-0.12.2 Building native extensions. ... -
如您的错误日志中所述,您需要将路径传递给pg_config。 尝试使用以下方式安装宝石: gem install pg -- --with-pg-config= 'PATH_TO_YOUR_PG_CONFIG' 如果您不确定pg_config在哪里,并且假设您在Linux或Mac上,则可以运行以下命令: which pg_config 您的pg-config可以位于不同的位置,具体取决于您安装postgres的方式。 As stated in your error log you need to pas ...
-
无法安装pg宝石(unable to install pg gem)[2023-07-22]
在这里回答: 无法在Windows上安装pg gem 昨天没有发布最新版本的pg(0.10.0)的Windows本机版本,但如果您安装0.9.0,则应该安装二进制文件。 Answered here: Can't install pg gem on Windows There is no Windows native version of latest release of pg (0.10.0) released yesterday, but if you install 0.9.0 it should i ... -
我用自制软件安装pg,解决了我的问题: $ brew install postgres I used homebrew to install pg and that solved my problems: $ brew install postgres
-
调用postgres命令,如pg_restore或pg_dump,形成sql文件(Calling postgres command like pg_restore or pg_dump form sql file)[2023-04-06]
可以实现您想要的效果,但不可能以您想要的方式实现。 使用可以在shell中运行的文件,例如bash(.sh),因为你在Unix系统上。 在该文件中,您可以使用psql和pg_dump命令运行您的sql文件。 shell文件看起来像这样: psql -p-U -h -d pg_dump -p5444 -U "username" -Fc -hhost-name -d Database_name ... -
Ruby PG提交了SQL(Ruby PG Submitted SQL)[2023-11-08]
没有办法,因为conn.exec_params是本地C实现,并且返回的对象PG::Resul t也无法显示SQL。 如果你真的想看到这个查询,你可以在你的postgress配置中打开语句日志记录并查看它 然而,您可能会尝试查看是否有一些信息可用于错误常量中,但必须强制给出错误信息(请注意,我alpha_non_existent查询中的表名改为alpha_non_existent以强制错误): begin resclose = conn.exec_params( %q{ SELECT * FROM a ... -
Ruby PG gem连接问题(Ruby PG gem connection issue)[2023-10-25]
两点: 将你的Sinatra助手包裹在helpers {}区块中。 这将允许您使用settings.db_config而不是Sinatra::Application.db_config : helpers do def db_connection connection = PG.connect(settings.db_config) yield connection ensure connection.close end end 在PG.connect调用中,您应该传递 ... -
从Ruby运行SQL脚本(Run sql script from Ruby)[2021-01-11]
使用一个函数或只运行多个准备好的查询。 Running multiple queries using DatabaseHandle#do is a missing feature in DBD-Pg. See Ruby/DBI feature 28001. Note that Pg, the native postgresql Ruby driver on which DBD-Pg is based, allows running multiple queries. Example: require 'pg ... -
我使用的是Postgres.app的旧版本。 将其更新到最新版本并重新安装ruby(通过RVM)解决了我的问题。 关于为什么这样解决问题的当前理论:显然Postgres.app提供了psql或在编译pg gem的原生扩展的过程中使用的其他东西。 无论Postgres.app提供什么都不好,所以结果自己传播到gem和app本身。 I was using an old version of Postgres.app. Updating it to the latest version and re-instal ...
-
SQL中的字符串文字使用sigle引号,双引号用于标识符(例如表名和列名)。 因此,当您提到"active" ,数据库会抱怨没有这样的列。 解决方案是使用占位符: @res = conn.exec_params( %q{SELECT count(id) FROM users WHERE username = $1 AND status = $2}, ['johnny5', 'active'] ) 或者在SQL中使用单引号: @res = conn.exec_params( %q{SELECT ...