Skip to main content

Where are the SQL statements generated of the NamedQueries stored?

6 replies [Last post]
zebhed
Offline
Joined: 2007-11-03
Points: 0

I´ve searched this forum and the Toplink Essentials forum but I have not found the answer yet ...

I assume that NamedQueries will be translated to SQL statements.
Is there a way to look at these generated SQL statements.
Where are they stored? How can I take a look at them?

I use:
glassfish v2
netbeans 6.0.1
mysql 5.0

Message was edited by: zebhed

Message was edited by: zebhed

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
zebhed
Offline
Joined: 2007-11-03
Points: 0

this solution seems to be outdated now.

since i have updated glassfish to the latest official release by sun (Sun Java System Application Server 9.1_02 (build b04-fcs)) the code provided by gyorke does not work anymore.

I get the following exception:
"java.lang.ClassCastException: com.sun.enterprise.util.QueryWrapper cannot be cast to oracle.toplink.essentials.ejb.cmp3.EJBQuery"

how do I have write the code now to take a look at the native sql queries?

mm110999
Offline
Joined: 2005-03-29
Points: 0

One option is to get hold of Toplink em. That is as follows

EntityManager toplinkEm = em.getDelegate();
((oracle.toplink.essentials.ejb.cmp3.EJBQuery)[b]toplinkEm[/b].createNamedQuery("name")).getDatabaseQuery();
dbQuery.prepareCall(((EntityManagerImpl)em.getDelegate ()).getServerSession(), new DatabaseRecord());

zebhed
Offline
Joined: 2007-11-03
Points: 0

that made the trick. thanks.

edwardrayl
Offline
Joined: 2003-06-13
Points: 0

> DatabaseQuery dbQuery =
> ((oracle.toplink.essentials.ejb.cmp3.EJBQuery)em.creat
> eNamedQuery("name")).getDatabaseQuery();
> dbQuery.prepareCall(((EntityManagerImpl)em.getDelegate
> ()).getServerSession(), new DatabaseRecord());

This works fine when using em.createNamedQuery(), but gives a IllegalArgumentException when using em.createQuery(). Is there any way to get the database query for an arbitrary JPQL string within your code?

gyorke
Offline
Joined: 2005-06-21
Points: 0

TopLink generates the SQL dynamically during runtime. Although TopLink can cache the statements they are not really 'stored' anywhere. The easiest way to view the SQL is to turn on logging using the "toplink.logging.level" "finer" persistence unit properties. To get the SQL within your application you can call.
[code]
DatabaseQuery dbQuery = ((oracle.toplink.essentials.ejb.cmp3.EJBQuery)em.createNamedQuery("name")).getDatabaseQuery();
dbQuery.prepareCall(((EntityManagerImpl)em.getDelegate()).getServerSession(), new DatabaseRecord());
dbQuery.getSQLString();
[/code]
--Gordon

zebhed
Offline
Joined: 2007-11-03
Points: 0

thanks, it works.