DOC PREVIEW
CU-Boulder CSCI 5448 - Mapping Objects With JPA

This preview shows page 1-2-16-17-18-33-34 out of 34 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Mapping Objects With JPAJava Persistence API 2.0Aaron SchramUniversity of Colorado at BoulderThursday, March 31, 2011MePhD Candidate at the University of ColoradoPrior to returning to CU I held several software engineering positionsMocapay, Inc. (Mobile Payments)Rally Software Development (Agile Tooling)BEA Systems (Weblogic Portal, Now Oracle)Lockheed Martin (IS & GS)Thursday, March 31, 2011Some HistoryThursday, March 31, 2011HistoryA result of the JSR 317 Expert GroupMembers includedSun Microsystems, Inc.OracleBEA Systems*IBMVMWareThursday, March 31, 2011History Cont...Developed as a replacement for EJB 2 entity beansVersion 2.0 was released Dec 10th, 2009Covers 2 areas of Object Relational Mapping (ORM)Object relational metadataJava Persistence Query Language (JPQL)Thursday, March 31, 2011History Cont...JPA 2.0 included consensus approval for new features Expanded ORM functionalityCriteria query APIStandardization of query hintsStandardization of metadata for DDL generationValidation supportThursday, March 31, 2011It’s Just A Specification*JPA is a specification used to detail what a reference provider should conform to when providing ORM functionalityIt’s actually more than just a specificationA finalized Java Specification Request will include a reference implementationSince JPA is a finalized JSR an implementation is providedThere are many JPA reference implementationsHibernate, EclipseLink, OpenJPAThursday, March 31, 2011HibernateThe most popular JPA vendor is Hibernate (JBoss)JPA 1.0 was heavily influenced by Gavin King, the creator of HibernateMuch of what exists in JPA is adopted directly from the Hibernate projectMany key concepts such as mapping syntax and central session/entity management exist in bothThursday, March 31, 2011Key ConceptsJPA utilizes annotated Plain Old Java Objects (POJOs)Define an EntityBean for persistence@EntityDefine relationships between beans@OneToOne@OneToMany@ManyToOne@ManyToManyThursday, March 31, 2011Key Concepts Cont...Primitive types and wrappers are mapped by defaultString, Long, Integers, Double, etc.Mappings can be defined on instance vars or on accessor methods of the POJOSupports inheritance and embeddingEntityManger is used to manage the state and life cycle of all entities within a give persistence contextPrimary keys are generated and accessed via @Id annotationThursday, March 31, 2011An ExampleThursday, March 31, 2011Office-Employees ExampleThis was a common interview question at one of my previous employersThursday, March 31, 2011Question:How could you model an employee management system using an ORM?Thursday, March 31, 2011Question DetailsDesign an application that allows a customer to view all employees that physically reside in a specific officeEach employee may only reside in one officeEmployees must haveFirst name, last name, phone number, idEach office must haveName, postal address, idAny ORM will do, we’ll use JPA...In the interview we would build the whole application Here, we’ll just build out the model tierThursday, March 31, 2011The ModelThursday, March 31, 2011Thursday, March 31, 2011From Model to CodeOur model contains four classesOfficeEmployeeDomainObjectPostalAddressOffice and Employee inherit from DomainObjectDomainObject holds on to best practice attributes such as id, creation date, modified date, version, etc.Thursday, March 31, 2011From Model to Code Cont...@Entity must be used to tell JPA which classes are eligible for persistence@ManyToOne must be used to tell JPA there is an aggregation between Office and EmployeeWe’ll show a use of @Embedded and @Embeddable for the Office-PostalAddress relationshipAs well as inheritance using @MappedSuperclassThursday, March 31, 2011DomainObjectThursday, March 31, 2011This class is not to be directly persistedDB generated IdFor optimistic lockingStore as datetimeCall these methods before creation and modificationThursday, March 31, 2011OfficeThursday, March 31, 2011Eligible for persistenceEmbed PostalAddress in the same table as OfficeThursday, March 31, 2011PostalAddressThursday, March 31, 2011Allow this object to be embedded by other objectsState is an Enum that will be treated as a String (varchar)Thursday, March 31, 2011EmployeeThursday, March 31, 2011Defines the many to one association with OfficeEligible for persistenceThursday, March 31, 2011Explanation@Embeddable and @EmbeddedAllows for the attributes of an embedded class to be stored in the same table as the embedding class@EnumeratedAllows for the value of an Enum to be stored in a column in the class’s database table@MappedSuperclassAllows for all attributes of the superclass to be utilized by the subclassesDuplicates all superclass attributes on subclass tablesThursday, March 31, 2011The DatabaseThursday, March 31, 2011The DatabaseJPA is capable of generating the underlying database for the developerMost aspects of the generation are available for customizationThe defaults are generally good enoughAny @Entity causes the generation of a database table. Our generated tables are:Office tableEmployee table Thursday, March 31, 2011Office TableFieldTypeidbigint(20)createDatedatetimemodifiedDatedatetimeversionint(11)namevarchar(255)addressOnevarchar(255)addressTwovarchar(255)cityvarchar(255)statevarchar(255)zipCodevarchar(255)Thursday, March 31, 2011Employee TableFieldTypeidbigint(20)createDatedatetimemodifiedDatedatetimeversionint(11)firstNamevarchar(255)lastNamevarchar(255)locationvarchar(255)phoneNumbervarchar(255)office_idbigint(20)FK to OfficeThursday, March 31, 2011Take AwaysThursday, March 31, 2011Take AwaysJPA is a specification that a developer can code to in order to easily leverage ORM technologiesThere are a wide variety of vendors that implement the specificationCoding to the spec allows the developer to be flexible in their choice of vendor implementations with limited ripple throughout the codebaseJPA greatly simplifies persistence of POJOs through a small set of easily utilized annotationsThursday, March 31, 2011Questions?Aaron [email protected], March 31,


View Full Document

CU-Boulder CSCI 5448 - Mapping Objects With JPA

Documents in this Course
Django

Django

42 pages

ENRS

ENRS

30 pages

PhoneGap

PhoneGap

22 pages

Load more
Download Mapping Objects With JPA
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Mapping Objects With JPA and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Mapping Objects With JPA 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?