Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Does JDBI accept UUID parameters?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Fernando Correia  
View profile  
 More options Aug 19 2012, 10:53 am
From: Fernando Correia <fernandoacorr...@gmail.com>
Date: Sun, 19 Aug 2012 07:53:55 -0700 (PDT)
Local: Sun, Aug 19 2012 10:53 am
Subject: Does JDBI accept UUID parameters?

As asked in StackOverflow (
http://stackoverflow.com/questions/12022452/does-jdbi-accept-uuid-par...
 ):

When using SQL Object argument binding, does JDBI work out-of-the-box with
UUID parameters?

I have a method such as this:

@SqlQuery("EXECUTE [MyProcedure] :myField")
MyDto myMethod(@Bind("myField") UUID myField);

which is bound to a SQL Server stored procedure that receives a parameter
like this:

@myField uniqueidentifier

When executed, this exception is thrown:

! com.microsoft.sqlserver.jdbc.SQLServerException: The conversion from
UNKNOWN to UNKNOWN is unsupported.
! at
com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServ erException.java:190)
! at
com.microsoft.sqlserver.jdbc.DataTypes.throwConversionError(DataTypes.java: 1117)
! at
com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setObject(SQLServer PreparedStatement.java:991)

If I change the parameter type on JDBI to String, and call it using the
toString() method from the UUID object, it works:

@SqlQuery("EXECUTE [MyProcedure] :myField")
MyDto trash(@Bind("myField") String myField);


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steve Nelson  
View profile  
 More options Aug 19 2012, 2:02 pm
From: Steve Nelson <snel...@iowaballetacademy.com>
Date: Sun, 19 Aug 2012 13:02:26 -0500
Local: Sun, Aug 19 2012 2:02 pm
Subject: Re: Does JDBI accept UUID parameters?

Are you using SQL server? I had quite a bit of trouble with this, I ended up just treating them as strings. But I am just passing them back and forth through REST services anyway so I didn't really need them to be UUIDs at that level. The SQL server driver seemed to handle them fine as strings.

On Aug 19, 2012, at 9:53 AM, Fernando Correia <fernandoacorr...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Fernando Correia  
View profile  
 More options Aug 19 2012, 3:01 pm
From: Fernando Correia <fernandoacorr...@gmail.com>
Date: Sun, 19 Aug 2012 16:01:20 -0300
Local: Sun, Aug 19 2012 3:01 pm
Subject: Re: Does JDBI accept UUID parameters?

Yes, I'm using SQL Server. Passing those parameters as string works. I'm
trying to find out if there's a way to pass them natively as UUIDs, whitout
converting to Strings.

You seem to have had the same issue I'm having.

2012/8/19 Steve Nelson <snel...@iowaballetacademy.com>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tatu Saloranta  
View profile   Translate to Translated (View Original)
 More options Aug 19 2012, 3:06 pm
From: Tatu Saloranta <tsalora...@gmail.com>
Date: Sun, 19 Aug 2012 12:06:38 -0700
Local: Sun, Aug 19 2012 3:06 pm
Subject: Re: Does JDBI accept UUID parameters?
On Sun, Aug 19, 2012 at 12:01 PM, Fernando Correia

<fernandoacorr...@gmail.com> wrote:
> Yes, I'm using SQL Server. Passing those parameters as string works. I'm
> trying to find out if there's a way to pass them natively as UUIDs, whitout
> converting to Strings.

At some point, conversion is needed, but all things considered it's
unlikely that this has measurable impact on performance (wrt
conversions).

In theory, support by DB could benefit iff there is native UUID type,
to benefit from its 128-bit size. I don't know which DBs have such
native type however.
But even in those cases, as long as table definition uses UUIDs,
conversions back and forth should be efficient -- there is no magic in
there, standard 36-char representation to/from UUID is easy.

-+ Tatu +-


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Fernando Correia  
View profile  
 More options Aug 19 2012, 4:50 pm
From: Fernando Correia <fernandoacorr...@gmail.com>
Date: Sun, 19 Aug 2012 17:50:48 -0300
Local: Sun, Aug 19 2012 4:50 pm
Subject: Re: Does JDBI accept UUID parameters?

I understand. I'm not concerned about performance; that would make a very
minor difference. But all my keys are UUIDs and right now my DAO interfaces
only accept strings:

TemplateDto selectSingle(@Bind("tenantId") String tenantId,
@Bind("templateId") String templateId);

That means I have to convert the values to strings when using these APIs,
which is a bit cumbersome and error-prone:

TemplateDto templateDto =
templatesDao.selectSingle(tenantId.get().toString(),
templateId.get().toString());

Is there a way to write a custom mapper on JDBI so I could write the
interface method accepting UUIDs, but have it convert them to string when
passing the parameter to the SqlQuery? Something like:

TemplateDto selectSingle(@Bind("tenantId")  UUID  tenantId,
@Bind("templateId") UUID templateId);

So the API would base based explicitly on the data types of the DTOs.

But again, without necessarily passing them along to the database as UUIDs,
just converting them to string before binding the parameters. I don't know
JDBI enough to understand if that's possible or how difficult it would be.

2012/8/19 Tatu Saloranta <tsalora...@gmail.com>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brian McCallister  
View profile  
 More options Aug 19 2012, 10:30 pm
From: Brian McCallister <bri...@skife.org>
Date: Sun, 19 Aug 2012 20:30:56 -0600
Local: Sun, Aug 19 2012 10:30 pm
Subject: Re: Does JDBI accept UUID parameters?

On Sun, Aug 19, 2012 at 2:50 PM, Fernando Correia <

Responded on SO with:

JDBI only exposes explicite type based bindings for the types which JDBC
exposes them for. JDBC does not expose a UUID type for binding, os it is
defaulting to setting it as an Object. Unfortunately, JDBC offers not
explicit UUID binding mechanism, so going through String is probably the
most portable way :-(

If you want to bind it as a UUID in Java and have it converted to a String
internally, there are two paths. The first, if you *always* want to bind
UUIDs as Strings is to use an ArgumentFactory, see
https://github.com/brianm/jdbi/blob/master/src/test/java/org/skife/jd...
for
an example.

The second is, if you want to do it only in specific cases, to create a
custom Binder, such as withhttp://jdbi.org/sql_object_api_argument_binding/

-Brian


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Fernando Correia  
View profile  
 More options Aug 20 2012, 7:12 pm
From: Fernando Correia <fernandoacorr...@gmail.com>
Date: Mon, 20 Aug 2012 16:12:23 -0700 (PDT)
Local: Mon, Aug 20 2012 7:12 pm
Subject: Re: Does JDBI accept UUID parameters?

Thank you very much, Briand! No more toString() when calling my DAO methods.

For the record, this is my implementation:

public class UUIDArgumentFactory implements ArgumentFactory<UUID> {

    @Override
    public boolean accepts(Class<?> expectedType, Object value,
StatementContext ctx) {
        return value instanceof UUID;
    }

    @Override
    public Argument build(Class<?> expectedType, UUID value,
StatementContext ctx) {
        return new UUIDArgument(value);
    }

}

public class UUIDArgument implements Argument {
    private UUID value = null;

    public UUIDArgument(UUID value) {
        this.value = value;
    }

    @Override
    public void apply(int position, PreparedStatement statement,
StatementContext ctx) throws SQLException {
        statement.setString(position, value.toString());
    }    

}

Registered as:

db.registerArgumentFactory(new UUIDArgumentFactory());

Tested and working with SQL Server.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »