Hibernate large Object Type hibernate mapping

Source: Internet
Author: User

Basic knowledge:

    • In Java, java.lang.String it can be used to represent long strings (over length 255), byte array byte[] can be used to store binary data for pictures or files. In addition, the Java.sql.Clob and Java.sql.Blob types are available in the JDBC API, respectively, corresponding to the Clob and BLOB types in standard SQL. CLOB represents the string large object (Character Large object), and the BLOB represents the binary objects (binary Large object)

    • MySQL does not support standard SQL CLOB type, in MySQL, with the text, Mediumtext and Longtext types to represent the length of operation 255 long text data
    • In a persisted class, a binary large object can be declared as byte[] or java.sql.Blob type; String can be declared as java.lang.String or Java.sql.Clob
    • Actually handling strings longer than 255 in Java applications, using java.lang.String is more convenient than Java.sql.Clob

How to map? You can use the Sql-type property if you want to map the SQL type exactly. For example:<!--mapping Large objects--
<!--can use the Sql-type property if you want to accurately map SQL types. -
<property name= "Content" >
<column name= "CONTENT" sql-type= "Mediumtext" ></column>
</property>

<property name= "image" >
<column name= "IMAGE" sql-type= "Mediumblob" ></column>
</property>

To save a 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);}

To read a 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 ()); }




Hibernate large Object Type hibernate mapping

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.