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.  [ 3 posts ] 
Author Message
 Post subject: Mapping to java.net.URL
PostPosted: Wed May 31, 2006 7:57 am 
Newbie

Joined: Thu May 05, 2005 8:08 am
Posts: 4
I am trying to map a database 'Varchar' column to a class property of type 'java.net.URL'.

However, a
Code:
org.hibernate.type.SerializationException: could not deserialize
is thrown at runtime.

If I change my class property type to String, it works fine. Is there any way I can map to a class property of type java.net.URL

Many Thanks...


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 8:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
The best way is to use a custom type that explicitly performs the conversion from URL to string-rep. What you were actually trying was to serialize the URL into the database.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 8:49 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
You need to create a CustomType as steve indicated. Here's part of what that implementation would look like, to help you out.

Code:
   public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
      throws HibernateException, SQLException {

        java.net.URL retVal = null;
        retVal = new java.net.URL((String) Hibernate.STRING.nullSafeGet(rs, names[0]));
        return retVal;
    }

   public void nullSafeSet(PreparedStatement st, Object value, int index)
      throws HibernateException, SQLException {

      if (value != null) {
         if (value instanceof String) {
            Hibernate.STRING.nullSafeSet(st, value, index);
         }
         else if (value instanceof java.net.URL) {
            java.net.URL url = (java.net.URL)value;
            Hibernate.STRING.nullSafeSet(st, url.toString(), index);
         }
      }
      else {
         st.setNull(index, Types.VARCHAR);
      }
   }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.