insert java byte[] into db postgres with mybatis

1,640 views
Skip to first unread message

dafcon

unread,
Jul 31, 2015, 10:08:54 AM7/31/15
to mybatis-user
Hi, I'm trying to insert a binary squence of a file in the record bytea of postgres. Unfortunately I get an exception: the column not exists

Inserisci qui il codice... (lug 31, 2015 10:39:02 AM com.sun.faces.lifecycle.ApplyRequestValuesPhase execute
AVVERTENZA: /views/contents/evento/insert/part/upload.xhtml @22,45 fileUploadListener="#{fileUpload.upload}": org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  Cause: org.postgresql.util.PSQLException: ERRORE: la colonna "bt_filebytes" della relazione "an_file" non esiste
  Posizione: 95)

can someone help me? 
Thank you in advance

DAO MAPPER
....
  <resultMap id="fileEntityResultMap" type="FileDB">
    <id column="id_file" jdbcType="BIGINT" property="idFile" />
    <result column="id_property" jdbcType="BIGINT" property="idProperty" />
    <result column="dt_property" jdbcType="DATE" property="dtProperty" />
    <result column="id_property2" jdbcType="BIGINT" property="idProperty2" />
    <result column="ds_property2" jdbcType="VARCHAR" property="dsProterty2" />
    <result column="bt_fileBytes" jdbcType="BLOB" property="btFileBytes" />
    <result column="fl_delete" jdbcType="BOOLEAN" property="flDelete" />
  </resultMap>
  
    <insert id="insertFile" parameterType="FileDB">
    <selectKey keyProperty="idFile" resultType="java.lang.Long" order="BEFORE">
   SELECT nextval('an_file_sequence');
</selectKey>
INSERT INTO
an_file (id_file id_property, dt_property, id_property2, ds_property, bt_fileBytes, fl_delete)
VALUES
(
#{idFile,jdbcType=INTEGER},
#{idProperty,jdbcType=INTEGER},
#{dtProperty,jdbcType=TIMESTAMP},
#{idProperty2,jdbcType=INTEGER},
#{dsProterty2,jdbcType=VARCHAR}, 
#{btFileBytes,jdbcType=BLOB},
#{flDelete,jdbcType=BOOLEAN}
)
</insert>
 
</mapper>

JAVA CLASS

public void copyFile(String fileName, InputStream in) 
    {
    logger.debug("copy file");
           try {
                OutputStream out = new FileOutputStream(new File(destination + fileName));
                int read = 0;
              buffer = new byte[10485760];// 10 Mb in byte
              
                while ((read = in.read(buffer)) != -1) {
                    out.write(buffer, 0, read);
                }
                in.close();
                out.flush();
                out.close();
    }
           catch (IOException e) 
           {
          JsfUtility.addErrorMessage("error.CaricamentoFile", "error.CaricamentoFile");
           }
           creaFileDB(buffer);
    }




 private void creaFileDB (byte[] fileB)
    {
    Utente utente = this.getUtenteLoggato();
    this.fileDaInserire.setDsNomeFile(this.event.getFile().getFileName());
    this.fileDaInserire.setIdUtenteCreate(utente.getIdUtente());
    this.fileDaInserire.setIdEvento(1L);
    this.fileDaInserire.setDtDatacreazione(new Date());
    this.fileDaInserire.setBtFileBytes(fileB);
    this.fileDaInserire.setFlDelete(false);
   
    fileMapper.insertFile(fileDaInserire);
   
   
    }

Thanks

Guy Rouillier

unread,
Aug 1, 2015, 12:52:37 AM8/1/15
to mybatis-user
You left out the most important part - the table definition.  From a psql prompt:
 
\d+ an_file
 
--
Guy Rouillier
 
--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

dafcon

unread,
Aug 1, 2015, 4:16:50 PM8/1/15
to mybatis-user, guy.ro...@gmail.com
Sorry

-- Table: an_file

-- DROP TABLE an_file;

CREATE TABLE an_file
(
 id_file bigint,
 id_user_create bigint,
  dt_date_crate timestamp without time zone,
 ds_name character varying(255),
 ds_tipo_file character varying(7),
  bn_file bytea,
 fl_delete character(1)
)
WITH (
 OIDS=FALSE
);
ALTER TABLE an_file
 OWNER TO postgres;


Guy Rouillier

unread,
Aug 2, 2015, 12:47:48 AM8/2/15
to dafcon, mybatis-user
There's your answer.  Your table contains a column called bn_file, but the SQL you provide references column bt_fileBytes.  Your entire column list - id_file id_property, dt_property, id_property2, ds_property, bt_fileBytes, fl_delete - doesn't match your table definition.
 
You need to update your SQL to reference the proper column names.
Reply all
Reply to author
Forward
0 new messages