Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 242
kazanaki Avatar asked Apr 14 '10 11:04

kazanaki


People also ask

How define CLOB in hibernate?

Databases use the data types BLOB (binary large object) and CLOB (character large object) to store large objects, like images and very long texts. JPA and Hibernate provide two kinds of mappings for these types. You can choose if you want to: Materialize the LOB and map it to a byte[] or a String.

How does hibernate implement JPA?

Hibernate is an implementation of JPA. Hence, the common standard which is given by JPA is followed by Hibernate. It is a standard API that permits to perform database operations. It is used in mapping Java data types with SQL data types and database tables.


1 Answers

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.

like image 118
ewernli Avatar answered Oct 18 '22 18:10

ewernli