140 likes | 278 Views
EclipseLink JPA Black Belt Course. Section 3: Advanced Mapping. EclipseLink JPA - Mappings. Common Mapping options Relationship Mappings Inheritance Optimistic Locking. BasicMap & BasicCollection.
E N D
EclipseLink JPABlack Belt Course Section 3: Advanced Mapping
EclipseLink JPA - Mappings • Common Mapping options • Relationship Mappings • Inheritance • Optimistic Locking
BasicMap & BasicCollection • @BasicMap - stores a collection of key-value pairs of simple types, such as String, Number, Date, etc., in a single table • @BasicCollection - stores a collection of simple types • Example: Responsibility list on Employee class “Code” Employee id-=7 “Documentation” responsibilities “Test” “Social Events”
BasicCollection Database Tables EMPLOYEE FK relationship RESPONS Primary keys
Converters • New converter mappings for type conversion and user defined types include: • @Converter • @TypeConverter • @ObjectTypeConverter • @StructConverter • @Convert @Entity@Converter( name="Currency", converterClass=CurrencyConverter.class)publicclassEmployeeimplementsSerializable{@Convert("Currency")privateCurrencysalaryCurrency;
joe: Employee Integer id; Date joinDate; String name; Type Converter • Converts from a Java type to specific database type • For example: String NUMERIC in the database • For example: StringDATE in the database • Actual value is not transformed, just the type EMP_ID J_DAY Name 2661 Jeb ‘11/13/95’ ‘04/01/93’ Jake 2662 java.util.Date java.sql.Date Mindy ‘08/01/96’ 2663
ObjectType Converter • Used to match a fixed number of database primitive values to Java objects • Set up a hashtable of keys and values, e.g. • “M” = “Male” • “F” = “Female”
ObjectType Converter Example @ObjectTypeConverter(name="gender-converter",objectType = model.Gender.class,conversionValues = {@ConversionValue(dataValue="M", objectValue = "Male"),@ConversionValue(dataValue="F", objectValue = "Female")}) @Entitypublicclass Employee { …@Convert("gender-converter")private Gender gender = Gender.Male; publicenum Gender { Female, Male, ; }
Transformation Mapping • Used when: • No other mappings are appropriate (extremely flexible) • The user has complete control over how data gets written to and read from database rows • The user must provide several methods to define a transformation mapping • Given raw database row, return a value to populate the attribute in question • One separate method for every database field to be populated • Each method returns a value to populate the field, given the object to be written
Transformation Mapping Example • Attribute value is a sum of database fields • Attribute value is an array of fields Attribute transformation method/class Employee class Vector normalHours; S_TIME EMP_ID E_TIME String firstName; Integer id; Field transformation method/class 2661 07:00 15:30 2662 07:00 15:30 Field transformation method/class 2663 15:30 24:00
Customizers • Use the @Customizer EclipseLink annotation to register a class that can customize the mappings for an Entity using the EclipseLink native API. @Entity@Customizer(EmployeeCustomizer.class)publicclassEmployeeimplementsSerializable{... publicclassEmployeeCustomizer implementsDescriptorCustomizer{publicvoidcustomize(ClassDescriptorDescriptor) throwsException{
Returning Policy • Return policy options can be configured using: • @ReturnInsert • @ReturnUpdate • On the Oracle Database, specifying an @ReturnInsert or @ReturnUpdate will add a RETURNING clause to insert and update statements to efficiently reread modified data. • On databases other than Oracle, the underlying ReturningPolicy can be configured using a descriptor customizer. @EntitypublicclassEmployeeimplementsSerializable{...@ReturnInsert@ReturnUpdateprivateStringfirstName;
Other Mappings • Other advanced mappings supported by TopLink 11g include: • @PrivateOwned - supports orphan management • @JoinFetch - enables the joining and reading of a referenced object(s) in the same query as the source object • @Mutable - indicates that the value of a complex field itself can be changed or not changed (instead of being replaced) • @VariableOneToOne - supports OneToOne mappings to an interface rather than an Entity • @ReadOnly - makes an Entity read only
EclipseLink JPA Mappings Summary • JPA 1.0 covers the bulk of what is needed • + Additional EclipseLink Mappings and options • Mapping extensions are useful but require native API or XML • JPA 2.0 will address several common extensions • Element Collection/Map to handle BasicCollection/Map • Map with key and values being embeddable, basic, or entity • @MapsId to avoid redundant id fields • Basically standardizing common extensions