Hibernate:如何設置自定義 UserType 的值 (Hibernate: How to set the value of a custom UserType)


問題描述

Hibernate:如何設置自定義 UserType 的值 (Hibernate: How to set the value of a custom UserType)

I've got a ClobType that extends Hibernate's UserType and does about what you'd expect:  converts the clob found in the database to a String.  I mapped my clob columns to the ClobType, and those columns render nicely when I access them in my JSP pages.

But now that I'm trying to set the value, I'm stumped.  Because I can write ${myObject.longField} in my JSP pages tells me I can probably say 

String s = myObject.getLongField().toString();

and get the string, despite the fact that LongField is of type ClobType.

What I don't know is how to use the setter.  I can instantiate a ClobType easily, but how do I give it string data that it needs to persist as a blob?

ClobType clobType = new ClobType();
String s = "... a really REALLY long string ...";
/* a miracle occurs */
myObject.setLongField( clobType );
myObjectDAO.saveOrUpdate( myObject );

What is the miracle I need in order to give the ClobType a string value?

Or, is there a better (or more proper) way of having my two setters return Strings rather than ClobTypes?  Is there a special mapping that does this?  My business logic has no need to know that a given column is implemented as a blob.   

‑‑‑‑‑

參考解法

方法 1:

I think if you're using a reasonably current hibernate, you can map a java String to a database CLOB without using a custom UserType at all.

Try

@Lob
public String getLongField() { return longField; }

with longField just a String.

If this works, it'll be much simpler than what you're doing.

(by MarvoDon Roby)

參考文件

  1. Hibernate: How to set the value of a custom UserType (CC BY‑SA 3.0/4.0)

#blob #usertype #hibernate-mapping #hibernate #clob






相關問題

Hibernate:如何設置自定義 UserType 的值 (Hibernate: How to set the value of a custom UserType)

從數據庫中讀取 zip 存檔 (Reading zip archive from database)

從 JSON 網絡服務檢索 android 中的 byte[] (Retrieve byte[] in android from JSON webservice)

ORA-01465: 使用 BLOB 時,oracle 中的十六進制數無效 (ORA-01465: invalid hex number in oracle while using BLOB)

無法使用 fopen/fwrite/fclose (php) 在 mysql 中保存 xml 文件內容 (Can't save xml file content in mysql with fopen/fwrite/fclose (php))

將 Azure Blob 中的數據流式傳輸回客戶端 (Streaming Data from Azure Blobs Back to Client)

我希望將畫布保存為 mySql blob 字段中的 blob (I am looking to save a canvas as a blob in mySql blob field)

如何在 SQLite 中分段更新 blob? (How to update piecewise a blob in SQLite?)

YugabyteDB 中是否像 Oracle 或 Postgres 那樣支持 BLOB? (Is there support for BLOBs in YugabyteDB like in Oracle or Postgres?)

使用顫振將圖像作為blob存儲在mysql數據庫中 (Storing image's as blob in mysql database with flutter)

如何從 docxtemplater.js 輸出 docx 以在 jszip 中輸入 (How can I output a docx from docxtemplater.js for input in jszip)

不同語言的 Blob 請求 (Blob request in different language)







留言討論