1 / 14

EclipseLink JPA Black Belt Course

EclipseLink JPA Black Belt Course. Section 3: Advanced Mapping. EclipseLink JPA - Mappings. Common Mapping options Relationship Mappings Inheritance Optimistic Locking. BasicMap & BasicCollection.

Télécharger la présentation

EclipseLink JPA Black Belt Course

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EclipseLink JPABlack Belt Course Section 3: Advanced Mapping

  2. EclipseLink JPA - Mappings • Common Mapping options • Relationship Mappings • Inheritance • Optimistic Locking

  3. 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”

  4. BasicCollection Database Tables EMPLOYEE FK relationship RESPONS Primary keys

  5. 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;

  6. 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: StringDATE 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

  7. 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”

  8. 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, ; }

  9. 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

  10. 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

  11. 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{

  12. 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;

  13. 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

  14. 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

More Related