-->
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.  [ 2 posts ] 
Author Message
 Post subject: Inserting blob data
PostPosted: Fri Mar 31, 2006 11:20 pm 
Newbie

Joined: Fri Mar 24, 2006 1:28 pm
Posts: 6
Hello,

I'm facing a strange problem when I try to insert a blob in my SQL database. I'm implementing a web application using Struts and hibernate and My SQL for the database. The application runs fine when I insert the blob (which is an image in my case). However, I'm unable to see the row in the table when I go to MySQL and do a "select" statement. All I get are a bunch of dashed lines that go on forever. I simply followed instructions given in a struts and hibernate tutorial to insert blob data and I don't get any errors when I run the application. Following are the details:

Table definition in SQL:

Images{ img_id int, AUTO_INCREMENT
....
ImagData longblob,
....
}

Code to insert blob:

// The java object that maps to the database table
1) public class Images{
....
private Blob imagData;

//the get and set methods for imagData
public Blob getImagData() {
return this.imagData;
}

public void setImagData(Blob data) {

this.imagData = data;
}
}

2) The Action that performs the insertion of blob into the database

public ActionForward execute{

...
// temp object to hold table data
Images image;

//blob to hold the image binary data
Blob imgBlob;
....

//obtains a transaction -- hibernate stuff
Transaction tx = session.beginTransaction();

//creates a blob with the image data
imgBlob = org.hibernate.Hibernate.createBlob(contentFile.getInputStream());

//sets the blob value in the object using the set method of the Images bean
image.setImagData(imgBlob);
...
//saves the data using hibernate
session.save(image);

//commits data to database using hibernate
tx.commit();
}

I'm also using myEclipse for my development and deploying on the sun web server. I don't get any compile or run time errors but the output when I do a "select" on the table is strange. Any ideas on why this is happening? Would appreciate any insightinto this strange behavior!


Top
 Profile  
 
 Post subject: Re: Inserting blob data
PostPosted: Tue Jun 21, 2011 5:48 pm 
Newbie

Joined: Mon Jun 20, 2011 8:35 pm
Posts: 1
package org.entity;

import java.sql.Blob;


public class Mp3 {
private int primaryKey;
private Blob mp3;
private Blob realMp3;

public int getPrimaryKey() {
return primaryKey;
}
public void setPrimaryKey(int primaryKey) {
this.primaryKey = primaryKey;
}

public Blob getRealMp3() {
return realMp3;
}
public void setRealMp3(Blob realMp3) {
this.realMp3 = realMp3;
}
public Blob getMp3() {
return mp3;
}
public void setMp3(Blob mp3) {
this.mp3 = mp3;
}

}

----------------------------------Mp3.java

<hibernate-mapping>
<class name="org.entity.Mp3" table="MP3">
<id name="primaryKey" type="int">
<column name="PRIMARYKEY" />
<generator class="increment" />
</id>
<property name="mp3" type="blob" >
<column name="MP3" sql-type="mediumblob" />
</property>
<property name="realMp3" type="blob" >
<column name="REAL_MP3" sql-type="mediumblob" not-null="true" />
</property>
</class>
</hibernate-mapping>
-----------------------------------Mp3.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory >
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">123</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ratan</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">ratan</property>
<property name="hibernate.hbm2ddl.auto">createorupdate</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping package="org.entity" resource="org/entity/Mp3.hbm.xml"/>
</session-factory>
</hibernate-configuration>
-----------------------------------------------hibernate.cfg.xml


package org.util;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
private static SessionFactory sessFactory;

static {
try{
sessFactory=new Configuration().configure().buildSessionFactory();
}catch(HibernateException e){
System.out.println("Hibernate Exception : "+e.getMessage());
}
}
public static SessionFactory getSessionFactory(){
return sessFactory;
}
}
-------------------------------------HibernateUtil.java


package org.main;
import java.io.*;
import java.sql.Blob;
import org.entity.Mp3;
import org.hibernate.*;
import org.util.HibernateUtil;

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
Main main=new Main();
main.uploadMp3();
}

public boolean uploadMp3(){


SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
Session session=sessionFactory.openSession();
Transaction transaction=session.beginTransaction();
Mp3 mp3=new Mp3();
//reading bytes from InputStream
InputStream inputStream=null;
try {
inputStream = new FileInputStream(new File("ExamResults.png"));
Blob blob1=Hibernate.createBlob(inputStream);
mp3.setMp3(blob1);
inputStream = new FileInputStream(new File("You're My Number One.mp3"));
Blob blob=Hibernate.createBlob(inputStream);
mp3.setRealMp3(blob);
} catch (FileNotFoundException e) {
System.out.println("File Not Found Exception");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException");
e.printStackTrace();
}
session.saveOrUpdate(mp3);
transaction.commit();
session.flush();
session.close();

return true;

}
}


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