• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jpa query help please

 
shashank beerla
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

I am trying to execute a jpa query as shown in the below code and it throws an exception.

Code:

String a = "";
EntityManager em = Persistence.createEntityManagerFactory("jpasampletest").createEntityManager();

Query query = em.createQuery("select count(a) from Actor a WHERE a.first_name =:a");
query.setParameter(a, "NICK");
List<Actor> list1= query.getResultList();
System.out.println(list1.size());
for(Actor actor : list1){
System.out.println(actor.getFirstName());

Exception details :

Exception in thread "main" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Problem compiling [select count(a) from Actor a WHERE a.first_name =:a].
[35, 47] The state field path 'a.first_name' cannot be resolved to a valid type.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)
at com.org.jpasampletest.client.client.main(client.java:19)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Problem compiling [select count(a) from Actor a WHERE a.first_name =:a].
[35, 47] The state field path 'a.first_name' cannot be resolved to a valid type.
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:347)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1603)
... 1 more
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The state field path 'a.first_name' cannot be resolved to a valid type.


"first_name" indeed sounds strange for a Java property name. Is it correct?
 
shashank beerla
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the java class. first_name is the table column name

package com.org.jpasampletest;

import java.io.Serializable;
import javax.persistence.*;
import java.sql.Timestamp;


/**
* The persistent class for the actor database table.
*
*/
@Entity
@NamedQuery(name="Actor.findAll", query="SELECT a FROM Actor a")
public class Actor implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name="actor_id")
private int actorId;

@Column(name="first_name")
private String firstName;

@Column(name="last_name")
private String lastName;

@Column(name="last_update")
private Timestamp lastUpdate;

public Actor() {
}

public int getActorId() {
return this.actorId;
}

public void setActorId(int actorId) {
this.actorId = actorId;
}

public String getFirstName() {
return this.firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return this.lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Timestamp getLastUpdate() {
return this.lastUpdate;
}

public void setLastUpdate(Timestamp lastUpdate) {
this.lastUpdate = lastUpdate;
}

}
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a no.

JPQL is not SQL. You don't access tables but objects.
 
shashank beerla
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so instead first_name i need to use firstname
 
shashank beerla
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you mind if you can help me with one query for my case using where clause, please
 
shashank beerla
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed it to firstname and now i get below exception

[28, 39] The basic mapping 'a.firstName' cannot be used in conjunction with the = operator.
[42, 46] The identification variable 'NICK' is not defined in the FROM clause.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1625)
at com.org.jpasampletest.client.client.main(client.java:20)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Problem compiling [select a from Actor a WHERE a.firstName = NICK].
[28, 39] The basic mapping 'a.firstName' cannot be used in conjunction with the = operator.
[42, 46] The identification variable 'NICK' is not defined in the FROM clause.
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:347)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1603)
... 2 more
 
Richard Chesterwood
Virtual Pair Programmers Rep
Greenhorn
Posts: 29
7
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, your problem is here:



The first parameter needs to be a literal value, you're using a variable (which you've set to a blank string earlier in the code.



It would probably better if you used a more descriptive name for the named parameter (and you've used 'a' as an alias as well), such as:



I hope that works.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic