Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/UserGuide/JPA/Advanced JPA Development/Customizers"
(→Customizers) |
(→DescriptorCustomizer) |
||
Line 18: | Line 18: | ||
==DescriptorCustomizer== | ==DescriptorCustomizer== | ||
− | DescriptorCustomizer is an interface that allows the customization of a class' meta-data through Java code. The interface defines a single <code>customize(ClassDescriptor)</code> | + | <code>DescriptorCustomizer</code> is an interface that allows the customization of a class' meta-data through Java code. The interface defines a single method, <code>customize(ClassDescriptor)</code>. The <code>ClassDescriptor</code> argument represents the meta-data for a class. The actually descriptor will be an instance of either <code>RelationalDescriptor</code> for relational data, or an <code>EISDescriptor</code> for NoSQL data. |
+ | A DescriptorCustomizer can be set on an Entity or Embeddable class using the <code>@Customizer</code> annotation or <code><customizer></code> XML element. | ||
+ | |||
+ | ====DescriptorCustomizer example==== | ||
+ | <source lang="java"> | ||
+ | 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); | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | <source lang="java"> | ||
+ | @Entity | ||
+ | @Customizer(MyCustomizer.class) | ||
+ | public class Employee { | ||
+ | ... | ||
+ | } | ||
+ | </source> | ||
{{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]] |
Revision as of 14:13, 3 May 2012
EclipseLink JPA
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Native API
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 { ... }