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
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;
1 Comment
Michael Munsey
This fails if over 4,000 characters.
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
Moorthy
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
Pascal Thivent
@Moorthy: Please update your question with the problem, it's unreadable in the comment box.
Moorthy
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.