• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Native Queries

 
Ranch Hand
Posts: 393
  • 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();
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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: 393
  • 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: 393
  • 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?
 
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: 393
  • 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?
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • 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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!