-->
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: Blob bridge error
PostPosted: Mon Sep 24, 2007 5:41 am 
Newbie

Joined: Tue Sep 11, 2007 9:05 am
Posts: 13
Ok heres the deal.

I am using hibernate (with annotations), struts and the lucene integrated module.
What I need is to be able to upload a file, index it and be able to retreave it.
So far I have created my DB, objects, bridge.

Heres the problem.

I upload a file(pdf always), i add it to the objects (setContent which is mapped as a Blob) document1.setContent(Hibernate.createBlob (myFile.getInputStream())); then the custom bridge takes over and i try to get the content as a String using PDFBox.

The problem occurs when executing blob.getBinaryStream(), after i cast it to a blob (note this works sometimes when the pdffile is small in size but when it works for some reason pdfbox doesnt get any text....).

I get the infamous "java.sql.SQLException: could not reset reader" error.

Here is the code for the action

Code:
EntityManager emMySQL = HibernateUtil.getEntityManagerFactory().createEntityManager();
            Collection documentos = new ArrayList();
           
           
            if(myFile!=null && !df.get("myFileName").toString().equals("")){
            emMySQL.getTransaction().begin();
                               
            Gorilla ape2 = new Gorilla();
            ape2.setLeapingHeight(new Long(255));
            Documento documento1 = new Documento();
            documento1.setTitulo(df.get("myFileName").toString());
            documento1.setNome(myFile.getFileName());
            documento1.setContenido(Hibernate.createBlob (myFile.getInputStream()));           
            ape2.getDocumentos().add(documento1);           
            emMySQL.persist(ape2);

           
            emMySQL.getTransaction().commit();   


Here is the code for the bean

Code:
@Entity
@Indexed()
public class Documento implements java.io.Serializable {

    @Id
    @DocumentId
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
   
    @Column(name = "titulo", nullable = false)
    @Field(index=Index.TOKENIZED, store=Store.NO)
    private String titulo;
   
    @Column(name = "nome", nullable = false)
    @Field(index=Index.TOKENIZED, store=Store.NO)
    private String nome;
   
    @Lob   
    @Column(name = "contenido", nullable = false)
    @FieldBridge(impl=BlobBridge.class)
    @Field(index=Index.TOKENIZED, store=Store.NO)   
    private Blob contenido;
   
       
    @ManyToOne()
    @JoinColumn(name="ape_id", nullable=false, insertable=false, updatable=false)
    private Ape ape;
   
    public Documento() {             
    }
   
    public Documento(String titulo) {       
        this.titulo=titulo;
    }   
   
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        id = id;
    }
   

    public String getTitulo() {
        return titulo;
    }

    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }   
   
    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }
   
    public Ape getApe() {
        return ape;
    }

    public void setApe(Ape ape) {
        this.ape = ape;
    }

    public Blob getContenido() {
        return contenido;
    }

    public void setContenido(Blob contenido) {
        this.contenido = contenido;
    }

Here is the code for the bridge

Code:
public class BlobBridge implements StringBridge{
   
    public String objectToString(Object object) {
       
        Blob blob = (Blob)object;
        String texto;
        try {
            texto = stripText(blob.getBinaryStream());
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
       
        if(texto!=null)return texto;
        else return null;
       
    }
   
    private String stripText(InputStream is) {
       
        String texto ="";
        PDDocument doc = null;
       
        try {
            PDFTextStripper stripper = new PDFTextStripper();
           
           
            doc = PDDocument.load(is);           
            texto = stripper.getText(doc);                       
           
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {
            if( doc != null ) {
                try {
                    doc.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
        return texto;       
            }
        }
        return texto;
    }
   
}


I have read the topics on the session.refresh(blobEntityObject); but in my case i cant use it in the bridge (or can I?????)

Please help, your my only hope.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 25, 2007 4:54 am 
Newbie

Joined: Tue Sep 11, 2007 9:05 am
Posts: 13
Using the createblob(byte[]) constructor i get over the problem of reseting the reader.


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.