3

How to persist XMLType column via JPA/Hibernate? As per oracle documentation, there are two ways in declaring storage clause for XMLType. They are, LOB and Object-Relational. I dont want to go with LOB. I have schema and register into database. I have not have example on how to design my Entity for XMLType. Does any one know please share it?

3 Answers 3

2

This is working fine for me (JPA 2.1 and Hibernate 5.1.0):

  • Create the field in the entity as a String.
  • Use the @ColumnTransformer annotation from Hibernate to define how to read and write from the column.
  • Set the columnDefinition in @Column as "XMLType"

    @ColumnTransformer(read = "to_clob(columnName)", write = "?")
    @Column(name = "COLUMN_NAME", columnDefinition = "XMLType")
    private String columnName;

Sign up to request clarification or add additional context in comments.

1 Comment

This fails if over 4,000 characters.
1

I would use a custom UserType (Hibernate extension). The blog post Hibernate with Oracle XmlType provides an implementation that you might reuse. Then, declare your custom UserType with the Type annotation.

3 Comments

Thanks. But I got this below SQL exception when I try to persist through JPA entitymanager. I have done exactly what you provided in your blogs >created hibernate usertype class >entity pointing to @Type("HibernateXMLType") >Document as field SQL Exception ------------- java.sql.SQLException: Could not convert Document to String for storage
@Moorthy: Please update your question with the problem, it's unreadable in the comment box.
The oracle xml parser xmlparserv2.jar is causing the issue. If I remove it, HibernateXMLType class throws the below exception from the line xmlType = XMLType.createXML(st.getConnection(),..) java.lang.NoClassDefFoundError: oracle/xml/parser/v2/XMLParseException at com.tutorial.hibernate.HibernateXMLType.nullSafeSet(HibernateXMLType.java:112) I know this parser conflict with javax.xml..parser Please throw some light on this how to resolve it.
0

The custom java class HibernateXMLType works fine for CLOB.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.