-->
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.  [ 8 posts ] 
Author Message
 Post subject: Read binary object into database via hibernate
PostPosted: Wed Feb 11, 2004 1:58 pm 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
Hi,
what is the best way to read a binary object into a postgresql table via hibernate?

I have defined the setter in my class to handle the object as:

public byte[] getArtistPhoto1() {
return ArtistPhoto1;
}
public void setArtistPhoto1(byte[] newArtistPhoto1) {
this.ArtistPhoto1 = newArtistPhoto1;
}


The java code I had deleloped so far is:

Artist newArt = new Artist();
File file = new File("photos01.jpg");
FileInputStream fis = new FileInputStream(file);

------- Missing hibernate code. What goes here ???? --------
I have seen the setBinary(String


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 2:11 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Just use type=binary


Top
 Profile  
 
 Post subject: gloeglm
PostPosted: Wed Feb 11, 2004 2:16 pm 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
Could you elaborate a bit more about in what context you mean 'just use type=binary'? What do I need to alter in my code, java is no one of my strong points i'm affraid!?


many thanks in advance


Top
 Profile  
 
 Post subject: further to my earlier post...
PostPosted: Wed Feb 11, 2004 2:35 pm 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
I have defined in my hbm.xml file defined a field as:

<property name="ArtistPhoto1" column="artist_photo1" type="binary" /> is this is what you meant in your earlier post? If so, how do I read the image into hibernate in the first place?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 2:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well you can keep your byte[] property and just map it as property with type=binary.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 2:49 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Here's a mapping:
Code:
<hibernate-mapping default-cascade="all">

    <class name="Graphic" table="graphics">

        <id name="id" type="long">
            <generator class="native"/>
        </id>
       
        <property name="data" type="binary" column="data" not-null="true" unique="false"/>
               
    </class>
           
</hibernate-mapping>


The class:
Code:
import java.io.Serializable;
import net.sf.hibernate.Session;

public class Graphic implements Serializable {

    private byte[] data;
   
    public Graphic() {}

    public GraphicAsset(byte[] data) {
        this.data = data;
    }
   
    public byte[] getData() {
        return this.data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }
}


Loading one up:
Code:
import java.io.*;

public class GraphicLoader {

    public static void main(String[] args) throws IOException {
   
        Session session = ...get your hibernate session...
       
        Graphic graphic = new Graphic(readImage(args[0]));
       
        session.save(graphic);
       
        session.close();
    }
   
    private static final byte[] readImage(String imageName)
                                   throws IOException {

        FileInputStream fis =
            new FileInputStream(imageName);

        byte[] buf = new byte[4096];

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        int len = 0;

        while((len = fis.read(buf)) != -1) {
            baos.write(
                       buf,
                       0,
                       len
                      );
        }

        return baos.toByteArray();
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 2:54 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
If you're using java1.4 or above you can read the file using a java.nio.ByteBuffer.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 3:23 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Oops... In the full constructor of the POJO "GraphicAsset" should be "Graphic"


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