[hibernate-commits] Hibernate SVN: r19647 - in core/trunk: core/src/main/java/org/hibernate/type and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Jun 1 15:16:51 EDT 2010


Author: steve.ebersole at jboss.com
Date: 2010-06-01 15:16:50 -0400 (Tue, 01 Jun 2010)
New Revision: 19647

Added:
   core/trunk/core/src/main/java/org/hibernate/type/PostgresUUIDType.java
Modified:
   core/trunk/core/src/main/java/org/hibernate/dialect/PostgreSQLDialect.java
   core/trunk/core/src/main/java/org/hibernate/type/BasicTypeRegistry.java
   core/trunk/documentation/manual/src/main/docbook/en-US/content/type.xml
Log:
HHH-3579 - Support for PostgreSQL UUID data type


Modified: core/trunk/core/src/main/java/org/hibernate/dialect/PostgreSQLDialect.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/dialect/PostgreSQLDialect.java	2010-06-01 18:34:12 UTC (rev 19646)
+++ core/trunk/core/src/main/java/org/hibernate/dialect/PostgreSQLDialect.java	2010-06-01 19:16:50 UTC (rev 19647)
@@ -45,7 +45,7 @@
 /**
  * An SQL dialect for Postgres
  * <p/>
- * For discussion of BLOB "support" in postrges, as of 8.4, have a peek at
+ * For discussion of BLOB support in Postgres, as of 8.4, have a peek at
  * <a href="http://jdbc.postgresql.org/documentation/84/binary-data.html">http://jdbc.postgresql.org/documentation/84/binary-data.html</a>.
  * For the effects in regards to Hibernate see <a href="http://in.relation.to/15492.lace">http://in.relation.to/15492.lace</a>
  *
@@ -73,6 +73,7 @@
 		registerColumnType( Types.CLOB, "text" );
 		registerColumnType( Types.BLOB, "oid" );
 		registerColumnType( Types.NUMERIC, "numeric($p, $s)" );
+		registerColumnType( Types.OTHER, "uuid" );
 
 		registerFunction( "abs", new StandardSQLFunction("abs") );
 		registerFunction( "sign", new StandardSQLFunction("sign", Hibernate.INTEGER) );

Modified: core/trunk/core/src/main/java/org/hibernate/type/BasicTypeRegistry.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/type/BasicTypeRegistry.java	2010-06-01 18:34:12 UTC (rev 19646)
+++ core/trunk/core/src/main/java/org/hibernate/type/BasicTypeRegistry.java	2010-06-01 19:16:50 UTC (rev 19647)
@@ -75,6 +75,7 @@
 		register( ClassType.INSTANCE );
 		register( UUIDBinaryType.INSTANCE );
 		register( UUIDCharType.INSTANCE );
+		register( PostgresUUIDType.INSTANCE );
 
 		register( BinaryType.INSTANCE );
 		register( WrapperBinaryType.INSTANCE );

Added: core/trunk/core/src/main/java/org/hibernate/type/PostgresUUIDType.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/type/PostgresUUIDType.java	                        (rev 0)
+++ core/trunk/core/src/main/java/org/hibernate/type/PostgresUUIDType.java	2010-06-01 19:16:50 UTC (rev 19647)
@@ -0,0 +1,85 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.type;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Types;
+import java.util.UUID;
+
+import org.hibernate.type.descriptor.ValueBinder;
+import org.hibernate.type.descriptor.ValueExtractor;
+import org.hibernate.type.descriptor.WrapperOptions;
+import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
+import org.hibernate.type.descriptor.java.UUIDTypeDescriptor;
+import org.hibernate.type.descriptor.sql.BasicBinder;
+import org.hibernate.type.descriptor.sql.BasicExtractor;
+import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
+
+/**
+ * Specialized type mapping for {@link UUID} and the Postgres UUID data type (which is mapped as OTHER in its
+ * JDBC driver).
+ *
+ * @author Steve Ebersole
+ * @author David Driscoll
+ */
+public class PostgresUUIDType extends AbstractSingleColumnStandardBasicType<UUID> {
+	public static final PostgresUUIDType INSTANCE = new PostgresUUIDType();
+
+	public PostgresUUIDType() {
+		super( PostgresUUIDSqlTypeDescriptor.INSTANCE, UUIDTypeDescriptor.INSTANCE );
+	}
+
+	public String getName() {
+		return "pg-uuid";
+	}
+
+	public static class PostgresUUIDSqlTypeDescriptor implements SqlTypeDescriptor {
+		public static final PostgresUUIDSqlTypeDescriptor INSTANCE = new PostgresUUIDSqlTypeDescriptor();
+
+		public int getSqlType() {
+			// ugh
+			return Types.OTHER;
+		}
+
+		public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
+			return new BasicBinder<X>( javaTypeDescriptor, this ) {
+				@Override
+				protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
+					st.setObject( index, javaTypeDescriptor.unwrap( value, UUID.class, options ) );
+				}
+			};
+		}
+
+		public <X> ValueExtractor<X> getExtractor(final JavaTypeDescriptor<X> javaTypeDescriptor) {
+			return new BasicExtractor<X>( javaTypeDescriptor, this ) {
+				@Override
+				protected X doExtract(ResultSet rs, String name, WrapperOptions options) throws SQLException {
+					return javaTypeDescriptor.wrap( rs.getObject( name ), options );
+				}
+			};
+		}
+	}
+}

