hibernate oracle blob資料類型的處理

來源:互聯網
上載者:User

hibernate對blob的儲存和讀取比較特殊,不能像平常處理資料那樣進行操作,下面是hibernate中對oracle blob類型的處理的例子:

oracle資料庫建表語句

create table stu
(
   id number(2),
   name varchar2(16),
   filename varchar2(64),
   filedata BLOB,
   primary key(id)
);

Stu.java檔案

public class Stu implements java.io.Serializable {

 // Fields

 private Byte id;
 private String name;
 private String filename;
 private byte[]  filedata;  //此處理將其屬性類型設為byte[]

 // Constructors

 /** default constructor */
 public Stu() {
 }

剩餘部分略去.....

Stu.hbm.xml檔案如下:

<hibernate-mapping>
    <class name="com.aoyou.mapping.Stu" table="STU" schema="LIXIN03080">
        <id name="id" type="java.lang.Byte">
            <column name="ID" precision="2" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" length="16" />
        </property>
        <property name="filename" type="java.lang.String">
            <column name="FILENAME" length="64" />
        </property>

<hibernate-mapping>
    <class name="com.aoyou.mapping.Stu" table="STU" schema="LIXIN03080">
        <id name="id" type="java.lang.Byte">
            <column name="ID" precision="2" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" length="16" />
        </property>
        <property name="filename" type="java.lang.String">
            <column name="FILENAME" length="64" />
        </property>
        <!-- 注意下面filedata的type為binary -->
        <property name="filedata" type="binary">
            <column name="FILEDATA" />
        </property>
    </class>
</hibernate-mapping>
        <property name="filedata" type="binary">
            <column name="FILEDATA" />
        </property>
    </class>
</hibernate-mapping>

在main方法中測試:

儲存資料到資料庫:

public static void main(String[] args) throws IOException{
         Session session = new Configuration().configure().buildSessionFactory().openSession();
        Transaction t = session.beginTransaction();
  
        File file = new File("D:\\購物網站\\二期\\資料庫設計.txt");
       BufferedReader br = new BufferedReader(new FileReader(file));
  
  
       Stu stu = new Stu();
       stu.setId(2);
       stu.setName("mary");
       stu.setFilename("資料清單");
  
       String content = "";
      String temp = "";
      while((temp = br.readLine()) != null){
            content += temp;
       }
       br.close();
       stu.setFiledata(content.getBytes());
       session.save(stu);
  
       t.commit();
 }

讀取資料庫資料:

public static void main(String[] args) throws IOException{
           Session session = new Configuration().configure().buildSessionFactory().openSession();
           Transaction t = session.beginTransaction();
  
           String hql = " from Stu  ";
           Query query = session.createQuery(hql);
           List list = query.list();
           if(list != null){
                   Stu stu = (Stu)list.get(0);
                  System.out.println(stu.getId());
                  System.out.println(stu.getName());
                  System.out.println(stu.getFilename());
                  byte[] b = stu.getFiledata();
                 System.out.print(new   String(b,   0,   b.length));
           }
  
       t.commit();
 }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.