Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Native Query issues
Native Query issues [message #385295] Sat, 24 January 2009 04:13 Go to next message
Ari Meyer is currently offline Ari Meyer
Messages: 136
Registered: July 2009
Senior Member
Hi,

I'm using EclipseLink 1.0.2, and I need to hit an Oracle 10.2 sequence
in ad hoc fashion (NOT when inserting a record), so I created a native
query like so (snippet):

final String query =
"SELECT proj_num_seq.nextval " +
"FROM dual ";

return (Long) _entityManager
.createNativeQuery(query)
.getSingleResult();

When this was executed, I got this bizarre error:

java.lang.ClassCastException:
org.eclipse.persistence.internal.helper.NonSynchronizedVecto r

So I see that it's returning a List, not the "single result" I
requested. Bug?


I then tried this:

List aList = (List) _entityManager
.createNativeQuery(query)
.getSingleResult();

return (Long) aList.get(0);

.... which resulted in:

java.lang.ClassCastException: java.math.BigDecimal


Finally, I ended up having to do this:

List aList = (List) _entityManager
.createNativeQuery(query)
.getSingleResult();

return ((BigDecimal) aList.get(0)).longValue();

or this with generics:

List<BigDecimal> aList = (List<BigDecimal>)
_entityManager.createNativeQuery(query)
.getSingleResult();

return aList.get(0).longValue();



All good now!

But the first attempt should have worked correctly, right? It should
not have returned a List holding a BigDecimal (maybe just a BigDecimal
for me to deal with, which at least would have been somewhat
acceptable). Also, just for fun, when I tried it with getResultList()
instead of getSingleResult(), it wrapped the List in another List, which
is what I expected.

So is createNativeQuery(String).getSingleResult() misbehaving, or am I
doing something wrong? I didn't find any similar bug reports or news
postings.

Thanks,
Ari
Re: Native Query issues [message #385296 is a reply to message #385295] Sun, 25 January 2009 05:19 Go to previous messageGo to next message
Doug Clarke is currently offline Doug Clarke
Messages: 155
Registered: July 2009
Senior Member
This has been fixed since 1.0.2.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=229627

You should now get either a Object or Object[] for a getSingleResult()
query execution. If you are querying for multiple rows the result will be
either List<Object> or List<Object[]>.

When issuing a native query the type returned is really database and JDBC
specific and could even vary between JDBC versions. When returning a
single number like this I tend to write my query execution like:

return ((Number) _entityManager
createNativeQuery(query)
getSingleResult()).longValue();

Doug
Re: Native Query issues [message #385297 is a reply to message #385296] Sun, 25 January 2009 18:25 Go to previous messageGo to next message
Ari Meyer is currently offline Ari Meyer
Messages: 136
Registered: July 2009
Senior Member
Thanks Doug -- when are they planning to release a 1.0.3 (or whatever
new version)?
Ari

Doug Clarke wrote:
> This has been fixed since 1.0.2.
>
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=229627
>
> You should now get either a Object or Object[] for a getSingleResult()
> query execution. If you are querying for multiple rows the result will
> be either List<Object> or List<Object[]>.
>
> When issuing a native query the type returned is really database and
> JDBC specific and could even vary between JDBC versions. When returning
> a single number like this I tend to write my query execution like:
>
> return ((Number) _entityManager
> createNativeQuery(query)
> getSingleResult()).longValue();
>
> Doug
>
Re: Native Query issues [message #385298 is a reply to message #385297] Sun, 25 January 2009 21:59 Go to previous messageGo to next message
Doug Clarke is currently offline Doug Clarke
Messages: 155
Registered: July 2009
Senior Member
We are working towards a 1.1 release in February. Our original date of Feb
4th will not work out we just got the final reviews completed on our IP
Log and will schedule the release review this week.

http://wiki.eclipse.org/EclipseLink/Development/RoadMap

Doug
Re: Native Query issues [message #385304 is a reply to message #385298] Tue, 27 January 2009 04:19 Go to previous message
Ari Meyer is currently offline Ari Meyer
Messages: 136
Registered: July 2009
Senior Member
Thanks Doug -- looking forward to it!

Doug Clarke wrote:
> We are working towards a 1.1 release in February. Our original date of
> Feb 4th will not work out we just got the final reviews completed on our
> IP Log and will schedule the release review this week.
> http://wiki.eclipse.org/EclipseLink/Development/RoadMap
>
> Doug
>
Previous Topic:Tree structure - avoid select on parent_id
Next Topic:JPQL and partial attributes
Goto Forum:
  


Current Time: Wed Jul 23 10:04:12 EDT 2014

Powered by FUDForum. Page generated in 0.01599 seconds