database - Get stream from java.sql.Blob in Hibernate -
i'm trying use hibernate @entity java.sql.blob store binary data. storing doesn't throw exceptions (however, i'm not sure if stores bytes), reading does. here test:
@test public void shouldstoreblob() { inputstream readfile = getclass().getresourceasstream("myfile"); blob blob = dao.createblob(readfile, readfile.available()); ent ent = new ent(); ent.setblob(blob); em.persist(ent); long id = ent.getid(); ent fromdb = em.find(ent.class, id); //exception thrown getbinarystream() byte[] fromdbbytes = ioutils.tobytearray(fromdb.getblob().getbinarystream()); }
so throws exception:
java.sql.sqlexception: not reset reader @ org.hibernate.engine.jdbc.blobproxy.getstream(blobproxy.java:86) @ org.hibernate.engine.jdbc.blobproxy.invoke(blobproxy.java:108) @ $proxy81.getbinarystream(unknown source) ...
why? shouldn't read bytes form db here? , can work?
try refresh entity:
em.refresh(fromdb);
stream reopened. suspect find(...) closing blob stream.
Comments
Post a Comment