Java 如何以与实现无关的方式在 JPA 中创建 Clob
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2637063/
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
How to create a Clob in JPA in an implementation agnostic way
提问by kazanaki
I am using Ejb3 and JPA (based on Hibernate and Oracle 10g at the moment)
我正在使用 Ejb3 和 JPA(目前基于 Hibernate 和 Oracle 10g)
I have an entity that contains a clob
我有一个包含 clob 的实体
@Entity
@Table(name = "My_TAB")
public class ExampleEntity implements java.io.Serializable {
private Clob someText;
public void setSomeText(Clob someText) {
this.someText= someText;
}
@Column(name = "COLUMN_NAME")
public Clob getSomeText() {
return this.someText;
}
Then I want to save an entity of this type.
然后我想保存这种类型的实体。
At the moment I am doing the following which works perfectly
目前我正在做以下完美的工作
ExampleEntity exampleEntity = new ExampleEntity();
exampleEntity.setSomeText(Hibernate.createClob(aStringValue));
someOtherDao.save(exampleEntity);
However this ties my code to Hibernate! I have specifically avoided so far Hibernate extensions and used only JPA annotations. The code works because indeed Hibernate is my current implementation.
然而,这将我的代码与 Hibernate 联系起来!到目前为止,我已经特别避免使用 Hibernate 扩展并且只使用 JPA 注释。该代码有效,因为确实 Hibernate 是我当前的实现。
Is there some sort of JPA API that allows me to create a clob in a generic way? So if later I decide to switch to Toplink/EclipseLink or something else I won't have to change a thing?
是否有某种 JPA API 允许我以通用方式创建 Clob?因此,如果以后我决定切换到 Toplink/EclipseLink 或其他什么,我将不必更改任何内容?
采纳答案by ewernli
There's such an example is the JPA spec (§ 9.1.5)
有这样一个例子是 JPA 规范(第 9.1.5 节)
@Column(name="DESC",
columnDefinition="CLOB NOT NULL",
table="EMP_DETAIL")
@Lob
public String getDescription() { return description; }
I believe it's the standard way for CLOB.
我相信这是 CLOB 的标准方式。
回答by Brian Deterling
I'm not sure I would do it again, but in the past when I needed to limit my app to the most widely used subset of sql types, I implemented binary objects using a separate table of char and stored it gzipped and base 64 encoded. Using XML mapping, it was something like:
我不确定我是否会再次这样做,但是在过去,当我需要将我的应用程序限制为最广泛使用的 sql 类型子集时,我使用单独的 char 表实现了二进制对象并将其存储为 gzipped 和 base 64 编码. 使用 XML 映射,它类似于:
<list name="encodedValue" lazy="true" table="TABLE" cascade="all-delete-orphan">
<key column="TABLE_ID"/>
<index column="SEQ"/>
<element type="string" column="LINE" length="2000"/>
</list>
In the code, the getValue method retrieved the getEncodedValue results, concatenated them all together and then decoded and unzipped them. As an optimization, I put a simple value column on the parent table and used that if it could fit in the 2000 characters and only went to the child table if necessary.
在代码中,getValue 方法检索 getEncodedValue 结果,将它们连接在一起,然后对其进行解码和解压缩。作为优化,我在父表上放置了一个简单的值列,如果它可以容纳 2000 个字符,则使用它,并且仅在必要时才转到子表。
The setValue method gzipped and encoded it and stored it in the simple column if it fit, otherwise split it into the child records. That also gets you lazy loading and if the data fits in a single column, it doesn't even have to do a separate query.
setValue 方法对其进行压缩和编码,如果合适,则将其存储在简单列中,否则将其拆分为子记录。这也会让您延迟加载,如果数据适合单个列,它甚至不必执行单独的查询。
Probably overkill if you know that your databases will support clobs, but worked pretty well in our situation.
如果您知道您的数据库将支持 clobs,但在我们的情况下工作得很好,那么您可能会矫枉过正。