Working with Oracle XMLType and JPA over Weblogic

I’ve been working with the object type of Oracle databases for XML for a long time, but always in the context of PL/SQL programming. Last week,  I developed a Java EE module that made use of JPA entities, supported by tables with SYS.XMLType columns, so I’d like to share my findings with an example.

First of all, here you have the creation sentence of a table that offers persistence to a simple messaging interface:


create table messages
  (
    message_id     number  constraint message_pk primary key,
    reception_date date    not null,
    msg_content    xmltype not null 
                           constraint message_msg_content_ck check(
                              xmlisvalid(msg_content) = 1)
  )
  xmltype column msg_content store as clob xmlschema
  "http://testdomain.com/Messages.xsd"
  element "Messages";

This is an excerpt of a JPA entity that could represent the former table:


package com.wordpress.fcosfc.test.messaging;

...

import org.eclipse.persistence.annotations.Customizer;

@Entity
@Table(name = "MESSAGES")
@Customizer(XMLTypeAttributeCustomizer.class)
public class Messages implements Serializable {

    @Id
    @Column(name = "MESSAGE_ID", nullable = false)
    private BigDecimal messageId;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "RECEPTION_DATE", nullable = false)
    private Date receptionDate;

    @Lob
    @Column(nullable = false, name ="msg_content")    
    private String msgContent;

    ...
}

I’m sure you’ve realized that there is an special annotation Customizer, which makes reference to this class:


package com.wordpress.fcosfc.test.messaging;

import org.eclipse.persistence.config.DescriptorCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.mappings.xdb.DirectToXMLTypeMapping;

public class XMLTypeAttributeCustomizer implements DescriptorCustomizer {

    @Override
    public void customize(final ClassDescriptor descriptor) throws Exception {
        descriptor.removeMappingForAttributeName("msg_content");
        final DirectToXMLTypeMapping mapping = new DirectToXMLTypeMapping();
        mapping.setAttributeName("msg_content");
        mapping.setFieldName("MSG_CONTENT");
        mapping.getField().setColumnDefinition("sys.XMLTYPE");
        descriptor.addMapping(mapping);
    }

}

This class has a method that changes the default mapping of the EclipseLink JPA provider in order to use the proper type from Oracle: SYS.XMLType.

Finally, I’d like to point out that the first time I deployed my module over an Oracle Weblogic 10.3.5 application server I got a ClassNotFoundException: oracle/xdb/xmltype , so I had to extend my Weblogic domain in order to support Oracle XMType. Here you have my reference about how to do it, I just followed the instructions to support Oracle XDB.


References

Advertisements

2 Comments on “Working with Oracle XMLType and JPA over Weblogic”

  1. wmontoyav@yahoo.com says:

    Funny this code actually does not work, when a new instance of DirectToXMLTypeMapping is created field is null, so mapping.getField().setColumnDefinition(“sys.XMLTYPE”); will throw nulpointer exception. but because customize method throws Exception there is no error message and Eclipselink deploys fine, in that case it takes the default value that is mapping to varchar(2000). Well that was eclipselink did in my case.

    • fcosfc says:

      Hi there,

      I’ve been reviewing the code and there is a bug. The column definition must be:

      
      @Lob
      @Column(nullable = false, name ="msg_content")    
      private String msgContent;
      

      I’m afraid, I simply copied my original code and changed the names of the columns. Thank you very much for your comment.

      Regards,

      Paco.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s