Hibernate ing for Large Object Types

Source: Internet
Author: User

Basic knowledge:

In Java, java. lang. string can be used to represent a long String (more than 255 characters in length). byte [] can be used to store binary data of images or files. in addition, java. SQL. clob and java. SQL. blob type, which corresponds to CLOB and BLOB types in standard SQL respectively. CLOB indicates the Character Large Object and BLOB indicates the Binary Large Object)

Mysql does not support the CLOB type of standard SQL. in Mysql, the TEXT, MEDIUMTEXT, and LONGTEXT types are used to represent long TEXT data with a length of 255 operations.
In the persistence class, large binary objects can be declared as byte [] or java. SQL. Blob. Strings can be declared as java. lang. String or java. SQL. Clob.
In fact, Java. lang. String is more convenient than java. SQL. Clob to process strings with a length of more than 255 characters.

How to map? To precisely map the SQL type, you can use the SQL-type attribute, for example:

VcGVydHkgbmFtZT0 = "content">







Save binary blob:
@Testpublic void testBlob() throws Exception{News news = new News();news.setAuthor("cc");news.setContent("CONTENT");news.setDate(new Date());news.setDesc("DESC");news.setTitle("CC");InputStream stream = new FileInputStream("Hydrangeas.jpg");Blob image = Hibernate.getLobCreator(session)              .createBlob(stream, stream.available());news.setImage(image);session.save(news);}

Read Binary blob:
@Testpublic void testBlob() throws Exception{News news = (News) session.get(News.class, 1);Blob image = news.getImage();InputStream in = image.getBinaryStream();System.out.println(in.available()); }




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.