These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Mapping UUID fields to java.util.UUID
PostPosted: Mon Nov 02, 2009 1:51 pm 
Newbie

Joined: Mon Nov 02, 2009 10:35 am
Posts: 1
I have a simple example working with postgres 8.4 & Hibernate 3 (driven by integration with a third party application). I can read limited records (not including uuid fields), but when I add the mapping for the uuid field (Id below), my code generates the following exception:

org.hibernate.type.SerializationException: could not deserialize
...
Caused by: java.io.StreamCorruptedException: invalid stream header
...

The records read OK, but they do not serialize (the exception is thrown in the records.add(iter.next()) line of code near the bottom below). Does anyone know what I may be missing in order to serialize the postgres uuid fields into Java UUID objects?


Component.java:
===================================================
package com.test.persist;
import java.util.UUID;
public class Component { // extends PersistentMessage {
private UUID Id;
private String Name;
private boolean Connect;
private boolean Connected;

public Component() {
}
public boolean Equals(Object object) {
if (object == null || !(object instanceof Component))
return false;
Component rhs = (Component) object;
return this.hashCode() == rhs.hashCode();
}
public int hashCode() {
return this.Id.hashCode();
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public boolean isConnect() {
return Connect;
}
public void setConnect(boolean connect) {
Connect = connect;
}
public boolean isConnected() {
return Connected;
}
public void setConnected(boolean connected) {
Connected = connected;
}
public UUID getId() {
return Id;
}
public void setId(UUID id) {
Id = id;
}
}

hibernate.cfg.xml:
===================================================
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost/nabdb/common</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">Password12345!</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="component.hbm.xml"/>
</session-factory>
</hibernate-configuration>

component.hbm.xml:
===================================================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<typedef name="uuid"
class="java.util.UUID">
</typedef>
<class name="com.test.persist.Component" table="Component" lazy="false">
<id name="Name" type="string" column="Name">
<generator class="native"/>
</id>
<property name="Connect" type="boolean"/>
<property name="Connected" type="boolean"/>
</class>
</hibernate-mapping>


DAO code performing the read ("Component" is passed in as the String):
==========================
public List findAll(Session session, String type) {
String SQL_QUERY = "from " + type;
session.beginTransaction();
Query query = session.createQuery(SQL_QUERY);
Iterator iter = query.iterate();
List records = new ArrayList();
while (iter.hasNext()) {
records.add(iter.next());
}
session.getTransaction().commit();
return records;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.