程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> hibernate3學習筆記(十四)|Blob、Clob

hibernate3學習筆記(十四)|Blob、Clob

編輯:關於JAVA

在Hibernate中,可以直接對Blob、Clob作映射。

DDL如下:

1.CREATE TABLE user (
2. id INT(11) NOT NULL auto_increment PRIMARY KEY,
3. name VARCHAR(100) NOT NULL default '',
4. age INT,
5. photo BLOB,
6. resume TEXT
7.);

注意:由於BLOB字段大小不可預計,有時需要增大BLOB字段的大小,可在數據庫中改用MEDIUMBLOB甚至LONGBLOB。

User.java

1.package com.hb3.pack_07.model;
2.
3.import java.sql.Blob;
4.import java.sql.Clob;
5. 6.public class User {
7. 8. private Integer id;
9. private String name;
10. private Integer age;
11. private Blob photo;
12. private Clob resume;
13.
14. public User() {
15. }
16. 17. public Integer getId() {
18. return id;
19. }
20. public void setId(Integer id) {
21. this.id = id;
22. }
23. public String getName() {
24. return name;
25. }
26. public void setName(String name) {
27. this.name = name;
28. }
29. public Integer getAge() {
30. return age;
31. }
32. public void setAge(Integer age) {
33. this.age = age;
34. }
35. public Blob getPhoto() {
36. return photo;
37. }
38. public void setPhoto(Blob photo) {
39. this.photo = photo;
40. }
41. public Clob getResume() {
42. return resume;
43. }
44. public void setResume(Clob resume) {
45. this.resume = resume;
46. }
47.}

User.hbm.xml文件:

1.<?xml version="1.0" encoding="UTF-8"?>
2. 3.<!DOCTYPE hibernate-mapping PUBLIC
4. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 6. 7.<hibernate-mapping> 8. 9. <class name="com.hb3.pack_07.model.User" table="user"> 10.
11. <id name="id" column="id" type="java.lang.Integer">
12. <generator class="native"/>
13. </id>
14.
15. <property name="name" column="name" type="java.lang.String"/>
16.
17. <property name="age" column="age" type="java.lang.Integer"/>
18.
19. <property name="photo" column="photo" type="java.sql.Blob"/>
20.
21. <property name="resume" column="resume" type="java.sql.Clob"/>
22.
23. </class> 24.
25.</hibernate-mapping>

修改hibernate.cfg.xml文件如下:

......
<mapping resource="com/hb3/pack_07/model/User.hbm.xml" />
......

測試代碼如下:

1.package com.hb3.pack_07;
2.
3.import java.io.FileInputStream;
4.import java.io.FileOutputStream;
5.import java.io.IOException;
6.import java.io.InputStream;
7.import java.sql.Blob;
8.import java.sql.Clob;
9.import java.sql.SQLException;
10. 11.import org.hibernate.Hibernate;
12.import org.hibernate.Session;
13.import org.hibernate.SessionFactory;
14.import org.hibernate.Transaction;
15.import org.hibernate.cfg.Configuration;
16. 17.import com.hb3.pack_07.model.User;
18. 19.public class BusinessService {
20. 21. public static void main(String[] args) throws IOException, SQLException {
22.
23. Configuration config = new Configuration().configure();
24. SessionFactory sessionFactory = config.buildSessionFactory();
25. Session session = sessionFactory.openSession();
26. 27. FileInputStream fileInputStream = new FileInputStream("c:\\sunset.jpg");
28. Blob photo = Hibernate.createBlob(fileInputStream);
29. Clob resume = Hibernate.createClob("Bla....Bla....resume text!!");
30.
31. User user = new User();
32. user.setName("shenbin");
33. user.setAge(new Integer(28));
34. user.setPhoto(photo);
35. user.setResume(resume);
36. 37. Transaction tx = session.beginTransaction();
38. session.save(user);
39. tx.commit();
40.
41. session.close();
42. session = sessionFactory.openSession();
43.
44. user = (User) session.load(User.class, new Integer(1));
45. System.out.print(user.getAge() + "\t" +
46. user.getName() + "\t");
47. String str_resume = user.getResume().getSubString(1, (int) user.getResume().length());
48. System.out.println(str_resume);
49. InputStream inputStream = user.getPhoto().getBinaryStream();
50. FileOutputStream fileOutputStream = new FileOutputStream("c:\\sunset_save.jpg");
51. byte[] buf = new byte[1];
52. int len = 0;
53. while((len = inputStream.read(buf)) != -1) {
54. fileOutputStream.write(buf, 0, len);
55. }
56. inputStream.close();
57. fileOutputStream.close();
58. System.out.println("save photo to c:\\sunset_save.jpg");
59. 60. session.close();
61. sessionFactory.close();
62. }
63.}

在MySQL中對BLOB以及CLOB類型的使用還是比較簡單的,如果在Oracle DB中則相對復雜一些,計劃在以後章節加以說明。

可先參閱:http://blog.csdn.net/fhway/archive/2006/11/01/1359482.aspx

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved