java JPA 中的 PreparedStatement 替代方案?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2447717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:12:22  来源:igfitidea点击:

PreparedStatement alternative within JPA?

javamysqljpa

提问by mebada

I am new to JPA, I used to used prepared statement in JDBC. Is there alternative to be used within JPA ? as there is a query which I call frequently

我是 JPA 的新手,我曾经在 JDBC 中使用过准备好的语句。在 JPA 中是否有替代方案?因为有一个我经常打电话的查询

see this for info about prepared statment http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html

有关准备好的声明的信息,请参阅此http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html

thanks in advance,,,

提前致谢,,,

采纳答案by BalusC

The JPA way is EntityManager#createQuery()with named parametersin JPQL(examples in this chapter).

JPA的方法是EntityManager#createQuery()命名参数JPQL(在例子本章)。

回答by Pascal Thivent

The Java Persistence API provides a Query API to create dynamic queries and named queries. The Query method setParameterbinds an argument to a parameter (both named queries and dynamic queries can use named parameters as well as positional parameters, you just can't mix both types of parameters).

Java Persistence API 提供了一个 Query API 来创建动态查询和命名查询。Query 方法setParameter将参数绑定到参数(命名查询和动态查询都可以使用命名参数和位置参数,只是不能混合使用这两种类型的参数)。

Have a look at Creating Queries Using the Java Persistence Query Languagein tha Java eE 6 tutorial for examples.

看一看创建查询使用Java持久化查询语言在临Java EE 6教程中的例子。

回答by mikek

What are you trying to accomplish? =)

你想达到什么目的?=)

If you have a statement which you use rather often, wouldn't that be a good reason to put it in a method in a session bean/DAO?

如果您有一个经常使用的语句,那么将它放在会话 bean/DAO 的方法中难道不是一个很好的理由吗?