Query in Return i Get a Java.util.Vector Containing the Count

  • View
    377

  • Download
    2

Embed Size (px)

DESCRIPTION

Query in Return i Get a Java.util.Vector Containing the Count

Transcript

Sun ForumsWelcome

Lo gin Wa tch Lis t Be gin ner s Qui ck Sta rt My For um s Fee dba ck FA Q Co de of Co ndu ct

Query.get SingleRes ult returns a Vector? 2010 Jan 11 03:53 Hallo

Top of Form

10903682Bottom of Form

Reply

I tested on glassfish 2.1 with oracle.toplink.essentials.PersistenceProvider. Running a test with the entitymanager i noticed that getSingleResult returns a vector containing only one element rather than an object that i could cast accordingly. Looking in the web and also taking a look at the spec all example cast the result directly rather than assuming a vector/list. Since the specification just wants Object as result type this is not wrong but does that really pass compatibility tests? regards

Top of Form

Top of Form Bottom of Form

Re: Query.getSingleResult returns a Vector? Posts:2.066 2010 Jan 11 08:14 (reply 1 of 8) (In reply to original post ) Registered: 26/01/08

jtahlborn

10903962

Reply Bottom of Form

Search ForumsD is c u s s io n o r u m F f o r u m s .s u n .c o m Search

what query are you running?

Top of Form

mark197 1

10903971

Re: Query.getSingleResult returns a Vector?Reply

Registered: 2/28/03

Bottom of Form

Select count(*) from In return i get a java.util.Vector containing the count.Query query = em.createNativeQuery("select count(*) from soc"); List v = (List) query.getSingleResult();

