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