|
|
|
Beyer,Nathan
|
I'm trying to write a bit of code that's portable between JPA providers and ran into a few differences and I'd like to know if they are undefined bits of the spec or just me doing something silly.
1. 0 is considered null for primary key fields (@Id) of type int, long - OpenJPA DOES NOT do this, but other providers, such as EclipseLink do. Is this in the spec or up for debate? 2. Return value of native queries is an Object[] or a List - Consider the following snippets // LOG is table name with a 'sequence' column/field of Java type 'long' final Query query = entityManager.createNativeQuery("SELECT MIN(e.sequence), MAX(e.sequence) FROM LOG e"); final Object result = query.getSingleResult(); With OpenJPA a Object[] is returned, in EclipseLink a List is returned. Is this defined anywhere? How can I define it. Thanks, -Nathan ---------------------------------------------------------------------- CONFIDENTIALITY NOTICE This message and any included attachments are from Cerner Corporation and are intended only for the addressee. The information contained in this message is confidential and may constitute inside or non-public information under international, federal, or state securities laws. Unauthorized forwarding, printing, copying, distribution, or use of such information is strictly prohibited and may be unlawful. If you are not the addressee, please promptly delete this message and notify the sender of the delivery error by e-mail or you may call Cerner's corporate offices in Kansas City, Missouri, U.S.A at (+1) (816)221-1024. |
||||||||||||||||
|
Kevin Sutter
|
Nathan,
Comments embedded below. This is just my interpretation of the spec (I am a member of the JPA expert group, but I am not the lead). Posting to the EclipseLink forum might be interesting as well... On Mon, Jul 21, 2008 at 10:15 AM, Beyer,Nathan <[hidden email]> wrote: > I'm trying to write a bit of code that's portable between JPA providers and > ran into a few differences and I'd like to know if they are undefined bits > of the spec or just me doing something silly. > > 1. 0 is considered null for primary key fields (@Id) of type int, long > - OpenJPA DOES NOT do this, but other providers, such as EclipseLink do. Is > this in the spec or up for debate? The spec does not identify valid values for a given annotation or field type. But, logic would say that 0 is a valid int or long and, thus, should be allowed as a primary key value. Of course, you could say that this wouldn't be a very solid value for a primary key, but it should be allowed in my book. > > 2. Return value of native queries is an Object[] or a List - Consider > the following snippets > > // LOG is table name with a 'sequence' column/field of Java > type 'long' > final Query query = entityManager.createNativeQuery("SELECT > MIN(e.sequence), MAX(e.sequence) FROM LOG e"); > final Object result = query.getSingleResult(); > > With OpenJPA a Object[] is returned, in EclipseLink a List > is returned. Is this defined anywhere? How can I define it. I'm a little confused by your example. You indicate that OpenJPA is returning an array of Objects? Did you mean that OpenJPA is returning a single Object? The JavaDoc for getSingleResult is like this: /** * Execute a SELECT query that returns a single result. * @return the result * @throws NoResultException if there is no result * @throws NonUniqueResultException if more than one result * @throws IllegalStateException if called for a Java * Persistence query language UPDATE or DELETE statement */ public Object getSingleResult(); So, I could see where someone might return a List (since it's an Object), but returning an array of Objects seems like a stretch. Given that, I would expect the result to be a single Object (not a List). If I wanted to process a List, then I would call getResultList. The example usage in the spec also supports this interpretation. Good luck with your comparisons, Kevin > > > Thanks, > -Nathan > > ---------------------------------------------------------------------- > CONFIDENTIALITY NOTICE This message and any included attachments are from > Cerner Corporation and are intended only for the addressee. The information > contained in this message is confidential and may constitute inside or > non-public information under international, federal, or state securities > laws. Unauthorized forwarding, printing, copying, distribution, or use of > such information is strictly prohibited and may be unlawful. If you are not > the addressee, please promptly delete this message and notify the sender of > the delivery error by e-mail or you may call Cerner's corporate offices in > Kansas City, Missouri, U.S.A at (+1) (816)221-1024. |
||||||||||||||||
|
Beyer,Nathan
|
Re: the question about the return type of the native query
To clarify, this is what I see in OpenJPA ... // LOG is table name with a 'sequence' column/field of Java type 'long' final Query query = entityManager.createNativeQuery("SELECT MIN(e.sequence), MAX(e.sequence) FROM LOG e"); If you use 'getResultList', then a List is return with one element, which is an Object[], so to process, you do this. final List result = query.getResultList(); final Object[] e0 = (Object[])result.get(0); final Number minSeq = (Number)e0[0]; final Number maxSeq = (Number)e0[1]; If you use 'getSingleResult', then just the Object[] is returned, so to process, you do this. final Object result = query.getSingleResult(); final Object[] e0 = (Object[])result; final Number minSeq = (Number)e0[0]; final Number maxSeq = (Number)e0[1]; -----Original Message----- From: Kevin Sutter [mailto:[hidden email]] Sent: Monday, July 21, 2008 12:01 PM To: [hidden email] Subject: Re: what to expect - jpa spec or impl discretion Nathan, Comments embedded below. This is just my interpretation of the spec (I am a member of the JPA expert group, but I am not the lead). Posting to the EclipseLink forum might be interesting as well... On Mon, Jul 21, 2008 at 10:15 AM, Beyer,Nathan <[hidden email]> wrote: > I'm trying to write a bit of code that's portable between JPA providers and > ran into a few differences and I'd like to know if they are undefined bits > of the spec or just me doing something silly. > > 1. 0 is considered null for primary key fields (@Id) of type int, long > - OpenJPA DOES NOT do this, but other providers, such as EclipseLink do. Is > this in the spec or up for debate? The spec does not identify valid values for a given annotation or field type. But, logic would say that 0 is a valid int or long and, thus, should be allowed as a primary key value. Of course, you could say that this wouldn't be a very solid value for a primary key, but it should be allowed in my book. > > 2. Return value of native queries is an Object[] or a List - Consider > the following snippets > > // LOG is table name with a 'sequence' column/field of Java > type 'long' > final Query query = entityManager.createNativeQuery("SELECT > MIN(e.sequence), MAX(e.sequence) FROM LOG e"); > final Object result = query.getSingleResult(); > > With OpenJPA a Object[] is returned, in EclipseLink a List > is returned. Is this defined anywhere? How can I define it. I'm a little confused by your example. You indicate that OpenJPA is returning an array of Objects? Did you mean that OpenJPA is returning a single Object? The JavaDoc for getSingleResult is like this: /** * Execute a SELECT query that returns a single result. * @return the result * @throws NoResultException if there is no result * @throws NonUniqueResultException if more than one result * @throws IllegalStateException if called for a Java * Persistence query language UPDATE or DELETE statement */ public Object getSingleResult(); So, I could see where someone might return a List (since it's an Object), but returning an array of Objects seems like a stretch. Given that, I would expect the result to be a single Object (not a List). If I wanted to process a List, then I would call getResultList. The example usage in the spec also supports this interpretation. Good luck with your comparisons, Kevin > > > Thanks, > -Nathan > > ---------------------------------------------------------------------- > CONFIDENTIALITY NOTICE This message and any included attachments are from > Cerner Corporation and are intended only for the addressee. The information > contained in this message is confidential and may constitute inside or > non-public information under international, federal, or state securities > laws. Unauthorized forwarding, printing, copying, distribution, or use of > such information is strictly prohibited and may be unlawful. If you are not > the addressee, please promptly delete this message and notify the sender of > the delivery error by e-mail or you may call Cerner's corporate offices in > Kansas City, Missouri, U.S.A at (+1) (816)221-1024. |
||||||||||||||||
|
Pinaki Poddar
|
Hi,
> I'm a little confused by your example. You indicate that OpenJPA is > returning an array of Objects? Did you mean that OpenJPA is returning a > single Object? For results of native queries, OpenJPA returns a List. But each member of the list can be Object[] depending upon what has been selected by the query and @SQLResultSetMapping. For example, a SQL SELECT p.name, p.age FROM PERSON p WHERE p.age > 20 may select 42 rows. In that case, OpenJPA will return a List of 42 elements where each element is a Object[] of length 2 with 0-th element being p.name and 1-st element is p.age.
Pinaki
|
||||
|
Pinaki Poddar
|
In reply to this post
by Beyer,Nathan
Hi,
OpenJPA does assign special meaning to '0' value for primary key. For example, one such special meaning is when an instance X is being merged and does not have a version field, if primary key value of X is 0, then X is assumed to be new, otherwise X is considered as an update.
Pinaki
|
||||||||||||||||
|
Craig L Russell
|
In reply to this post
by Pinaki Poddar
The spec says in 3.6.1
"TheelementsoftheresultofaJavaPersistencequerywhoseSELECTclauseconsistsofmorethanone selectexpressionareoftypeObject[]" with apologies to those who prefer spaces in sentences. So the result of a SELECT MIN(e.sequence), MAX(e.sequence) should be an Object[] in which the first element is the MIN and the second element the MAX. If you use getResultList, then the result will be a List each element of which is an Object[2]. If you call getSingleResult, the result will be a single Object[2]. Craig On Jul 21, 2008, at 10:45 AM, Pinaki Poddar wrote: > > Hi, >> I'm a little confused by your example. You indicate that OpenJPA is >> returning an array of Objects? Did you mean that OpenJPA is >> returning a >> single Object? > > For results of native queries, OpenJPA returns a List. But each > member of > the list can be Object[] depending upon what has been selected by > the query > and @SQLResultSetMapping. > For example, a SQL > SELECT p.name, p.age FROM PERSON p WHERE p.age > 20 > > may select 42 rows. In that case, OpenJPA will return a List of 42 > elements where each element is a Object[] of length 2 with 0-th > element > being p.name and 1-st element is p.age. > > > > > Beyer,Nathan wrote: >> >> Re: the question about the return type of the native query >> >> To clarify, this is what I see in OpenJPA ... >> >> // LOG is table name with a 'sequence' column/field of Java type >> 'long' >> final Query query = entityManager.createNativeQuery("SELECT >> MIN(e.sequence), MAX(e.sequence) FROM LOG e"); >> >> If you use 'getResultList', then a List is return with one element, >> which >> is an Object[], so to process, you do this. >> final List result = query.getResultList(); >> final Object[] e0 = (Object[])result.get(0); >> final Number minSeq = (Number)e0[0]; >> final Number maxSeq = (Number)e0[1]; >> >> >> If you use 'getSingleResult', then just the Object[] is returned, >> so to >> process, you do this. >> final Object result = query.getSingleResult(); >> final Object[] e0 = (Object[])result; >> final Number minSeq = (Number)e0[0]; >> final Number maxSeq = (Number)e0[1]; >> >> >> >> -----Original Message----- >> From: Kevin Sutter [mailto:[hidden email]] >> Sent: Monday, July 21, 2008 12:01 PM >> To: [hidden email] >> Subject: Re: what to expect - jpa spec or impl discretion >> >> Nathan, >> Comments embedded below. This is just my interpretation of the >> spec (I am >> a >> member of the JPA expert group, but I am not the lead). Posting to >> the >> EclipseLink forum might be interesting as well... >> >> On Mon, Jul 21, 2008 at 10:15 AM, Beyer,Nathan <[hidden email]> >> wrote: >> >>> I'm trying to write a bit of code that's portable between JPA >>> providers >>> and >>> ran into a few differences and I'd like to know if they are >>> undefined >>> bits >>> of the spec or just me doing something silly. >>> >>> 1. 0 is considered null for primary key fields (@Id) of type >>> int, >>> long >>> - OpenJPA DOES NOT do this, but other providers, such as >>> EclipseLink do. >>> Is >>> this in the spec or up for debate? >> >> >> The spec does not identify valid values for a given annotation or >> field >> type. But, logic would say that 0 is a valid int or long and, thus, >> should >> be allowed as a primary key value. Of course, you could say that >> this >> wouldn't be a very solid value for a primary key, but it should be >> allowed >> in my book. >> >> >>> >>> 2. Return value of native queries is an Object[] or a List - >>> Consider >>> the following snippets >>> >>> // LOG is table name with a 'sequence' column/field >>> of >>> Java >>> type 'long' >>> final Query query = entityManager.createNativeQuery("SELECT >>> MIN(e.sequence), MAX(e.sequence) FROM LOG e"); >>> final Object result = query.getSingleResult(); >>> >>> With OpenJPA a Object[] is returned, in EclipseLink >>> a List >>> is returned. Is this defined anywhere? How can I define it. >> >> >> I'm a little confused by your example. You indicate that OpenJPA is >> returning an array of Objects? Did you mean that OpenJPA is >> returning a >> single Object? The JavaDoc for getSingleResult is like this: >> >> /** >> * Execute a SELECT query that returns a single result. >> * @return the result >> * @throws NoResultException if there is no result >> * @throws NonUniqueResultException if more than one result >> * @throws IllegalStateException if called for a Java >> * Persistence query language UPDATE or DELETE statement >> */ >> public Object getSingleResult(); >> >> So, I could see where someone might return a List (since it's an >> Object), >> but returning an array of Objects seems like a stretch. >> >> Given that, I would expect the result to be a single Object (not a >> List). >> If I wanted to process a List, then I would call getResultList. The >> example >> usage in the spec also supports this interpretation. >> >> Good luck with your comparisons, >> Kevin >> >> >>> >>> >>> Thanks, >>> -Nathan >>> >>> ---------------------------------------------------------------------- >>> CONFIDENTIALITY NOTICE This message and any included attachments >>> are from >>> Cerner Corporation and are intended only for the addressee. The >>> information >>> contained in this message is confidential and may constitute >>> inside or >>> non-public information under international, federal, or state >>> securities >>> laws. Unauthorized forwarding, printing, copying, distribution, or >>> use of >>> such information is strictly prohibited and may be unlawful. If >>> you are >>> not >>> the addressee, please promptly delete this message and notify the >>> sender >>> of >>> the delivery error by e-mail or you may call Cerner's corporate >>> offices >>> in >>> Kansas City, Missouri, U.S.A at (+1) (816)221-1024. >> >> > > -- > View this message in context: http://n2.nabble.com/what-to-expect---jpa-spec-or-impl-discretion-tp575285p575455.html > Sent from the OpenJPA Users mailing list archive at Nabble.com. > Architect, Sun Java Enterprise System http://java.sun.com/products/jdo 408 276-5638 mailto:[hidden email] P.S. A good JDO? O, Gasp! |
||||||||||||||||
| Free Embeddable Forum Powered by Nabble | Help |