This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Native Queries

 
Nikhil Jain
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Query createNativeQuery(String sql, Class entityClass)

Does this only work for query returning all values of entity class. For example (select * from employee, employee.class).

I tried this using select name, age from employee, employee.class. This did not work.

I tried the above by using @ResultSetMapping.
@SqlResultSetMapping(name="myquery", entities={@EntityResult(entityClass=Employee.class, fields={@FieldResult(name="name", column="name")})})

Query query = em.createNativeQuery("SELECT name FROM EMPLOYEE", "myquery");
List result = query.getResultList();
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this only work for query returning all values of entity class. For example (select * from employee, employee.class).

No, you can select any field of the entity. Note that you don't especially need a FieldResult here. What error did you get ?
[ June 25, 2008: Message edited by: Christophe Verre ]
 
Nikhil Jain
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@SqlResultSetMapping(name="myquery", entities={@EntityResult(entityClass=Customer.class, fields={@FieldResult(name="name", column="name")})})

Caused by: java.sql.SQLException: Column 'customerid129_0_' not found.

This works well If I get dont't use @FieldResut & e select * from customer.

I am trying to execute
Query query = em.createNativeQuery("select c.name from customer c", "myquery");

1. I wanted to use @resultset mapping such that I could get customer object & I could use customer.getName().

Am I going in the right direction?
 
Nikhil Jain
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Likewise if I use

Query query = em.createNativeQuery("select c.customerId, c.name, c.age from customer c", Customer.class);

this works

but
Query query = em.createNativeQuery("select c.name from customer c", Customer.class);
this does'nt work.
Error: Caused by: java.sql.SQLException: Column 'customerid' not found.

It seems that these only work for queries when it fetches all the columns?
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubt: have you got a case-sensitive database?
I had the same problem with mysql.
 
Nikhil Jain
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think thats the problem with me. Else other queries would'nt have worked?
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anu Tilwalli:
Likewise if I use

Query query = em.createNativeQuery("select c.customerId, c.name, c.age from customer c", Customer.class);

this works

but
Query query = em.createNativeQuery("select c.name from customer c", Customer.class);
this does'nt work.
Error: Caused by: java.sql.SQLException: Column 'customerid' not found.

It seems that these only work for queries when it fetches all the columns?


Can you post the complete code (specially the @ResultSetMapping annotation) and the table definition?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic