Using java.util.UUID with Hibernate

803 views
Skip to first unread message

jgrasper

unread,
Nov 19, 2009, 11:07:32 AM11/19/09
to H2 Database
I have been trying to use Hibernate and H2s capability of using
java.util.UUID. The problem is that no matter what I have the UUID
field of my object set to, the UUID ends up in the db as:

aced0005-7372-000e-6a61-76612e757469


Is this value hard coded somewhere? I get exceptions like this even
when I print out the UUID to the console on the Java Side and it is
different then the previous:

org.h2.jdbc.JdbcSQLException: Unique index or primary key violation:
PRIMARY_KEY_7 ON PUBLIC.MYOBJECT(UUID) [23001-106]
at org.h2.message.Message.getSQLException(Message.java:103)
at org.h2.message.Message.getSQLException(Message.java:114)
at org.h2.message.Message.getSQLException(Message.java:77)
at org.h2.index.BaseIndex.getDuplicateKeyException(BaseIndex.java:
160)
at org.h2.index.BtreeLeaf.add(BtreeLeaf.java:67)
at org.h2.index.BtreeIndex.add(BtreeIndex.java:223)
at org.h2.table.TableData.addRow(TableData.java:117)
at org.h2.command.dml.Insert.update(Insert.java:96)
at org.h2.command.CommandContainer.update(CommandContainer.java:71)
at org.h2.command.Command.executeUpdate(Command.java:207)
at org.h2.server.TcpServerThread.process(TcpServerThread.java:297)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:136)
at java.lang.Thread.run(Unknown Source)

jgrasper

unread,
Nov 19, 2009, 11:12:57 AM11/19/09
to H2 Database
Ok.. so I tried something else (Having the UUID not as a PK to see
what h2 would see)

On the Java Side:

(Generated UUIDs)
2ac27d72-ea0f-46ef-8c96-29e728d4aaa1
d760a7c7-854e-4ce3-a403-d7c9d0dd6b47
330f42e0-ff11-4c9e-bd35-f57fb8646ebe
cf470ef1-3f51-4e8d-856b-8a6b55f0729b
9280bb8c-33f6-43b6-aab0-05fdf182bc75
d3000866-72e2-4193-8199-4b06053f5d12
e7cc5565-4c7b-405e-95fb-7e0eb94f4aab
54c9ff15-57e4-4e1f-b788-5426cb9574b4
4a13dd3e-d407-4ca1-9819-6f66cda9506b
1298e12f-366f-47fd-aa24-61b6f4c73467

What H2 Sees:

ID UUID
0 aced0005-7372-000e-6a61-76612e757469
1 aced0005-7372-000e-6a61-76612e757469
2 aced0005-7372-000e-6a61-76612e757469
3 aced0005-7372-000e-6a61-76612e757469
4 aced0005-7372-000e-6a61-76612e757469
5 aced0005-7372-000e-6a61-76612e757469
6 aced0005-7372-000e-6a61-76612e757469
7 aced0005-7372-000e-6a61-76612e757469
8 aced0005-7372-000e-6a61-76612e757469
9 aced0005-7372-000e-6a61-76612e757469

Max Bridgewater

unread,
Nov 19, 2009, 11:20:42 AM11/19/09
to h2-da...@googlegroups.com
Out of curiosity, can you show you SQL statement for creating the table and for insertion?



--

You received this message because you are subscribed to the Google Groups "H2 Database" group.
To post to this group, send email to h2-da...@googlegroups.com.
To unsubscribe from this group, send email to h2-database...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/h2-database?hl=.



Message has been deleted
Message has been deleted

jgrasper

unread,
Nov 19, 2009, 1:26:30 PM11/19/09
to H2 Database
This is all Hibernate Generated:

create table MyObject (
id integer not null,
UUID uuid,
primary key (id)
)

insert
into
MyObject
(id, UUID)
values
(?, ?)

Here is what Hibernate is Binding:

commonObjects.MyObject{id=0, UUID=2ac27d72-
ea0f-46ef-8c96-29e728d4aaa1}

On Nov 19, 10:20 am, Max Bridgewater <max.bridgewa...@gmail.com>
wrote:
> Out of curiosity, can you show you SQL statement for creating the table and
> for insertion?
>
> > h2-database...@googlegroups.com<h2-database%2Bunsu...@googlegroups.com>
> > .

jgrasper

unread,
Nov 19, 2009, 2:06:21 PM11/19/09
to H2 Database
I figured it out... it was a problem between using Postgres and using
H2.

For Postgres I had to do this when setting a UUID via the setObject()
command:

st.setObject(index, value,1111);

for H2 I do this:

st.setObject(index, value);

Thomas Mueller

unread,
Nov 22, 2009, 4:35:09 AM11/22/09
to h2-da...@googlegroups.com
Hi,

If it works now, that's great. If there is still a problem: something
I noticed is that you are using version 1.1.106 (the version number is
encoded in the exception error code which is [...-106]). The class
java.util.UUID is supported since H2 version 1.1.115 (2009-06-21). I
suggest to try again with the latest stable version of H2, which is
1.1.119 (2009-09-26).

Regards,
Thomas
Reply all
Reply to author
Forward
0 new messages