Modified: core/trunk/documentation/manual/src/main/docbook/en-US/content/type.xml
===================================================================
--- core/trunk/documentation/manual/src/main/docbook/en-US/content/type.xml	2010-06-01 18:34:12 UTC (rev 19646)
+++ core/trunk/documentation/manual/src/main/docbook/en-US/content/type.xml	2010-06-01 19:16:50 UTC (rev 19647)
@@ -610,6 +610,19 @@
                             </para>
                         </listitem>
                     </varlistentry>
+                    <varlistentry>
+                        <term><classname>org.hibernate.type.PostgresUUIDType</classname></term>
+                        <listitem>
+                            <para>
+                                Maps a java.util.UUID to the PostgreSQL UUID data type (through
+                                <literal>Types#OTHER</literal> which is how the PostgreSQL JDBC driver defines it).
+                            </para>
+                            <para>
+                                Registered under <literal>pg-uuid</literal> in the type registry (see
+                                <xref linkend="types.registry"/>).
+                            </para>
+                        </listitem>
+                    </varlistentry>
                 </variablelist>
             </section>
             <section id="types.basic.value.serializable">
@@ -698,29 +711,17 @@
             The main categorization was between entity types and value types.  To review we said that entities, by
             nature of their unique identifier, exist independently of other objects whereas values do not.  An
             application cannot "delete" a Product sku; instead, the sku is removed when the Product itself is
-            deleted (obviously you can <emphasis>update</emphasis> the sku of that Product to null to maker it
+            deleted (obviously you can <emphasis>update</emphasis> the sku of that Product to null to make it
             "go away", but even there the access is done through the Product).
         </para>
         <para>
             Nor can you define an association <emphasis>to</emphasis> that Product sku.  You <emphasis>can</emphasis>
-            define an association to Product <emphasis>based on</emphasis> its sku, assuming sku is unique but that
+            define an association to Product <emphasis>based on</emphasis> its sku, assuming sku is unique, but that
             is totally different.
-
-
-            entity types define
-            data that maintains its own lifecycle, while value types define data that only exists dependently.  Essentially it defines it own unique identifier.  In turn that means
-            that it can be used to share references to that data from other entities (or components or collections).
-            This dependence/independence has a few important ramifications:
-            <itemizedlist>
-                <listitem>
-                    <para>
-                        First, entities can be looked up and referenced (as in foreign keys).  The same is not true of
-                        a value type.  For example, a <classname>Product</classname> entity can be looked up by its
-                        unique identifier.  The sku of that Product
-                    </para>
-                </listitem>
-            </itemizedlist>
         </para>
+        <para>
+            TBC...
+        </para>
     </section>
 
     <section id="types.custom">



More information about the hibernate-commits mailing list