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 beanCode:
@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 bridgeCode:
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.