0

I want to upload a BLOB from the file explorer, but I'm a little new to uploading blobs, especially when using JPA(Java Persistance API).

I thought I'd show you some of my code and see if you can give me any ideas towards the right direction.

My entity class looks like this:

@Entity
@Table(name = "exampletable")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Blob1337.findByTestBlob", query = "SELECT e FROM Blob1337 e WHERE e.testBlob = :testBlob")})

@Column(name = "test_blob")
private Integer testBlob;

public Integer getTestBlob() {
return testBlob;
}

public void setTestBlob(Integer testBlob) {
    this.testBlob = testBlob;
}

As you can see, I'm not sure what to use for BLOBs here, so it's an Integer for now.

My facade looks like this:

@PUT
@Path("{id}")
@Consumes({"application/json"})
public void edit(@PathParam("id") Integer id, Blob1337 entity) {

    Blob1337 entityToMerge = find(id);

    if (entity.getTestBlob() != null) {
        entityToMerge.setTestBlob(entity.getTestBlob());
}
    super.edit(entityToMerge);

}

How do I make my facade and entity class know that this is a BLOB? I want to be able upload this document through an ajax post, which should be pretty elementary if I'm correct.

Thank you! Any help is greatly appreciated!

Dr Cox
  • 149
  • 6
  • 35
  • 1
    Why is your `testBlob` an `Integer` and not `byte[]` or `string`? – Mikey Jan 15 '16 at 10:08
  • @Mikey That's what I'm looking for, what to use instead of Integer. And how to handle it if that's needed, in the Facade posted beneath. Should I use some kind of inputstream or something? – Dr Cox Jan 15 '16 at 10:10
  • For facade: [File Upload](http://stackoverflow.com/questions/24890675/file-upload-using-rest-api-in-java). `byte[]` for your blob `testBlob` should be OK. – Mikey Jan 15 '16 at 10:14
  • @Mikey Thank you, I'll keep looking for examples who uploaded to DB, forgot to mention that. – Dr Cox Jan 15 '16 at 10:37
  • HOw about that: [how-to-write-java-sql-blob-to-jpa-entity](http://stackoverflow.com/questions/5031585/how-to-write-java-sql-blob-to-jpa-entity) – Mikey Jan 15 '16 at 10:48

1 Answers1

0

I've finally managed to upload a blob through rest/ajax

The Facade should've looked like this(this expects a blob and streams it into the DB):

    @POST
    @Path("upload/{id}")
    @Consumes({"application/x-www-form-urlencoded", "multipart/form-data"})

    public void addBlob(@PathParam("id") Integer id, @FormDataParam("file") InputStream uploadedInputStream) throws IOException {
        E24ClientTemp entityToMerge = find(id);
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = uploadedInputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            entityToMerge.setTestBlob(out.toByteArray());
            super.edit(entityToMerge);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

And instead of an integer for the blob as posted in the question above, I used a byte[] with a @Lob annotation

Like this:

@Lob
@Column(name = "test_blob")
private byte[] testBlob;

Bonus, for future visitors - this is an example of how the Ajax could look like in this scenario:

    $(".uploadDocumentGeneral").on("click", function (evt) {
    IdToEdit = $(this).closest('tr').siblings().find('p.important').text();
    var url = "http://localhost:10110/MavenProject/api123/General123/upload/"+IdToEdit;
    evt.preventDefault();

    var documentData = new FormData();
    documentData.append('file', $('input#file.findDocumentOnboarding')[0].files[0]);
    $.ajax({
        url: url,
        type: 'POST',
        data: documentData,
        cache: false,
        contentType: false,
        processData: false,
        success: function (response) {
            alert("Document uploaded successfully.");
        }
    });

    return false;
});
Dr Cox
  • 149
  • 6
  • 35