aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Native Queries Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Native Queries" Watch "Native Queries" New topic
Author

Native Queries

Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
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();


SCJP 1.4, SCWCD 1.4, SCBCD 1.5
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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 ]

[My Blog]
All roads lead to JavaRanch
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
@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

Joined: May 15, 2005
Posts: 385
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

Joined: May 14, 2007
Posts: 244
I have a doubt: have you got a case-sensitive database?
I had the same problem with mysql.


SCJP<br />SCWCD 1.4 Upgrade (Remember: me stupid)<br />SCWCD 1.4<br /><a href="http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html" target="_blank" rel="nofollow">SCBCD 5.0</a><br /><a href="http://www.enthuware.com" target="_blank" rel="nofollow">SCBCD 5.0 mock exam</a> <br /> <br />SCEA 5 Part1: Preparing...
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
I don't think thats the problem with me. Else other queries would'nt have worked?
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

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?


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Native Queries