I have a simple entity bean as follows
package com.persistence.entities;
import javax.persistence.*; @Entity @Table(name = "picture") public class Picture implements java.io.Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @Column(name = "media_info") private String mediaInfo; @Lob //@Type(type = "com.persistence.entities.types.BlobType") private byte[] blobData; public Picture() { } public Picture(String mediaInfo, byte[] blobData) { setMediaInfo(mediaInfo); setBlobData(blobData); } public void setMediaInfo(String mediaInfo) { this.mediaInfo = mediaInfo; } public void setBlobData(byte[] blobData) { this.blobData = blobData; } public long getId() { return id; } public String getMediaInfo() { return mediaInfo; } public byte[] getBlobData() { return blobData; } @Override public String toString() { return "BlobDataRecord{" + "id=" + id + ", mediaInfo='" + mediaInfo + '\'' + ", blobData=" + new String(blobData) + '}'; } }
And a simple test as follows
public static void main(String[] args) { EntityManagerFactory factory = Persistence.createEntityManagerFactory("albumTest"); EntityManager entityManager = factory.createEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); Picture pic = new Picture("type/command-unix", "asdf #@$".getBytes()); entityManager.persist(pic); tx.commit(); Picture previousPic = entityManager.find(Picture.class, 1L); System.out.println(previousPic.getBlobData().toString() + " --> blob"); }
The above works fine, even when I use @Lob annotation.
But, if I run another test, just try to retrieve the previously saved data from the blob table
public static void main(String[] args) { EntityManagerFactory factory = Persistence.createEntityManagerFactory("albumTest"); EntityManager entityManager = factory.createEntityManager(); Picture previousPic = entityManager.find(Picture.class, 1L); System.out.println(previousPic.getBlobData().toString() + " --> blob"); }
I get the following exception
java.lang.UnsupportedOperationException: The method com.sybase.jdbc3.jdbc.SybResultSet.getBlob(String) is not supported and should not be called. at com.sybase.jdbc3.jdbc.ErrorMessage.raiseRuntimeException(Unknown Source) at com.sybase.jdbc3.utils.Debug.notSupported(Unknown Source) at com.sybase.jdbc3.jdbc.SybResultSet.getBlob(Unknown Source) at org.hibernate.type.descriptor.sql.BlobTypeDescriptor$1.doExtract(BlobTypeDescriptor.java:64) at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64) at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:261) at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257) at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:247) at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:332) at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2912) at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1672) at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1604) at org.hibernate.loader.Loader.getRow(Loader.java:1504) at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:712) at org.hibernate.loader.Loader.processResultSet(Loader.java:942) at org.hibernate.loader.Loader.doQuery(Loader.java:910) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:311)
So, my question is, why does it work when I save and retrieve it in the same test and why not when I run a separate test just to get the blob data?
Of course, it always works fine when I use custom hibernate usertype for the sybase blob.
Using sybase ASE 15.0, Hibernate 3.6.10, jconn3.jar as sybase JDBC driver. Tried with latest Hibernate versions as well but it didnt help.
Anyone, please?
|