-->
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.  [ 6 posts ] 
Author Message
 Post subject: Persist file to db
PostPosted: Mon Sep 17, 2007 6:20 am 
Newbie

Joined: Tue Sep 11, 2007 9:05 am
Posts: 13
I am using hibernate with annotations and im currently looking at the search capabilities of the Lucene integration.

The problem i have concerns the annotation type to use when storing and retreaving the content of a file. I have searched through forums and google but havent found anything that has helped.

if i map the content as Blob or Byte[] i get an error at initialization

Initial SessionFactory creation failed.org.hibernate.search.SearchException: Unable to guess FieldBridge for content.

What should i annotate it as?? how do i persist it in the db? And all this in such a way that it can be indexed and searched apon.?

i am using MySQL.

Isn't there a way to get the inputstream of a file and simply pass it to the setter of the object??

Please help, thanks in advance


Top
 Profile  
 
 Post subject: LOB
PostPosted: Mon Sep 17, 2007 8:04 am 
Newbie

Joined: Tue Sep 11, 2007 9:05 am
Posts: 13
I found the problem. I had to specify the fieldbridge type to LongBridge.class.

The only problem now is that the content of the file doesnt get indexed or searched. The title which is a string does though.

here is the code for the object:

package com.agader.struts.actions.beansAnnotated;

import java.sql.Blob;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import org.hibernate.Hibernate;

import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.FieldBridge;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;
import org.hibernate.search.bridge.builtin.LongBridge;


@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;

@Lob
@Column(name = "contenido", nullable = false)
@FieldBridge(impl=LongBridge.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 leapingHeight) {
titulo = leapingHeight;
}

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;
}

}


Top
 Profile  
 
 Post subject: STRING string string
PostPosted: Mon Sep 17, 2007 9:39 am 
Newbie

Joined: Tue Sep 11, 2007 9:05 am
Posts: 13
Ok found the problem. Apparently you are not able to use diffarent types of variables other than string for the fields that are to be indexed.

So how im I supposed to insert files to the database via String??? Im guessing it has to go through the setter that has the index field annotation so that the listener fires up where an insert happens????


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 17, 2007 1:31 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
What you need to do is to save the field in a blob, but add a custom FieldBridge that will know what to index from your file and how to index it.
for example if your file is a PDF, you would have a FieldBridge routine that read the file and index the text stored in the PDF, the blob structure in the DB, will keep the original PDF file.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: FieldBridge
PostPosted: Wed Sep 19, 2007 4:33 am 
Newbie

Joined: Tue Sep 11, 2007 9:05 am
Posts: 13
Ok I get the part about the field bridge. I have created one that gets the string from the blob and returns it so that it can be indexed. The problem now is that i cant tell what kind of document it is. Depending wether the blob is a pdf , doc or whatever i need to parse it diffarently. The problem is i cant let the bridge know what kind of file its dealing with. The parameters you can pass to the fieldbridge are constants. Does anyone know of a way??


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 19, 2007 5:15 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Traditionally, you analyze the first bytes of the document to figure it out. Usually, you can tell the document type from the first few bytes, this is how strings are decoded for example.

_________________
Emmanuel


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