How to create a Clob in JPA in an implementation agnostic way

kazanaki picture kazanaki · Apr 14, 2010 · Viewed 65.3k times · Source

I am using Ejb3 and JPA (based on Hibernate and Oracle 10g at the moment)

I have an entity that contains a 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.

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?

Answer

ewernli picture ewernli · Apr 14, 2010

There's such an example is the JPA spec (§ 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.