• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

facing problem while creating JPA namedQuery

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
iam facing problem while creating a named Query in an interface as a variable and use that query in an entity.i paste my code below

it is an interface which i define queries as variables
package org.an;

import javax.persistence.NamedQuery;

public interface StudentQueries
{
static String STUDENT_FIND_ALL_NAME="\"Student.findAll\"";
static String STUDENT_FIND_ALL="\"select s FROM Student s\"";

}
it is an enity wher i used above said named queries declared in an interface

package org.an;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
@Entity
@Table(name="student")
@NamedQueries
({

@NamedQuery(name=StudentQueries.STUDENT_FIND_ALL_NAME,query=StudentQueries.STUDENT_FIND_ALL)

})

public class Student implements Serializable,StudentQueries

{

@Id
@Column(name="sno")
String sno;
@Column(name="sname")
String sname;
@Column(name="sadd")
String sadd;
/**
* @return the sno
*/
public String getSno() {
return sno;
}
/**
* @param sno the sno to set
*/
public void setSno(String sno) {
this.sno = sno;
}
/**
* @return the sname
*/
public String getSname() {
return sname;
}
/**
* @param sname the sname to set
*/
public void setSname(String sname) {
this.sname = sname;
}
/**
* @return the sadd
*/
public String getSadd() {
return sadd;
}
/**
* @param sadd the sadd to set
*/
public void setSadd(String sadd) {
this.sadd = sadd;
}
public Student() {

}

}
this is a main program
/**
*
*/
package org.an;

import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
* @author ganani1000
*
*/
public class StudentMain {

/**
* @param args
*/
public static void main(String[] args) {
EntityManager em=null;
try{


javax.persistence.EntityManagerFactory emf=javax.persistence.Persistence.createEntityManagerFactory("ttt");
em=emf.createEntityManager();
Query q=em.createNamedQuery(StudentQueries.STUDENT_FIND_ALL_NAME);
System.out.println(q.getResultList().size());

}
finally{
if(em!=null&& em.isOpen())
{
em.close();
}
}


}

}
iam getting exception when i run client program ie Exception Description: Syntax error parsing the query ["Student.findAll": "select s FROM Student s"], line 1, column 1: unexpected token ["select s FROM Student s"].
Internal Exception: line 1:1: unexpected token: "select s FROM Student s"
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think the problem is that you put char " inside strings.
So, you should have:
Notice that I removed \" from strings.
Hope it helps.
 
anish reddy
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the the help,i got it
 
reply
    Bookmark Topic Watch Topic
  • New Topic