How to write java.sql.Blob to JPA entity?

amorfis picture amorfis · Feb 17, 2011 · Viewed 70.9k times · Source

I have a JPA entity with java.sql.Blob:

@Entity
public class LargeData {

  @Lob
  private java.sql.Blob data;

  //getters/setters
}

How to create instance of this entity? I want to set Blob with setData() method, but how to get Blob from JPA? java.sql.Blob is only interface, there are different implementations for different databases, so I assume JPA should give me right implementation. How to get it?

Answer

Bozho picture Bozho · Feb 17, 2011

Use a byte array:

@Lob
@Column(length=100000)
private byte[] data;

If you want to use streams, create the blob using Hibernate.createBlob(..)