Getting Blob from MySQL Database in Hibernate / JSF Application

I am having trouble getting blob data from my database using java in a JSF 2.0 application. The first problem I came across is my getter method for Blob named "file". I get the error "Basic attributes can only be of the following types ...", basically saying that I cannot return a Blob object. My code looks like below. Even with an error, my code compiles. I created a test method (last bit of code below) and tried to check, but it gives me runtime errors.

Bean controller

import java.sql.Blob;

@Entity
@Table(named = "files")
@NamedQueries( {
    @NamedQuery(name = "MyBlob.getBlob",
    query = from MyBlob WHERE fileId =: fileId")
})
public class MyBlob implements Serializable {

     private Integer fileId;
     private Blob file;
     ...

     public Integer getFileId() {
         return fileId;
     }

     public void setFileId() {
         this.fileId = fileId;
     }

     public Blob getFile() {
         return file;
     }

     public void setFile(Blob file) {
         this.file = file;
     }

     ....

}

BlobDao.java File method for getting blob

public MyBlob getBlob(Integer fileId) throws HibernateException {
    Session session = getSessionFactory().openSession();
    try { 
      MyBlob blob = (MyBlob)session.getNamedQuery("MyBlob.getBlob").setInteger("fileId", fileId).uniqueResult();
      return blob;
    } catch(HibernateException e) {
         throw new HibernateException(e);
    } finally {
        session.close();
    }
}

TestDao.java

@Test
public void testBlob() {

    MyBlob test = blobdao.getBlob(1);  // 1 is a fileId that exists in the DB with a blob image file.
    Blob blob = test.getFile();
    try {
        System.out.println(blob.length));   //this prints a number but I dont think the right one. 
        if(blob.length > 0 ) {
             System.out.println(blob.toString() ); //errors out here
        }
    } catch(SQLException e) {
          return System.out.println("Failed");
    }
}

I'm not sure I'm doing it right. Any help would be great. Thank.

+3
3

getter Blob "file" " ...", , Blob.

, a java.sql.Blob , Hibernate/JPA. JPA :

2.1.1

...

: Java; java.lang.String; Java ( , java.math.BigInteger java.math.BigDecimal, java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Time, java.sql.Timestamp, , byte[], byte[], char[] Character[]); ; / ; (. 2.1.5).

, Lob . :

9.1.19

Lob , a . Lob. Lob Basic. A Lob , . Lob , Blob.

...

1:

@Lob @Basic(fetch=EAGER)
@Column(name="REPORT")
protected String report;

2:

@Lob @Basic(fetch=LAZY)
@Column(name="EMP_PIC", columnDefinition="BLOB NOT NULL")
protected byte[] pic;

  • JPA 1.0
    • 2.1.1 " "
    • 9.1.19 " "

, , Blob Blob, byte [], , , - InputStream.

ByteArrayInputStream(byte[])?

+1

, Blob - Entity. , .

, Blob byte[]

@Basic(fetch = FetchType.LAZY)
@Lob             
@Column(length = 104857600, nullable = false)
private byte[] data;
0

, , , , Blob BLOB- . Blob , [],

0

Source: https://habr.com/ru/post/1771285/


All Articles