简体   繁体   中英

jpa composite primary key table not returning values

I am new JPA and I am facing exceptions when I retrieve the values from composite primiary key table.

Exception Description:

Problem compiling [select t from ASSIGN_TASK_EMPLOYEE t]. 
    [14, 34] The abstract schema type 'ASSIGN_TASK_EMPLOYEE' is unknown.
        at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)

Following are my code

@Entity
        @Table(name = "ASSIGN_TASK_EMPLOYEE")
        //@IdClass(AssignTaskEmployeePk.class)
        public class AssignTaskEmployee implements Serializable {

            @EmbeddedId
            private AssignTaskEmployeePk assignTaskEmployeePk;

            public AssignTaskEmployeePk getAssignTaskEmployeePk() {
                return assignTaskEmployeePk;
            }

            public void setAssignTaskEmployeePk(AssignTaskEmployeePk assignTaskEmployeePk) {
                this.assignTaskEmployeePk = assignTaskEmployeePk;
            }

        }

       @Embeddable
        public class AssignTaskEmployeePk  {

            private String employeeId;
            private String taskId;
            public AssignTaskEmployeePk() {
            }

            @Override
            public boolean equals(Object obj) {
                // TODO Auto-generated method stub

                if (obj instanceof AssignTaskEmployeePk) {

                    AssignTaskEmployeePk employeePk = (AssignTaskEmployeePk) obj;
                    if (!employeePk.getEmployeeId().equals(this.employeeId)) {
                        return false;
                    }           
                    else if (!employeePk.getTaskId().equals(this.taskId)) {
                        return false;
                    }

                }
                else {
                    return false;
                }

                return false;

            }

            @Override
            public int hashCode() {      
                return employeeId.hashCode() + taskId.hashCode() ;
            }

            public String getEmployeeId() {
                return employeeId;
            }

            public void setEmployeeId(String employeeId) {
                this.employeeId = employeeId;
            }

            public String getTaskId() {
                return taskId;
            }

            public void setTaskId(String taskId) {
                this.taskId = taskId;
            }


            }

I have added four values in the databases for composite primary key ASSIGN_TASK_EMPLOYEE (table) which PK table

EMP_ID   TASKID
1         2
2         4
3         5
4         6 

Now I would like to get the tasks assigned to emp_id 1 For that I wrote the query below:This would supposed to return list of AssignTaskEmployee object.

entityManager.createQuery("select t from ASSIGN_TASK_EMPLOYEE t").getResultList()

When I execute this query, I am getting the following exception

Exception Description: 

Problem compiling [select t from ASSIGN_TASK_EMPLOYEE t]. 
            [14, 34] The abstract schema type 'ASSIGN_TASK_EMPLOYEE' is unknown.
                at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)

JPQL should use entities names, default are the name of the class. AssignTaskEmployee

It should be

entityManager.createQuery("select t from AssignTaskEmployee  t").getResultList()

The above will be return all the records in table ASSIGN_TASK_EMPLOYEE.

If you want to retrieve a specific record with JPQL you should use WHERE statement as it follows:

    Query query =  entityManager.createQuery("select t from AssignTaskEmployee t WHERE 
t.assignTaskEmployeePk.employeeId = :employeeId and t.assignTaskEmployeePk.taskId = :taskId")

query.setParameter("employeeId", 1);
query.setParameter("taskId",1);

query.getSingleResult()  //As expected to have only one record.

Read this to query over EmbeddedId

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