java - JPA 1.2 createQuery Where 子句条件

标签 java jakarta-ee jpa jpa-2.0

您好,我是 JPA 的新手。 我试图使用一列作为条件来选择一组特定的记录。 我的实体代码是根据表结构自动生成的,如下:

create table TEST.COMPUTERS(
       "COLUMN1" VARCHAR2(6) not null,
       "COLUMN2" VARCHAR2(10) not null,
       "COLUMN3" VARCHAR2(5) not null,
       "COLUMN4" VARCHAR2(8) not null,
       "COLUMN5" VARCHAR2(48),
        constraint "PK" primary key ("COLUMN1","COLUMN2")
    );

整个类的构造函数代码:

@Table(name = "COMPUTERS", schema = "TEST") 
public class Computers implements java.io.Serializable {


 /** full constructor */
        public Computers(ComputersId id, String column3, String column4,
                String column5) {
            this.id = id;
            this.column3 = column3;
            this.column4 = column4;
            this.column5 = column5;
        }

          @EmbeddedId
          public ComputersId getId() {
            return this.id;
       }

      public void setId(ComputersId id) {
        this.id = id;
      }
        // and then ....getter and setter methods for Column 3-5

然后我执行以下操作:

Query query  =   EntityManagerHelper.getEntityManager().createQuery("SELECT s from Computers s where s.column1 = :column1").setParameter("column1", "SONY LAPTOPS");

运行上述命令后,出现以下错误:

An error occurred while parsing the query filter "SELECT s from Computers where s.column1 = :column1". Error message: No field named "column1" in class "class Computers".

请问对此有什么指示吗?非常感谢..

最佳答案

由于 Column1 是 embededId 的一部分,因此您必须遍历 id 字段:

Query query  =   EntityManagerHelper.getEntityManager().createQuery("SELECT s from Computers s where s.id.column1 = :column1").setParameter("column1", "SONY LAPTOPS");

PS:你的专栏名称似乎选得相当糟糕..?也许您应该给它们起一些更具描述性的名称?

关于java - JPA 1.2 createQuery Where 子句条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10149090/

相关文章:

java - .equals 和 .get 一起使用

java - 更新 JPA 实体中的所有对象

java - 拦截@Finally 和@Catch

java - mybatis 映射器 xml : The content of element type "mapper" must match

java - 使用 WPA 密码的所有允许的 ASCII 字符填充 java 数组

java - 设置自定义字段名称和代码

java - hibernate spring 不关闭连接

java - Hibernate继承(TABLE_PER_CLASS)表扩展

java - 使用 struts 2 表单标签编辑对象的 ArrayList

xml - 使用 Facelets 从 JSF2 输出 XML