Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/JPA/Advanced JPA Development/Customizers"

(DescriptorCustomizer)
(SessionCustomizer example)
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{EclipseLink_UserGuide
 
{{EclipseLink_UserGuide
 
|info=y
 
|info=y
|toc=n
+
|toc=y
 
|eclipselink=y
 
|eclipselink=y
 
|eclipselinktype=JPA
 
|eclipselinktype=JPA
Line 14: Line 14:
  
 
EclipseLink provides two types of customizers:
 
EclipseLink provides two types of customizers:
* DescriptorCustomizer : defines an interface to customize the mapping meta-data for a class.
+
* <code>DescriptorCustomizer</code> : defines an interface to customize the mapping meta-data for a class.
* SessionCustomizer : defines an interface to customize the meta-data for a persistence unit, or a set of its classes.
+
* <code>SessionCustomizer</code> : defines an interface to customize the meta-data for a persistence unit, or a set of its classes.
  
 
==DescriptorCustomizer==
 
==DescriptorCustomizer==
Line 42: Line 42:
 
}
 
}
 
</source>
 
</source>
 +
 +
 +
==SessionCustomizer==
 +
<code>SessionCustomizer</code> is an interface that allows the customization of the persistence unit meta-data through Java code.  The interface defines a single method, <code>customize(Session)</code>.  The <code>Session</code> argument represents the meta-data for a persistence unit.  The actually session will be an instance of either <code>ServerSession</code> for most applications, or an <code>SessionBroker</code> for composite persistence units.
 +
 +
A SessionCustomizer can be set as a persistence unit property in the <code>persistence.xml</code> file or persistence unit properties map.
 +
 +
====SessionCustomizer example====
 +
<source lang="java">
 +
public class MySessionCustomizer implements SessionCustomizer {
 +
  public void customize(Session session) {
 +
    session.getLogin().setConnector(new JNDIConnector(MyDataSourceManager.getDataSource());
 +
 +
    for (ClassDescriptor descriptor : session.getDescriptors().values()) {
 +
      descriptor.getCachePolicy().setIdentityMapSize(1000);
 +
    }
 +
  }
 +
}
 +
</source>
 +
 +
<source lang="xml">
 +
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_2_0.xsd" version="2.0">
 +
    <persistence-unit name="acme" transaction-type="RESOURCE_LOCAL">
 +
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
 +
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
 +
        <properties>
 +
            <property name="eclipselink.session.customizer" value="com.acme.MySessionCustomizer"/>
 +
        </properties>
 +
    </persistence-unit>
 +
</persistence>
 +
</source>
 +
 +
<br/>
 +
 
{{EclipseLink_JPA
 
{{EclipseLink_JPA
|previous=[[EclipseLink/UserGuide/JPA/Advanced JPA Development/NoSQL|Supported Data-sources]]
+
|previous=[[EclipseLink/UserGuide/JPA/Advanced JPA Development/NoSQL/Supported Data-sources|Supported Data-sources]]
 
|next=[[EclipseLink/UserGuide/JPA/Advanced JPA Development/Schema Generation|Schema Generation]]  
 
|next=[[EclipseLink/UserGuide/JPA/Advanced JPA Development/Schema Generation|Schema Generation]]  
 
|up=[[EclipseLink/UserGuide/JPA/Advanced JPA Development|Advanced JPA Development]]
 
|up=[[EclipseLink/UserGuide/JPA/Advanced JPA Development|Advanced JPA Development]]
 
|version=2.4.0 DRAFT}}
 
|version=2.4.0 DRAFT}}

Revision as of 14:36, 3 May 2012

EclipseLink JPA

Customizers

EclipseLink allows extended configuration of the persistence unit meta-data through Java code. EclipseLink provides a descriptor and mapping API that allows its meta-data to be accessed and configured through Java code. EclipseLink also provides a session API that allows the persistence unit meta-data to be accessed and configured through Jacva code. The APIs allows access to certain extended features not provided through annotations or XML. They also allow usage of custom extensions, allow configurations that are only possible through code, and allow easier configuration of defaults and computed configurations.

EclipseLink provides two types of customizers:

  • DescriptorCustomizer : defines an interface to customize the mapping meta-data for a class.
  • SessionCustomizer : defines an interface to customize the meta-data for a persistence unit, or a set of its classes.

DescriptorCustomizer

DescriptorCustomizer is an interface that allows the customization of a class' meta-data through Java code. The interface defines a single method, customize(ClassDescriptor). The ClassDescriptor argument represents the meta-data for a class. The actually descriptor will be an instance of either RelationalDescriptor for relational data, or an EISDescriptor for NoSQL data.

A DescriptorCustomizer can be set on an Entity or Embeddable class using the @Customizer annotation or <customizer> XML element.

DescriptorCustomizer example

public class MyCustomizer implements DescriptorCustomizer {
  public void customize(ClassDescriptor descriptor) {
    DirectToFieldMapping genderMapping = (DirectToFieldMapping)descriptor.getMappingForAttributeName("gender");
    ObjectTypeConverter converter = new ObjectTypeConverter();
    convert.addConversionValue("M", Gender.MALE);
    convert.addConversionValue("F", Gender.FEMALE);
    genderMapping.setConverter(converter);
  }
}
@Entity
@Customizer(MyCustomizer.class)
public class Employee {
 ...
}


SessionCustomizer

SessionCustomizer is an interface that allows the customization of the persistence unit meta-data through Java code. The interface defines a single method, customize(Session). The Session argument represents the meta-data for a persistence unit. The actually session will be an instance of either ServerSession for most applications, or an SessionBroker for composite persistence units.

A SessionCustomizer can be set as a persistence unit property in the persistence.xml file or persistence unit properties map.

SessionCustomizer example

public class MySessionCustomizer implements SessionCustomizer {
  public void customize(Session session) {
    session.getLogin().setConnector(new JNDIConnector(MyDataSourceManager.getDataSource());
 
    for (ClassDescriptor descriptor : session.getDescriptors().values()) {
      descriptor.getCachePolicy().setIdentityMapSize(1000);
    }
  }
}
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_2_0.xsd" version="2.0">
    <persistence-unit name="acme" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.session.customizer" value="com.acme.MySessionCustomizer"/>
        </properties>
    </persistence-unit>
</persistence>


Eclipselink-logo.gif
Version: 2.4.0 DRAFT
Other versions...