This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

facing problem while creating JPA namedQuery

 
anish reddy
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • 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"
 
Aurelian Tutuianu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • 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: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the the help,i got it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic