14

Closely following this example, I'm uploading a small file and trying to store into postgresql bytea column.

Here is error (first two outputs are logging statements outputting attributes of bean before the INSERT is attempted:

SAGE 1 -- action.registration.LetterTemplateHome - content type: text/xml

SAGE 1 -- action.registration.LetterTemplateHome - letterTemplateText: [B@48c7aaef

SAGE 1 -- action.registration.LetterTemplateHome - contents as String: xml version="1.0" encoding="UTF-8" standalone="yes" .... etc

SAGE 1 -- org.hibernate.util.JDBCExceptionReporter - Batch entry 0 insert into letter_template (content_type, file_name_template, fileSize, letter_template_name, letter_template_text, letter_template_id) values ('text/xml', 'letterDate.xml', '0', 'yu', '37078', '202') was aborted. Call getNextException to see the cause.

SAGE 1 -- org.hibernate.util.JDBCExceptionReporter - ERROR: column "letter_template_text" is of type bytea but expression is of type bigint Hint: You will need to rewrite or cast the expression. Position: 162

here is how the field is defined in the bean:

    private byte[] letterTemplateText;

@Lob
@Column(name = "letter_template_text")
@Basic(fetch = FetchType.LAZY)
public byte[] getLetterTemplateText() {
    return this.letterTemplateText;
}

public void setLetterTemplateText(byte[] letterTemplateText) {
    this.letterTemplateText = letterTemplateText;
}

5 Answers 5

13

For me it's working with type bytea in Postgres database.

@Lob
@Type(type="org.hibernate.type.BinaryType")
@Column(name = "photo")
private byte[] photo;

//and get and set
7

I suspect that Hibernate is trying to use the "large object" method with PostgreSQL, which involves storing an OID "handle" to the file in the table. Some example reading: http://virgo47.wordpress.com/2008/06/13/jpa-postgresql-and-bytea-vs-oid-type/

If you want to stick with just using a bytea column (and this is considerably simpler to work with on the SQL side), use BinaryType to map the column. See: proper hibernate annotation for byte[]

1
  • thanks. Your first link definitely described the problem. The problem, however, went away when I went to a different version of postgresql (and a different driver) I'll post a chart of which drivers/which postgresql versions / which annotations work together.
    – mcgyver5
    Commented Feb 3, 2012 at 15:14
4
change @Lob annotation with @Table 



   instead of this ; 
    **@Lob**
    @Column(name = "letter_template_text")
    @Basic(fetch = FetchType.LAZY)
    private byte[] icon;

    write ;
    
    **@Type(type="org.hibernate.type.BinaryType")**
    @Column(name = "letter_template_text")
    @Basic(fetch = FetchType.LAZY)
    private byte[] icon;
1

The similar error may appear if you fave <loadData/> with missed column tags inside and some csv file, with, say, path to blob files in some csv column used for populating blobs column in the table. It doesn't point to "wow, you don't have columns there in loadData tag", but assume all the csv columns are string data and then throws the said error (but writes column is bytea but expression is of type string). You just need to specify all columns of the table explicitly with column tags and provide their data type.

0

This doesn't really answer your question, but I thought I'd share anyway...

There are two ways of storing files with the help of a database, really: storing the actual contents of the file (like you're doing) and storing only the file path (and saving it in the actual filesystem).

I've worked with both methods and I preferred the latter for two reasons: I can move my files around to other hard drives, partitions and even access them through a share and all I need to do is change the file paths in the database. Also, it makes database dumps (aka backups) a lot smaller and faster to perform.

1
  • 2
    Thanks for your comment. Our environment is locked down as far as storing uploaded files in the filesystem. I'd have to have a really good reason for a variance. Also, we have load balancers and they handle database changes nicely. I'm not sure how they would juggle file system files. The amount of data in question is very small and will not impact performance. And, I think that creating this link between db and file system would turn out to be very fragile indeed.
    – mcgyver5
    Commented Feb 2, 2012 at 16:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.