I have an Image class that has a byte [] to contain the actual image data. I can upload and paste the image just fine in my webapp. When I try to display an image after reading it from JPA, the length of my byte [] is always 2x-1 or 2x-2, where x is the length of the bytea field in postgres 9. Obviously, the image that it ruined will not be displayed by the browser. I could use some help figuring out why I get what I expect twice. Here is a mapping of my image class. Using eclipselink with JPA 2 hitting postgres 9 on mac.
When I select from a database with
select *, length(bytes) from image;
I get a length of 9765. At the breakpoint in my controller, the byte length [] is 19529, which is one byte, which is half the size of the database.
@Entity
@Table( name = "image" )
@SequenceGenerator( name = "IMAGE_SEQ_GEN", sequenceName = "IMAGE_SEQUENCE" )
public class Image
extends DataObjectAbstract<Long>
{
@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "IMAGE_SEQ_GEN" )
private Long key;
@Column( name="content_type" )
private String contentType;
@Lob
@Basic( optional=false )
@Column( name="bytes" )
private byte[] bytes;
}
pgadmin shows me the following for an image table
CREATE TABLE image
(
"key" bigint NOT NULL,
bytes bytea,
content_type character varying(255),
"version" integer,
CONSTRAINT image_pkey PRIMARY KEY (key)
)
WITH (
OIDS=FALSE
);
source
share