Hello guys i have the the xpertivy v5....following process: User have to upload a pdf file. #1 Step The pdf file have to save into a postsql db (bytea field). #2 Step The user have to open the file from db What have i done? I created a java method. I can save and read a pdf from the postsqldb but it wont work in the xpert ivy process. How can i fetch a file over the fileinputstream with the databasestep and write it to the db? How can i read the file over the db step?

Thank you so much for your help

asked 13.10.2014 at 15:37

MaxIvy's gravatar image

accept rate: 0%

edited 13.10.2014 at 16:01

I guess you did something like this:


            p:fileUpload label="Upload" fileUploadListener="#{uploadBean.handleFileUpload}" mode="advanced"
                auto="true" />

Due to this is a primeface-action there should be no problem with this.

Your Java-Class:

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

public class UploadBean {

    public void handleFileUpload(FileUploadEvent event) {
        UploadedFile uploadedFile = event.getFile();
        byte[] myFile= uploadedFile.getContents();

The byte[] you now can save to your database as bytea.

How it works

In case you wonder how the persistence could be implemented:


@Table(name = "myFunny_FilesTableName")
public class Files implements Serializable {

private Long id;
    private static final long serialVersionUID = 1L;
        private byte[] myPdf;

    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id")
    public Long getId() {
        return id;

    public void setId(Long id) {
        this.id = id;

    @Lob //this annotation is only nescessary if you want it to save as a large object in your database. If you use @Lob //you get a postgres oid. If you just send the byte[] without annotation you get a bytea.
    public byte[] getMyPdf() {
      return myPdf;

    public void set(MyPdf(byte[] myPdf){
       //do sth

Persistence class:

public class FilesDao {

public Files saveOrUpdate(Files files) throws Exception {
    IIvyEntityManager entityManager= Ivy.persistence().get(myDatabaseContextName);
    Files newFiles;
    try {
        newFiles = entityManager.merge(files);
    } catch (Exception e) {
        Ivy.log().error("Unable to save file.");
        throw e;
    return newFiles;

And now you can add these line to save your files:

byte[] pdfByteArray;  //the byteArray from the primefaces

Files myPdf= new Files(); //create a new entity
myPdf.setMyPdf(pdfByteArray) //fill it with your data
Files thePersistedPdf= new FilesDao().saveOrUpdate(myPdf); //merge it to your databse.

Hope this helps a bit.


answered 14.10.2014 at 08:38

Daniel%20Oechslin's gravatar image

Daniel Oechslin
accept rate: 39%

edited 14.10.2014 at 09:11

Hi MaxIvy

You can do it the very same as explained here: http://jeebestpractices.blogspot.ch/2011/03/save-images-into-database-postgres-with.html

Just adjust the naming a little and remove the conversion part.


answered 13.10.2014 at 16:02

Daniel%20Oechslin's gravatar image

Daniel Oechslin
accept rate: 39%

Yes my java code is like this example but it wont work in the dynamic ivy process. how can i start the javamethod from a processtep ( in the expample ImageToByte() ) from the ivy process ? The picture below is the the two option what i tried the option 2 wont work

(13.10.2014 at 16:09) MaxIvy MaxIvy's gravatar image

alt text


answered 13.10.2014 at 16:18

MaxIvy's gravatar image

accept rate: 0%

Thank you su much for your answer and soory that i get a little bit late get back: For the moment i got an error in this line IIvyEntityManager entityManager= Ivy.persistence().get(myDatabaseContextName); --> What have i put here inside?

Thank u so much


answered 01.12.2014 at 11:11

MaxIvy's gravatar image

accept rate: 0%

Added a new answer as response.

(01.12.2014 at 12:54) Daniel Oechslin Daniel%20Oechslin's gravatar image

Hi MaxIvy

below you see a sample database configuration. You can add your connection as shown in the picture and configure with the parameters you need and add the name- here: building as your myDatabaseContextName.

Hope this helps Daniel

Database Config


answered 01.12.2014 at 11:41

Daniel%20Oechslin's gravatar image

Daniel Oechslin
accept rate: 39%

Hi Daniel, Thank you for your answer. I tried this of course, but i wont work. Look at the screen alt text

Im am not sure whats wrong


answered 01.12.2014 at 13:39

MaxIvy's gravatar image

accept rate: 0%

You added the database correctly. Hence you need to add the persistence as well. Add a persistence called "antraege" with the datasource "antraege". Then the configuration part is done.

Now you can provide the name "antraege" from your persistence configuration as a String to your configuration:

IIvyEntityManager entitymanager= Ivy.persistence().get("antraege");

(01.12.2014 at 13:50) Daniel Oechslin Daniel%20Oechslin's gravatar image

OK thanks :-) i got i now one step more to my target. I have this database: antrage, where i want to save the pdf files to specific id from the formular. Where in the code have i put my Insert-into prepareStament line ("INSERT INTO antraege VALUES ? WHERE id=?) and how can i excute the statement... Thank u so much for your help.

(01.12.2014 at 14:56) MaxIvy MaxIvy's gravatar image

IIvyQuery query = getEntityManager().createQuery("INSERT XXX"); query.setParameter("file", ...); query.executeUpdate()

(03.12.2014 at 13:10) Reguel Werme... ♦♦ Reguel%20Wermelinger's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: 13.10.2014 at 15:37

Seen: 9,800 times

Last updated: 03.12.2014 at 13:13