Edited by: mark1971 on Jan 11, 2010 8:21 AM Oracle Social Media Participation Policy | About Sun | About This Site | Newsletters | Contact Us | Employment How to Buy | Licensing | Terms of Use | jtahlborn Privacy | Trademarks Re: Query.getSingleResult returns a Vector? Posts:2.066 2010 Jan 11 09:15 (reply 3 of 8) (In reply to #2 ) Registered: 26/01/08 2009, Oracle Corporation and/or its affiliates

A Sun Developer Network SiteTop of Form

Unless otherwise licensed, code in all technical manuals herein (including articles, FAQs, samples) is provided under Bottom of this License.Form

Content Feeds

Cast Functions and Operators what a native query or spec which nailed down exactlyTable 11.13.shouldFunctionsthis situation. if use the 2-arg method, Cast return in though, it seems like jpa is supposed to return a single Name object.Description i'd try "em.createNativeQuery("the query", Integer.class)" or some similar number type.BINARY

oddly, i couldn't find anything in the jpa documentation

Cast a string to a binary string

CAST()

mark197 Re: Query.getSingleResult returns a Vector? 1 Cast a value09:27 (reply 4 of 8) (In reply to #3 ) as a certain Convert() 2010 Jan 11 Posts:62 type Registered: 2/28/03 BINARY

Cast a value as a certain type

Top of Form

Bottom of Form

The BINARY operator casts the string following it to a binary string. This is an easy way to force a column comparison to be done byte by byte rather than character by character. This causes the comparisonnailed case sensitivemy bookthe column is not defined as You are right it is not to be down. From even if and BINARY or BLOB. BINARY thought that trailing spaces to be significant. various examples i also causes an Object or Object[] wasmysql> SELECT 'a' = 'A'; accordingly but maybe this is returned and you then cast only -> 1for hibernate. Since i am bound to toplink in this true mysql> SELECT BINARY 'a' = 'A'; case -> 0 screwed. i am mysql> SELECT 'a' = 'a '; Regarding the additional paramaeter, this also fails, i tried -> 1 mysql> SELECT BINARY 'a' = 'a '; that before: Query query = em.createNativeQuery("select count(*) from soc",Integer.class);

results in[TopLink Warning]: 2010.01.11 06:24:23.070-UnitOfWork(12359283)--Exception [TOPLINK-6007] (Oracle TopLink Essentials - 2.1 (Build b60efcs (12/23/2008))):

ption Exception Description: Missing descriptor for [class java.lang.Integer]. Query: ReadAllQuery(java.lang.Integer) Exception in thread "main" Local Exception Stack: Exception [TOPLINK-6007] (Oracle TopLink -> 0 Essentials - 2.1 (Build b60e-fcs (12/23/2008))): In a comparison, BINARY affects the entire operation; it can be given before either oracle.toplink.essentials.exceptions.QueryExce operand with the same result. ption BINARY str is shorthand for CAST(str AS descriptor for Exception Description: Missing BINARY). Note [classsome contexts, if you cast an indexed column to BINARY, MySQL is not able that in java.lang.Integer]. Query: ReadAllQuery(java.lang.Integer) to use the index efficiently. at oracle.toplink.essentials.exceptions.QueryExce CAST(expr AS type) ption.descriptorIsMissing(QueryException.java: The CAST() function takes a value of one type and produce a value of another type, 419) similar to CONVERT(). See the description of CONVERT() for more information. .

CONVERT(expr,type), CONVERT(expr USING transcoding_name)

The CONVERT() and CAST() functions take a value of one type and produce a value of Top of Form another type. jtahlborntype can be one of the following values: The Re: Query.getSingleResult returns a Vector? BINARY[(N)] Posts:2.066 2010 Jan 11 10:57 (reply 5 of 8) (In reply to #4 ) CHAR[(N)] Registered: Bottom of Form 26/01/08 DATE DATETIME DECIMAL[(M[,D])] mark1971 wrote: SIGNED [INTEGER] You are right it is not nailed down. From my book and various examples i thought that an Object or Object[] was TIME returned and you then cast accordingly but maybe this is UNSIGNED [INTEGER] only true for hibernate. BINARY produces a string with the BINARY data type. See Section 10.4.2, The BINARY and VARBINARY Types for a down for jpa query this affects comparisons. If the optional i think that is all nailed description of how language length N is given, BINARY(N) causes the cast to use no more than N bytes of the argument. queries, just not for native queries. As of MySQL 5.0.17, values shorter than N bytes are padded with 0x00 bytes to a length of N. speaking of which are you querying a table which is mapped to the cast to use no more than N characters of the CHAR(N) causes an entity? if so, you could do this query using argument. the jpa query language, and then i think the result The DECIMAL type is available as of MySQL 5.0.8. would/should be a single value. CAST() and CONVERT(... USING ...) are standard SQL syntax. The non-USING form of CONVERT() is ODBC syntax.Top of Form with USING is used to convert data between different character sets. In MySQL, transcoding names are the same as the corresponding character set names. For mark197 example, this statement converts the string a Vector? default character set to the Re: Query.getSingleResult returns 'abc' in the 1 corresponding string in the utf86character set: to #5 ) 2010 Jan 12 01:02 (reply of 8) (In reply Posts:62 SELECT CONVERT('abc' USING utf8); Registered: Bottom of Normally, you cannot compare a BLOB value or other binary string in case-insensitive fashion 2/28/03 Form because binary strings have no character set, and thus no concept of lettercase. To perform a case-insensitive comparison, use the CONVERT() function to convert the value to a nonbinary

CONVERT()

No, the entity is not mapped to an entity but yes it returns long if i do this@NamedQuery(name="count", query="select count(s) from Soc s")

oracle.toplink.essentials.Persi string. If the stenceProvider case-insensitive collation, the LIKE operation is not character set of the result has a case sensitive: jdbc/myDataSource LIKE CONVERT(blob_col USING latin1) FROM tbl_name; Soc To use a different character set, substitute its name for latin1 in the preceding statement. To ensure that a case-insensitive collation is used, specify a COLLATE clause following the CONVERT() call. and CONVERT() can be used = em.createNamedQuery("count"); Query q2 more generally for comparing strings that are represented in different character sets. Object o = q2.getSingleResult(); System.err.println(o.getClass().getName()); LOWER() (and UPPER()) are ineffective when applied to binary strings (BINARY, VARBINARY, BLOB).

that

To perform lettercase conversion, convert the string to a nonbinary string:

mysql> SET @str = BINARY 'New York'; But adding an entity just for a count seems a bit too mysql> SELECT LOWER(@str), LOWER(CONVERT(@str USING latin1)); much. +-------------+-----------------------------------+ | LOWER(@str) | LOWER(CONVERT(@str USING latin1)) | +-------------+-----------------------------------+ | New York | new york | Top of Form +-------------+-----------------------------------+

jtahlborn The cast functions are useful when you want to create a column with a specific type in a CREATE Re: Query.getSingleResult returns a Vector? TABLE ... SELECT statement: Posts:2.066 2010 Jan 12 07:55 (reply 7 of 8) (In reply to #6 )CREATE TABLE new_table SELECT CAST('2000-01-01' AS DATE);

Registered: Bottom of The functions also can be useful for sorting ENUM columns in lexical order. Normally, sorting of Form 26/01/08 ENUM columns occurs using the internal numeric values. Casting the values to CHAR results in a lexical sort:SELECT enum_col FROM tbl_name ORDER BY CAST(enum_col AS CHAR); CAST(str AS BINARY)wrote: same thing mark1971 is the

as BINARY str. CAST(expr AS CHAR) treats the expression as a string with the default for a count seems a bit too But adding an entity just character set. much. CAST() also changes the result if you use it as part of a more complex expression such as CONCAT('Date: ',CAST(NOW() AS DATE)). certainly. i was just saying that if it was already an entity, You should not use CAST() to query language would work better. instead use string functions that using the jpa extract data in different formats but like LEFT() or EXTRACT(). See Section 11.7, Date and Time Functions. To cast a string to a