Cannot deploy the project, Caused by: Errors in named queries

165 views Asked by At

I have a query. But it does not work. I tested it on Oracle SQL Developer, It worked. So I wrote it in namedquery. The query is below that works fine on Oracle SQL Developer

   SELECT q.* FROM TAKE_EXAM_QUESTION q 
   INNER JOIN TAKE_EXAM e 
   on q.tk_exam_id = e.tk_exam_id 
   where e.user_id= :userId;

And I typed above query in Entity class

 @NamedQuery(name = "TakeExamQuestionEntity.question", query = "SELECT qs FROM          TakeExamQuestionEntity qs INNER JOIN TakeExamEntity ex on qs.tk_exam_id = ex.tk_exam_id where    ex.user_id= :userId "),

But it is not working, I do not know why now working please suggest me.

1

There are 1 answers

0
wittakarn On BEST ANSWER

If you generate entity from table, any fields of table will be remove _ and first character after _ will be upper case. Thus, you should write NamedQuery as below example.

@NamedQuery(name = "TakeExamQuestionEntity.question", 
            query = "SELECT qs FROM TakeExamQuestionEntity qs 
                     INNER JOIN TakeExamEntity ex on qs.tkExamId = ex.tkExamId 
                     where ex.userId= :userId ")

If it not work, you should check you entity they are separated to 2 class (entity Primary Key and entity class) or not.

@Embeddable
public class EmployeePK implements Serializable {
    private String name;
    private long id;

    public EmployeePK() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public int hashCode() {
        return (int) name.hashCode() + id;
    }

    public boolean equals(Object obj) {
        if (obj == this) return true;
        if (!(obj instanceof EmployeePK)) return false;
        if (obj == null) return false;
        EmployeePK pk = (EmployeePK) obj;
        return pk.id == id && pk.name.equals(name);
    }
}


@Entity
public class Employee implements Serializable {
    EmployeePK primaryKey;

    public Employee() {
    }

    @EmbeddedId
    public EmployeePK getPrimaryKey() {
        return primaryKey;
    }

    public void setPrimaryKey(EmployeePK pk) {
        primaryKey = pk;
    }

    ...
}

If your entity has generated as 2 class as above example. You should define

SELECT e FROM Employee e 
where e.primaryKey.name=:name

the primaryKey will be an object of EmployeePK that has annotation Embeddable.

If you want to use native query you should use @NamedNativeQuery instead of @NamedQuery.

See also: NamedNativeQuery