【问题标题】:JPA 1.2 createQuery Where Clause CriteriaJPA 1.2 createQuery Where 子句标准
【发布时间】:2012-04-13 22:24:28
【问题描述】:

您好,我是 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".

请对此有任何指示吗?非常感谢..

【问题讨论】:

    标签: java jakarta-ee jpa jpa-2.0


    【解决方案1】:

    由于 Column1 是 embededId 的一部分,您必须通过 id 字段:

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

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

    【讨论】:

    • 谢谢一百万!! Barsju 解决了这个问题,是的,我将使用正确的列名重新创建表格....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 2011-10-22
    • 2011-06-25
    相关资源
    最近更新 更多