2

如何在 JPQL 的 JPA 的 IN 子句中使用字符?例如在 oracle DB 我有这个查询:

select * from channel o where channel_mode IN ('O','R')

通道模式是 DB 中的 char 列。

我从 JPQL 收到一个错误invalid IN argument[o],它是一个字符。

@Chris 这是 JPQL:

typeMasterList = em.createQuery("select object(o) from ChannelRequest as o where     o.crqState IN ('O','R') and o.crqMode = 'B' ").getResultList();

实体 ChannelRequest 是:

@Entity
@Table(name = "CHANNEL_REQUEST")
@XmlRootElement

public class ChannelRequest implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "CRQ_ID")
private Long crqId;
@Size(max = 100)
@Column(name = "CRQ_QUEUE")
private String crqQueue;
@Size(max = 160)
@Column(name = "CRQ_DATA")
private String crqData;
@Column(name = "CRQ_STATE")
private Character crqState;
@Column(name = "ACTIVE_FLAG")
private Short activeFlag;
@Column(name = "ENABLED")
private Short enabled;
@Size(max = 100)
@Column(name = "CRQ_ADDRESS")
private String crqAddress;
@Column(name = "CRQ_MODE")
private Character crqMode;
@Column(name = "DCO")
@Temporal(TemporalType.TIMESTAMP)
private Date dco;
@Column(name = "DLUO")
@Temporal(TemporalType.TIMESTAMP)
private Date dluo;
@OneToMany(mappedBy = "rpmrqCrId")
private Collection<RpmRequest> rpmRequestCollection;
@JoinColumn(name = "MM_ID", referencedColumnName = "MM_ID")
@ManyToOne
private MediumMaster mmId;
@JoinColumn(name = "MC_ID", referencedColumnName = "MC_ID")
@ManyToOne
private MediumChannel mcId;`enter code here`

这是我得到的错误:invalid IN expression argument [O], expected argument of type [java.lang.Character]

4

2 回答 2

1

JPA 规范声明字符串文字用单引号括起来(例如'X',长度为 1 的字符串不是字符X)。

在您的情况下,JPQL 需要一个字符列表,而不是字符串。您可以通过将字符列表作为参数传递来解决此问题:

typeMasterList = em.createQuery("... o.crqState IN :states ...")
                   .setParameter("states", Arrays.asList('O', 'R'))
                   .getResultList();

旁注:使用枚举而不是字符可以让你的生活更轻松。然后,您可以直接在查询中引用枚举值而不会出现问题。在数据库级别,枚举仍然可以映射到单个字符。

于 2013-09-05T16:38:09.103 回答
0

Did you try using a string instead of a char, i.e. "O", "R".

于 2013-09-03T14:20:12.353 回答