JPA 2.0中当前事务中执行一些任意sql
我是 JPA 2.0/EclipseLink/Glassfish/JEE6 的新手,有一个基本问题。
我有一个 DAO,其中大多数实体都使用 JPA 注释直接映射到列,因此我使用 EntityManager,它工作得很好,没有任何问题。
然而,有一些表我自己构建 SQL 语句,因为它们使用 Oracle 特定的函数(空间),并且我想要对 SQL 进行非常细粒度的控制。所以我用字符串连接来构建它。我希望能够在当前事务中注册我的 SQL 执行(如果已经有一个事务正在进行)。
因此,我自然不想直接进入 DriverManager 并创建自己的连接,我一直在寻找某种 EntityManager.executeArbitrarySQL(String) 函数来查找当前连接并使我的 SQL 成为当前事务的一部分。我疯了吗?
I am new to JPA 2.0/EclipseLink/Glassfish/JEE6, and have kind of a basic question.
I have a DAO in which most of the entities are mapped directly to columns using JPA annotations, so I use the EntityManager, and it works great with no issues.
However there are some tables in which I am constructing the SQL statements myself b/c they use oracle-specific functions (spatial), and I want very fine-grained control of the SQL. So I am building it with string concatenation. I would like to be able to enroll my SQL executions in the current transaction, if there is one already underway.
So naturally I don't want to go directly to the DriverManager and create my own connection, I was looking for some kind of EntityManager.executeArbitrarySQL(String) function that would find the current connection and make my SQL part of the current transaction. Am I off my rocker?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
人们可以使用 EntityManager.createNativeQuery() 方法在您使用的同一个 EntityManager 的上下文中执行本机 SQL 查询。这些方法有
two三种不同类型,它们提供的参数有所不同。第一个,
createNativeQuery(String sqlString, Class resultClass)
希望您提供表示查询将返回的值的类型的 Class 对象。如果您返回一组可以映射到持久性单元中另一个实体定义的类的值,则可以使用此方法。第二个
createNativeQuery(String sqlString, String resultSetMapping)
希望您提供结果集映射的名称。结果集映射应使用@SqlResultSetMapping 定义
注释。最后一个
createNativeQuery(String sqlString)
显然是用于不返回结果集的场景,即执行 INSERT、UPDATE 和 DELETE 语句时。您还可以使用
@NamedNativeQuery
注释或
persistence.xml
文件中的named-native-query
元素,但这些更适合您了解开发过程中查询。但是,您可以创建多个此类命名的本机查询来表示您打算执行的各种 SQL 语句,然后根据用户输入在运行时执行不同的语句。带注释的本机查询是使用 EntityManager.createNamedQuery() 方法执行的。我们需要使用位置参数(使用?
占位符定义)在运行时向本机查询提供值。One can use the
EntityManager.createNativeQuery()
methods to execute native SQL queries within the context of the same EntityManager that you are using. There aretwothree different types of these methods, and they differ in the arguments provided.The first,
createNativeQuery(String sqlString, Class resultClass)
expects you to provide the Class object representing the type of the values that will be returned by the query. This is to be used in case you are returning a set of values that can be mapped to the class of another entity definiton in your persistence unit.The second
createNativeQuery(String sqlString, String resultSetMapping)
expects you to provide the name of the result set mapping. The result set mapping ought to be defined using the@SqlResultSetMapping
annotation.The last
createNativeQuery(String sqlString)
is apparently meant to be used in scenarios where no result set will be returned, i.e. in execution of INSERT, UPDATE and DELETE statements.You can also define native queries using the
@NamedNativeQuery
annotation or thenamed-native-query
element in yourpersistence.xml
file, but these are better suited for scenarios where you know the structure of the query during development. You can however, create multiple such named native queries to represent all varieties of the SQL statement you intend to execute, and then execute different ones at runtime based on the user inputs. Annotated native queries are executed using theEntityManager.createNamedQuery()
methods. One will need to use positional parameters (defined using the?
placeholder) to supply values to the native queries at runtime.