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