Hibernate operates Blob data and hibernateblob data

Source: Internet
Author: User

Hibernate operates Blob data and hibernateblob data

 


First, let's look at the database. Create a New BlobTable table in the database. The table has two fields, one id (primary key) and one picture field, which is a Blob field. Then, use Hibernate to write and read data to the database.

In the POJO class, the picture attribute uses Blob data.

See the operation source code below

Package dao; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java. math. bigDecimal; import java. SQL. blob; import org. hibernate. lobHelper; import org. hibernate. query; import org. hibernate. session; import org. hibernate. transaction; import Factory. hibernateSessionFactory; import entity. bl Obtable; public class BlobDao {private Session session = null; private Transaction tran = null; public BlobDao () {this. session = HibernateSessionFactory. getSession ();} public void saveBlob (Blobtable bigdate, String path) throws IOException {/* InputStream in = this. getClass (). getResourceAsStream (path); byte [] bytes = new byte [in. available ()]; in. read (bytes); in. close (); */File file = new File (path); FileInputS Tream Sox = new FileInputStream (file); byte [] bytes = new byte [FCM. available ()]; FCM. read (bytes); LobHelper lh = session. getLobHelper (); bigdate. setPicture (lh. createBlob (bytes); tran = session. beginTransaction (); try {session. save (bigdate); tran. commit (); System. out. println ("inserted successfully! ");} Catch (Exception e) {System. out. println (" insertion failed! "); Tran. rollback ();} finally {HibernateSessionFactory. closeSession (); FCM. close () ;}} public void getBlob (BigDecimal id, String targetpath) throws Exception {String hql = "From Blobtable where id =? "; Query query = session. createQuery (hql); query. setBigDecimal (0, id); Blobtable bt = (Blobtable) query. uniqueResult (); Blob image = bt. getPicture (); InputStream in = image. getBinaryStream (); OutputStream OS = new FileOutputStream (targetpath); int n =-1; while (n = in. read ())! =-1) {OS. write (n) ;}in. close (); OS. close ();}}
Package Test; import java. io. IOException; import java. math. bigDecimal; import dao. blobDao; import entity. blobtable; public class Test {public static void main (String [] args) {BlobDao bb = new BlobDao (); Blobtable bt = new Blobtable (); bt. setId (new BigDecimal (5); try {String path = "f: \ a.jpg"; bb. saveBlob (bt, path);} catch (IOException e) {e. printStackTrace ();} BlobDao bd = new BlobDao (); try {bd. getBlob (new BigDecimal (1), "e: \ a.jpg"); System. out. println ("written successfully! ");} Catch (Exception e) {e. printStackTrace ();}}}



 


How does hibernate save blob data?

Java. SQL. Blob n = Hibernate. createBlob (bytes );
You can refer to the following example to help you a lot
2017.51cto.com/art/200906/130231.htm
 
Beginner in JAVA, operating Blob for Hibernate

In this case, we recommend that you change the oracle driver package to the latest one.
Hope to help you.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.