What type of data record with the annotation @ Lob with JPA?

MiguelCPJava picture MiguelCPJava · Dec 29, 2012 · Viewed 10.3k times · Source

I have a problem, When I'm recording a PDF file inside the bank, I better save as file or byte []?

@Lob @Basic(fetch=FetchType.EAGER)
@Column(name="arqdocumento")
private File arquivo; 

or

@Lob
@Basic(fetch=FetchType.EAGER)
@Column(name="arqdocumento")
private byte[] arquivo; 

And how do I get this file from database and display in the browser?

I wonder why I do it this way:

public File getDocumentoBinary(int iDdocumento){
   Query consulta = getSesseion().createSQLQuery("SELECT arqdocumento FROM documento WHERE iddocumento = :id");
   consulta.setInteger("id", iDdocumento);
   return  (File) consulta.uniqueResult();     }

but displays this error:

Caused by: org.hibernate.MappingException: No Dialect mapping for JDBC type: -4

Answer

MGPJ picture MGPJ · Dec 29, 2012
@Lob
@Basic(fetch=FetchType.EAGER)  
@Column(name="arqdocumento")  
private byte[] arquivo;  

is the correct approach to store the huge amount of data in the DB. Your error says you have to include the Dialect in the persistence.xml file. So verify you have entered the correct Dialect property.