0

I am new in maven and java. I am developing a maven project and I want to select some data with multiple rows from mysql DB. the following code has copied from below link. but it does not work. I have searched a lot and found some same results. but none of them works. I have no idea how can I do that.

JPA, Entity manager, select many columns and get result list custom objects

my code:

try{
EntityManagerFactory factory = Persistence.createEntityManagerFactory("com.mycompany_LinkedIn_war_1.0-SNAPSHOTPU");
EntityManager em = factory.createEntityManager();
TypedQuery<KarjooSearchInfo[]> q = em.createQuery(" SELECT name,education,gender,more,time FROM karjoo_search_info WHERE 1 ", KarjooSearchInfo[].class);
List<KarjooSearchInfo[]> resultList = q.getResultList();
do something...
}catch....

the exception is:

An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing [ SELECT name,education,gender,more,time FROM `karjoo_search_info` WHERE 1 ]. 
 [69, 69] An identification variable must be provided for a range variable declaration.
 [75, 76] The expression is not a valid conditional expression.
Community
  • 1
  • 1
Meysam Valueian
  • 661
  • 1
  • 5
  • 21

2 Answers2

1

Keep in mind that your query is actually in JPQL, not SQL, so you are searching against managed classes -- not tables.

I think you may want:

TypedQuery<KarjooSearchInfo[]> q = em.createQuery("SELECT K FROM KarjooSearchInfo K", KarjooSearchInfo.class);
Tony Pierce
  • 788
  • 1
  • 6
  • 11
0

I suggest you the following changes:

  • Trim off the leading blanks from the SQL.
  • The class for the return objects should be KarjooSearchInfo.class instead of KarjooSearchInfo[].class.
  • The condition should be WHERE true instead of WHERE 1.
Little Santi
  • 8,563
  • 2
  • 18
  • 46