gpt4 book ai didi

java - JPA 命名查询参数

转载 作者:行者123 更新时间:2023-12-01 19:02:24 27 4
gpt4 key购买 nike

是否有任何约定如何在JPQL中命名参数?因为今天经过一些调试后我意识到我不能在名称中输入例如冒号。

我还发现了第二个问题,由于某种原因我不能使用常量作为参数名称。我总会得到一个

org.hibernate.hql.ast.HqlSqlWalker.lookupProperty(HqlSqlWalker.java:466)

这是我的代码

@NamedQueries({
@NamedQuery(
name=Lookup.GET_LOOKUP_DATA_QUERY,
query="SELECT t.textShort, lookup.metaCode, lookup.tableName FROM Lookup lookup, Txt t" +
" WHERE (lookup.metaCode = t.txtHeadCode OR t.txtHeadCode IS NULL) AND lookup.module.id =:" + Lookup.PARAM_MODULE_ID +
" AND lookup.metaCode IN (:" + Lookup.PARAM_LOOKUP_META_CODES + ") AND t.lang.metaCode = 'lang_cz'")
})

@Entity
@Table(name = "TABLE")
public class Lookup {

private static final long serialVersionUID = 1L;

public static final String GET_LOOKUP_DATA_QUERY = "Lookup.GetLookupDataQuery";
public static final String PARAM_MODULE_ID = "Lookup.PARAM_MODULE_ID";
public static final String PARAM_LOOKUP_META_CODES = "Lookup.PARAM_LOOKUP_META_CODES";

@Id
@Column(name ="META_CODE")
private String metaCode;

@Column(name = "ID")
private Long id;

@ManyToOne
@JoinColumn(name="MODULE_ID")
private Module module;
}

+ getter、setter 和其他属性

提供程序是Hibernate,但查询是用纯JPA编写的

有谁知道如何解决这个问题吗?

最佳答案

您不能使用不属于 Character.isJavaIdentifierPart 定义的有效标识符的字符。 。命名参数在 JPA 2.0 规范中定义:

A named parameter of a Java Persistence query language query is an identifier that is prefixed by the ":" symbol. The parameter names passed to the setParameter methods of the Query and TypedQuery interfaces do not include this ":" prefix.
...
An identifier is a character sequence of unlimited length. The character sequence must begin with a Java identifier start character, and all other characters must be Java identifier part characters. An identifier start character is any character for which the method Character.isJavaIdentifierStart returns true. This includes the underscore (_) character and the dollar sign ($) character. An identifier part character is any character for which the method Character.isJavaIdentifierPart returns true. The question mark (?) character is reserved for use by the Java Persistence query language.

在构建 @NamedQuery 的查询字符串时使用常量不会导致问题。它是编译时构造,Hibernate 不知道该查询是否使用常量构建。

在您当前的查询中,所有命名参数均无效。那是因为“.”。根据Character.isJavaIdentifierPart,它不是有效字符之一。

关于java - JPA 命名查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11718846/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com