Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Hibernate Blob data type mapping

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to implement Hibernate Blob data type mapping". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The following is an example of Hibernate Blob data type mapping, which is used to grasp Hibernate Blob data type mapping.

Hibernate Blob:Java Code:

Public class User implements Java.io.Serializable {/ / Fields private long id; private String name; private String email; private String addr; / / defines the pthto private Blob photo of Blob

Hibernate Blob:xml Code:

Two test methods:

Java Code:

Public void testCreate () {User user = new User (); user.setName ("linweiyang"); user.setAddr ("beijing"); user.setEmail ("linweiyang@163.com"); Blob photo = null Try {/ / reads the picture into the input stream FileInputStream fis = new FileInputStream ("c:\\ a.jpg"); / / converts it to Blob type photo = Hibernate.createBlob (fis);} catch (FileNotFoundException e) {e.printStackTrace () } catch (IOException e) {e.printStackTrace ();} user.setPhoto (photo); Session session = factory.openSession (); Transaction tr = session.beginTransaction (); session.save (user); tr.commit (); session.close () } public void testRerieve () {Session session = factory.openSession (); User user = (User) session.load (User.class, new Long (3)); try {/ / to read from the database InputStream is = user.getPhoto () .getBinaryStream () / / in a file written to an image format, FileOutputStream fos = new FileOutputStream ("c:\\ linweihan.jpg"); byte [] buffer = new byte [1024]; int len = 0 / / read from the database to the specified byte array while ((len = is.read (buffer))! =-1) {/ / read from the specified array and then output it, so here buffer seems to be something that connects inputStream and outputStream fos.write (buffer,0,len) } catch (FileNotFoundException e) {e.printStackTrace ();} catch (SQLException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} session.close ();}

To understand the input and output stream in this way, the read-in stream naturally has to have the source of reading, and the output also has to be output to a certain place. The output usually has to be read first, where the input and output are connected by a byte array buffer in memory. So read from the database to this array, and the output stream is output from this array to a specific file format.

This is the end of "how to implement Hibernate Blob data type mapping". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report