1
我正在使用Hibernate/JPA來執行本機MySql查詢。我想獲得櫃檯的價值。JPA/Hibernate原生查詢無法識別EmbeddedId參數
例如,下面的查詢崩潰:
"SELECT sizeList FROM myCounter WHERE myColA=?1 AND myColB=?2 AND LIMIT 1"
錯誤:
java.lang.IllegalArgumentException: Parameter with that name [1] did not exist
來源:
方法:
@Override
public Long getSizeList(String myColA, String myColB) {
Query q = em.createNativeQuery("MyCounter.getSizeList");
q.setParameter(1, myColA);
q.setParameter(2, myColB);
return (Long) q.getSingleResult();
}
實體:
@Entity
@Table(name = "myCounter")
@XmlRootElement
@NamedNativeQueries({
@NamedNativeQuery(name = "MyCounter.getSizeList", query = "SELECT sizeList FROM myCounter WHERE myColA=?1 AND myColB=?2 AND LIMIT 1", resultClass = MyCounter.class)})
public class MyCounter implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected MyCounterPK myCounterPK;
@Column(name = "sizeList")
private BigInteger sizeList;
public PostListCounterPTTESLTTT() {
}
...
public BigInteger getSizeList() {
return sizeList;
}
}
嵌入式:
@Embeddable
public class MyCounterPK implements Serializable {
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "myColA")
private String myColA;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "myColB")
private String myColB;
public MyCounterPK() {
}
...
}
有誰知道如何在這種情況下使用的setParameter()?
我嘗試:
//case 1:
"SELECT sizeList FROM myCounter WHERE myColA=?1 AND myColB=?2 AND LIMIT 1"
q.setParameter(1, myColA); //the same error
//case 2:
"SELECT sizeList FROM myCounter WHERE myColA=?1 AND myColB=?2 AND LIMIT 1"
q.setParameter("1", myColA); //the same error
//case 3:
"SELECT sizeList FROM myCounter WHERE myColA=:myColA AND myColB=:myColB AND LIMIT 1"
q.setParameter("myColA", myColA); //the same error