-->
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.  [ 9 posts ] 
Author Message
 Post subject: How do I map file data in .hbm file.
PostPosted: Mon May 12, 2008 2:39 am 
Newbie

Joined: Mon Mar 31, 2008 10:15 am
Posts: 5
Hi all,

I am working on a struts based online file management system. On this system user have facility to upload his file into the database
I am using formFile by which, I can get all the necessary information of the file like name, type and data. I have mapped name and type in .hbm mapping file. But I don’t know how to map the file data in mapping file. The file data is in the form of byte array.
I am using mySql 5.0 as database and the file table contains following rows:

1. FILE_ID (Type int)
2. FILE_NAME (Type varChar(50))
3. FILE_TYPE (Type varChar(50))
4. FILE_DATA (Type LONGBLOB)


Please help me.

Thanks in advance.

_________________
Sachin


Top
 Profile  
 
 Post subject: blob
PostPosted: Tue May 13, 2008 6:56 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
<property name="fileData" type="blob" .../>


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 13, 2008 10:17 am 
Newbie

Joined: Mon Mar 31, 2008 10:15 am
Posts: 5
Thanks for suggetion but when I am trying to map Bolb in the .hbm file as follows:

<property name="fileData" type="java.sql.Blob" column="FILE_DATA" length="1048567" />
then I am getting following exception:

javax.servlet.ServletException: java.lang.ClassCastException: [B cannot be cast to java.sql.Blob


Can you tell me where is the problem.

_________________
Sachin


Top
 Profile  
 
 Post subject: more details
PostPosted: Tue May 20, 2008 8:17 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Could you pls post the entire exception?
thanks.


Top
 Profile  
 
 Post subject: Re: How do I map file data in .hbm file.
PostPosted: Mon Sep 13, 2010 2:07 pm 
Newbie

Joined: Mon Sep 13, 2010 1:55 pm
Posts: 4
Hi, did anyone manage to fix this issue because i am getting the same issue as the OP. i cant use longblob


Top
 Profile  
 
 Post subject: Re: How do I map file data in .hbm file.
PostPosted: Mon Sep 13, 2010 5:21 pm 
Regular
Regular

Joined: Sun Feb 14, 2010 3:29 pm
Posts: 58
Location: USA
Try
Code:
type="blob"
instead of
Code:
type="java.sql.Blob"


Read http://docs.jboss.org/hibernate/stable/ ... n-property

Read also on sticky post on how to post question.

_________________
Zemian Deng
------------
Need a Java Scheduler? Try
http://bitbucket.org/timemachine/scheduler


Top
 Profile  
 
 Post subject: Re: How do I map file data in .hbm file.
PostPosted: Tue Sep 14, 2010 3:11 am 
Newbie

Joined: Mon Sep 13, 2010 1:55 pm
Posts: 4
yes i have tried :

Code:
<property name="file" type="blob" column="FILE" />
but i still get this exception:

Code:
java.lang.ClassCastException: [B cannot be cast to java.sql.Blob


here is how my fileObject class looks like:

Code:
package com.kc.models;

public class FileObject {
   
   private String fileName;
   private String type;
   private double size;
   private byte[] file;
   private int id;
   
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public String getFileName() {
      return fileName;
   }
   public void setFileName(String fileName) {
      this.fileName = fileName;
   }
   public String getType() {
      return type;
   }
   public void setType(String type) {
      this.type = type;
   }
   public double getSize() {
      return size;
   }
   public void setSize(double size) {
      this.size = size;
   }
   public byte[] getFile() {
      return file;
   }
   public void setFile(byte[] file) {
      this.file = file;
   }
   
   

}


And here is my fileObject.hbm.xml file:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.kc.models.FileObject" >

   <class name="com.kc.models.FileObject" table="FILES">
      <id name="id" column="ID">
         <generator class="native" />
      </id>
      <property name="fileName" type="string" column="FILENAME" />
      <property name="type" type="string" column="TYPE" />
      <property name="size" type="double" column="SIZE" />
      <property name="file" type="blob" column="FILE" />
   </class>

</hibernate-mapping>


O and here is a print screen of mySql: Image


Top
 Profile  
 
 Post subject: Re: How do I map file data in .hbm file.
PostPosted: Wed Sep 15, 2010 8:53 am 
Regular
Regular

Joined: Sun Feb 14, 2010 3:29 pm
Posts: 58
Location: USA
The type blob is for java.lang.Blob. If you are going to use byte[], then use the type "binary".

Also, the default binary type is to use tinyblob in MySQL, if you want longblob, use the length attribute. For example:

Code:
<!-- set length to 16M, ensure your database driver allow this size as well. -->
<property name="file" type="binary" column="FILE" length="16777216" />

_________________
Zemian Deng
------------
Need a Java Scheduler? Try
http://bitbucket.org/timemachine/scheduler


Top
 Profile  
 
 Post subject: Re: How do I map file data in .hbm file.
PostPosted: Wed Sep 15, 2010 8:56 am 
Regular
Regular

Joined: Sun Feb 14, 2010 3:29 pm
Posts: 58
Location: USA
Again, I suggest you read the ref manual more carefully to understand what the type means in mapping.

You have java type, hibernate basic type, and then SQL type. The type used in mapping file can be either a hibernate basic type or a java type.

I have pointed the relevant sections for you already:

http://docs.jboss.org/hibernate/stable/ ... n-property

http://docs.jboss.org/hibernate/stable/ ... basictypes

_________________
Zemian Deng
------------
Need a Java Scheduler? Try
http://bitbucket.org/timemachine/scheduler


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