-->
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.  [ 4 posts ] 
Author Message
 Post subject: UserType for SQLData Types
PostPosted: Wed Sep 22, 2004 5:04 pm 
Newbie

Joined: Wed Sep 22, 2004 10:10 am
Posts: 15
Location: Darmstadt, Germany
Hibernate version:
2.1.6

Name and version of the database you are using:
Oracle 9i, JDBC Driver: classes12.zip

Hello everyone,

we have an Oracle 9i (9.2) DB which uses some UDTs, like the following:
Code:
create or replace type color as object (r number, g number,b number);

These are part of existing classes. Until now, these have been accessed using the SQLData interface. From searching through the Forum, I found several pointers to the UserType/CompositeUserType Interfaces, and tried to implement these. However, even after looking through the Reference, Hibernate in Action Chapter 6.1 and the APIdoc, I do not understand how to implement the two methods nullSafeGet() and nullsafeSet() in my case. Could anyone give me a hint what I have do do in these with my UDT?

The portion of the mapping file for the table thast has this color UDT looks like this:
Code:
    <property
        name="color"
        type="net.sf.hibernate.type.Color"
        column="COLOR">

        <meta attribute="field-description">
           @hibernate.property
            column="COLOR"
        </meta>   
    </property>


Unfortunately, I keep getting Exceptions when trying to instantiate a Session., so I assume there is a mistake in the UserType, and more specific, in my implementations of the aforementioned methods.

I am not very happy with these UDTs, however, changing the DB schema is, very unfortunately, not an option, since several legacy apps use it.

Thanks in Advance & regards,

Thorsten


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 22, 2004 5:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Just look at some other already existing UserTypes - there are various in the Wiki Area on the website, in the Hibernate Unit Tests, etc.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 23, 2004 11:18 am 
Newbie

Joined: Wed Sep 22, 2004 10:10 am
Posts: 15
Location: Darmstadt, Germany
Thanks for the quick reply!

OK, I had a look at the various UserTypes that have been placed in the WIKI, and according to the patterns in these, I wrote ("sketched") these methods:

Code:
public Object nullSafeGet(ResultSet resultSet, String[] names,
                              java.lang.Object owner) throws HibernateException,
        SQLException {

        if (resultSet.wasNull()) { return null; }
        else {
            Color color = new Color();
            SQLInput stream = (SQLInput) resultSet.getBinaryStream("COLOR");
            color.r = stream.readDouble();
            color.g = stream.readDouble();
            color.b = stream.readDouble();
            return color;
        }
}


and

Code:
public void nullSafeSet(PreparedStatement statement, Object value,
                            int index) throws
        HibernateException, SQLException {

        if (value == null) {
            statement.setNull(index, 0);
        }
        else {
            Color color = (Color) value;
            SQLOutput stream;
            stream.writeDouble(color.r);
            stream.writeDouble(color.g);
            stream.writeDouble(color.b);

            statement.setBinaryStream(index, (InputStream) stream, 128);
        }
}


Is this, in principle, the right pattern? (I know what I am doing above dosn't actually work, as I have not yet found a way to get a SQLInput from a ResultSet and/or a SQLOutput from a PreparedStatement - but I assume there is some way.).

Thanks again & best regards,

Thorsten


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 23, 2004 3:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Looks alright to me


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

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.