简体   繁体   中英

JPA 1.2 createQuery Where Clause Criteria

Hi there I am complete newbie to JPA. I am trying to select a particular set of records using one column as the criteria. My entity code was auto generated based on the table structure as follows:

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")
    );

Contructor Code for Entiry Class:

@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

I then do the following:

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

Upon running the above I get the following error:

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".

Any pointers on this please ? Many thanks..

Since the Column1 is part of the embededId you have to go through the id field:

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

PS: Your columns seem to have rather poorly chosen names..? Perhaps you should give them some more descriptive names?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM