在 Hibernate 中将 String 转换为 Clob,反之亦然
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16641758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert String to Clob and vice versa in Hibernate
提问by hoang nguyen
Suppose that I have a Class:
假设我有一个类:
class EventTransaction {
.....
private Clob dataXML;
public Clob getDataXML() {
return dataXML;
}
public void setDataXML(Clob dataXML) {
this.dataXML = dataXML;
}
}
And Hibernate mapping xml:
和 Hibernate 映射 xml:
<property name="dataXML" type="java.sql.Clob">
<column name="XML" sql-type="CLOB"/>
</property>
In java code, how to I convert a String to Clob and vice versa to save into to the database:
在java代码中,如何将String转换为Clob,反之亦然以保存到数据库中:
Ex: EventTransaction et = new EventTransaction();
String xml = "fdfsafafafa";
et.setDataXML(convertStringToClob(xml));
HibernateTemplate.saveOrUpdate(et);
Could you please help how to implement function convertStringToClob(String data);
请您帮忙如何实现函数convertStringToClob(String data);
Thanks,
谢谢,
回答by Amir Pashazadeh
Do this
做这个
@Column(name='xml')
@Lob
private String dataXML;
public String getDataXML() {
return dataXML;
}
public void setDataXML(String dataXML) {
this.dataXML = dataXML;
}
So there is no need to convert, and everything is done by Hibernate.
所以不需要转换,一切都由Hibernate完成。
I showed it using annotations, the same thing can be done using .hbm.xml
files.
我使用注释展示了它,同样的事情可以使用.hbm.xml
文件来完成。
回答by george_h
Here is code I made a long time ago to convert a Clob to a String. It's meant to be used in a utility class.
这是我很久以前制作的将 Clob 转换为字符串的代码。它旨在用于实用程序类。
public static String convertClobToString(Clob clob) throws IOException, SQLException {
Reader reader = clob.getCharacterStream();
int c = -1;
StringBuilder sb = new StringBuilder();
while((c = reader.read()) != -1) {
sb.append(((char)c));
}
return sb.toString();
}
And if I am not mistaken, to create a Clob you would do something like this
如果我没记错的话,要创建一个 Clob,你会做这样的事情
Clob myClobFile = new SerialClob("my string".toCharArray());
回答by Taoufik Mohdit
The limitation of 64000 characters is on the database side when you declare the XML column as VARCHAR (and not on Java String), so as long as your column XML is a CLOB, it should work.
当您将 XML 列声明为 VARCHAR(而不是在 Java String 上)时,64000 个字符的限制是在数据库端,因此只要您的列 XML 是 CLOB,它就应该可以工作。
Excerpt from working code:
工作代码摘录:
Entity:
实体:
private String xml;
SQL (ORACLE):
SQL(甲骨文):
XML CLOB,
Hibernate mapping:
休眠映射:
<property name="xml" type="java.lang.String">
<column name="XML" length="999999" />
</property>
If you want to store the XML as a file, then you should rather use BLOBs as shown below :
如果要将 XML 存储为文件,则应该使用 BLOB,如下所示:
Entity:
实体:
private byte[] xmlFile;
SQL (ORACLE):
SQL(甲骨文):
XML BLOB,
Hibernate mapping:
休眠映射:
<property name="xmlFile" type="java.io.File">
<column name="XML" />
</property>