Native Query issues [message #385295] |
Sat, 24 January 2009 09:13  |
Eclipse User |
|
|
|
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 10:19   |
Eclipse User |
|
|
|
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 23:25   |
Eclipse User |
|
|
|
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
>
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03580 seconds