-->
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.  [ 5 posts ] 
Author Message
 Post subject: Mapping a URL property to mysql
PostPosted: Thu Sep 10, 2009 3:16 am 
Newbie

Joined: Thu Sep 10, 2009 2:43 am
Posts: 7
Hi
My entity has a java.net.URL property:
Code:
public URL getLogUrl(){..}

My DB is a mysql, and i m using Spring and JPA:
Code:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
       <property name="persistenceXmlLocation" value="WEB-INF/persistence.xml"/>
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" /> <!-- Prints used SQL to stdout -->
                <property name="generateDdl" value="true" /> <!-- Generates tables. -->
                <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
            </bean>
        </property>
    </bean>

This creates the table with a tinyblob column for the URL, and then when persisting, the URL is saved by serializing it (which that alone is not good anyway). The problem is that when reading from the table, i get:
Code:
Caused by: org.hibernate.type.SerializationException: could not deserialize
Caused by: java.io.StreamCorruptedException: invalid stream header: 68747470

I've tried adding:
Code:
@Column(columnDefinition = "VARCHAR(1024)")

to the property. This persisted the URL as String, but when reading i got the same problem! Looks like it was written as String and read as blob.

How can i make the URL field be persisted as string (and read as one...)?


Top
 Profile  
 
 Post subject: Re: Mapping a URL property to mysql
PostPosted: Sun Sep 13, 2009 7:08 am 
Newbie

Joined: Thu Sep 10, 2009 2:43 am
Posts: 7
I've used userType. Seems quite basic, isn't there some default URLUserType class implemented by hibernate or spring?


Top
 Profile  
 
 Post subject: Re: Mapping a URL property to mysql
PostPosted: Sun Sep 13, 2009 10:04 pm 
Beginner
Beginner

Joined: Thu Feb 21, 2008 3:31 pm
Posts: 34
Hi Reformy,

I am using annotations, but I would assume you can most certainly do the same with XML mapping as it has the same features if not more than the annotations provide. I store java.io.File objects in my database so I don't have to mess with prefixes and determining then where files are. It is very trivial:

@Lob
@Column
public File getFile()
{return(file);}


In your case, you'd probably want to use:

@Lob
@Column
public URL getURL()
{return(url);}

java.net.URL is serializable, so this should work. If you're trying to store binary data as a char (text) by specifying the type as varchar(1024), you're bound to get corruption, hibernate is smart enough to figure out if the column needs to be binary or not.


Walter


Top
 Profile  
 
 Post subject: Re: Mapping a URL property to mysql
PostPosted: Mon Sep 14, 2009 2:09 am 
Newbie

Joined: Thu Sep 10, 2009 2:43 am
Posts: 7
Thanks for your answer.

walterw wrote:
I am using annotations, but I would assume you can most certainly do the same with XML mapping as it has the same features if not more than the annotations provide.

I m using annotations too...

walterw wrote:
java.net.URL is serializable, so this should work.

I don't want to serialize the URL object, and I think you shouldn't either... serialization of objects to DB may cause you problems in the future when the serialized class changes.


Top
 Profile  
 
 Post subject: Re: Mapping a URL property to mysql
PostPosted: Mon Sep 14, 2009 9:06 pm 
Beginner
Beginner

Joined: Thu Feb 21, 2008 3:31 pm
Posts: 34
You have a good point - a String is also human-readable so you're SQL insert scripts would be more maintainable.

Walter


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