1

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!

5
  • 1
    Why is your testBlob an Integer and not byte[] or string? Commented Jan 15, 2016 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? Commented Jan 15, 2016 at 10:10
  • For facade: File Upload. byte[] for your blob testBlob should be OK. Commented Jan 15, 2016 at 10:14
  • @Mikey Thank you, I'll keep looking for examples who uploaded to DB, forgot to mention that. Commented Jan 15, 2016 at 10:37
  • HOw about that: how-to-write-java-sql-blob-to-jpa-entity Commented Jan 15, 2016 at 10:48

1 Answer 1

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;
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.