首页 \ 问答 \ 要使用的Hibernate或JPA注释(Hibernate or JPA annotations to use)

要使用的Hibernate或JPA注释(Hibernate or JPA annotations to use)

我在Hibernate Domain Pojo Objects的项目和基于注释的配置中使用Hibernate。 对于基于注释的配置,我们有两个选项

  1. 使用javax.persistence.*基于JPA的注释javax.persistence.*
  2. 使用Hibernate Native Annotations org.hibernate.annotations.*

目前,我们为POJO文件和Hibernate本机API(如SessionFactory,Session等)使用基于JPA的注释配置来打开会话并执行数据库操作。

在下面的事情需要你的帮助,

  1. 混合JPA注释和使用Hibernate本机API有什么问题吗?
  2. 这个链接解释了一个这样的问题(cascade-jpa-hibernate-annotation-common-wrong)
  3. 请提供您的专业知识,使用哪种注释
    1. JPA
    2. Hibernate原生
    3. 混合他们两个?

I am using Hibernate in our projects and annotation based configuration for Hibernate Domain Pojo Objects. For Annotations based configuration we have two options

  1. JPA based annotations using javax.persistence.*
  2. 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:

  1. Is there any problem mixing both JPA annotations and use Hibernate native API?
  2. This link explains one such issue (cascade-jpa-hibernate-annotation-common-mistake)
  3. Please provide your expertise, which type of annotations to use
    1. JPA
    2. Hibernate native
    3. Mix both of them?

原文:https://stackoverflow.com/questions/16052347
更新时间:2021-09-06 20:09

最满意答案

没有办法,因为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 object PG::Result 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 it

You 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 --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 ...
  • 在这里回答: 无法在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
  • 可以实现您想要的效果,但不可能以您想要的方式实现。 使用可以在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 ...
  • 没有办法,因为conn.exec_params是本地C实现,并且返回的对象PG::Resul t也无法显示SQL。 如果你真的想看到这个查询,你可以在你的postgress配置中打开语句日志记录并查看它 然而,您可能会尝试查看是否有一些信息可用于错误常量中,但必须强制给出错误信息(请注意,我alpha_non_existent查询中的表名改为alpha_non_existent以强制错误): begin resclose = conn.exec_params( %q{ SELECT * FROM a ...
  • 两点: 将你的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调用中,您应该传递 ...
  • 使用一个函数或只运行多个准备好的查询。 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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。