SlideShare ist ein Scribd-Unternehmen logo
1 von 34
Downloaden Sie, um offline zu lesen
Mapping Objects With JPA
         Java Persistence API 2.0
         Aaron Schram
         University of Colorado at Boulder




Thursday, March 31, 2011
Me
               PhD Candidate at the University of Colorado
               Prior to returning to CU I held several software
               engineering positions
                     Mocapay, Inc. (Mobile Payments)
                     Rally Software Development (Agile Tooling)
                     BEA Systems (Weblogic Portal, Now Oracle)
                     Lockheed Martin (IS & GS)


Thursday, March 31, 2011
Some History




Thursday, March 31, 2011
History
               A result of the JSR 317 Expert Group
                     Members included
                           Sun Microsystems, Inc.
                           Oracle
                           BEA Systems*
                           IBM
                           VMWare


Thursday, March 31, 2011
History Cont...

               Developed as a replacement for EJB 2 entity beans
               Version 2.0 was released Dec 10th, 2009
               Covers 2 areas of Object Relational Mapping (ORM)
                     Object relational metadata
                     Java Persistence Query Language (JPQL)




Thursday, March 31, 2011
History Cont...
               JPA 2.0 included consensus approval for new features
                     Expanded ORM functionality
                     Criteria query API
                     Standardization of query hints
                     Standardization of metadata for DDL generation
                     Validation support



Thursday, March 31, 2011
It’s Just A Specification*
               JPA is a specification used to detail what a reference
               provider should conform to when providing ORM
               functionality
                     It’s actually more than just a specification
                     A finalized Java Specification Request will include a
                     reference implementation
                     Since JPA is a finalized JSR an implementation is
                     provided
               There are many JPA reference implementations
                     Hibernate, EclipseLink, OpenJPA
Thursday, March 31, 2011
Hibernate

               The most popular JPA vendor is Hibernate (JBoss)
               JPA 1.0 was heavily influenced by Gavin King, the
               creator of Hibernate
                     Much of what exists in JPA is adopted directly from
                     the Hibernate project
                     Many key concepts such as mapping syntax and
                     central session/entity management exist in both



Thursday, March 31, 2011
Key Concepts
               JPA utilizes annotated Plain Old Java Objects (POJOs)
                     Define an EntityBean for persistence
                           @Entity
                     Define relationships between beans
                           @OneToOne
                           @OneToMany
                           @ManyToOne
                           @ManyToMany

Thursday, March 31, 2011
Key Concepts Cont...
               Primitive types and wrappers are mapped by default
                     String, Long, Integers, Double, etc.
               Mappings can be defined on instance vars or on
               accessor methods of the POJO
               Supports inheritance and embedding
               EntityManger is used to manage the state and life cycle
               of all entities within a give persistence context
               Primary keys are generated and accessed via @Id
               annotation

Thursday, March 31, 2011
An Example




Thursday, March 31, 2011
Office-Employees Example
               This was a common interview question at one
               of my previous employers




Thursday, March 31, 2011
Question:
         How could you model an employee management
         system using an ORM?
Thursday, March 31, 2011
In the interview we
         Question Details                                would build the
                                                        whole application
               Design an application that allows a customer to view all
               employees that physically reside in a specific office
               Each employee may only reside in one office
               Employees must have
                     First name, last name, phone number, id
               Each office must have
                                                     Here, we’ll just build
                     Name, postal address, id         out the model tier
               Any ORM will do, we’ll use JPA...

Thursday, March 31, 2011
The Model




Thursday, March 31, 2011
Thursday, March 31, 2011
From Model to Code
               Our model contains four classes
                     Office
                     Employee
                     DomainObject
                     PostalAddress
               Office and Employee inherit from DomainObject
               DomainObject holds on to best practice attributes such
               as id, creation date, modified date, version, etc.
Thursday, March 31, 2011
From 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 Employee
               We’ll show a use of @Embedded and @Embeddable
               for the Office-PostalAddress relationship
               As well as inheritance using @MappedSuperclass



Thursday, March 31, 2011
DomainObject




Thursday, March 31, 2011
This class is not to
      be directly persisted
           DB generated Id

    For optimistic locking


         Store as datetime
       Call these methods
       before creation and
           modification


Thursday, March 31, 2011
Office




Thursday, March 31, 2011
Eligible for
                   persistence

           Embed
     PostalAddress in the
     same table as Office




Thursday, March 31, 2011
PostalAddress




Thursday, March 31, 2011
Allow this object to
        be embedded by
          other objects




        State is an Enum
       that will be treated
      as a String (varchar)


Thursday, March 31, 2011
Employee




Thursday, March 31, 2011
Eligible for
                   persistence




     Defines the many to
     one association with
            Office


Thursday, March 31, 2011
Explanation
               @Embeddable and @Embedded
                     Allows for the attributes of an embedded class to be
                     stored in the same table as the embedding class
               @Enumerated
                     Allows for the value of an Enum to be stored in a
                     column in the class’s database table
               @MappedSuperclass
                     Allows for all attributes of the superclass to be
                     utilized by the subclasses
                     Duplicates all superclass attributes on subclass
                     tables
Thursday, March 31, 2011
The Database




Thursday, March 31, 2011
The Database
                     JPA is capable of generating the underlying database
                     for the developer
                     Most aspects of the generation are available for
                     customization
                           The defaults are generally good enough
                     Any @Entity causes the generation of a database
                     table. Our generated tables are:
                             Office table
                             Employee table

Thursday, March 31, 2011
Office Table
                               Field         Type
                                 id       bigint(20)
                            createDate     datetime
                           modifiedDate     datetime
                              version       int(11)
                               name      varchar(255)
                           addressOne    varchar(255)
                           addressTwo    varchar(255)
                                city     varchar(255)
                               state     varchar(255)
                             zipCode     varchar(255)
Thursday, March 31, 2011
Employee Table
                                Field        Type
                                  id      bigint(20)
                            createDate     datetime
                           modifiedDate     datetime
                               version      int(11)
                             firstName    varchar(255)
                             lastName    varchar(255)
     FK to                    location   varchar(255)
     Office
                           phoneNumber   varchar(255)
                              office_id    bigint(20)
Thursday, March 31, 2011
Take Aways




Thursday, March 31, 2011
Take Aways
                     JPA is a specification that a developer can code to in
                     order to easily leverage ORM technologies
                     There are a wide variety of vendors that implement
                     the specification
                           Coding to the spec allows the developer to be
                           flexible in their choice of vendor implementations
                           with limited ripple throughout the codebase
                     JPA greatly simplifies persistence of POJOs through
                     a small set of easily utilized annotations

Thursday, March 31, 2011
Questions?
         Aaron Schram
         aaron.schram@colorado.edu
Thursday, March 31, 2011

Weitere ähnliche Inhalte

Ähnlich wie Mapping Java Objects with JPA

Node js techtalksto
Node js techtalkstoNode js techtalksto
Node js techtalkstoJason Diller
 
Flowdock's full-text search with MongoDB
Flowdock's full-text search with MongoDBFlowdock's full-text search with MongoDB
Flowdock's full-text search with MongoDBFlowdock
 
Entity framework introduction sesion-1
Entity framework introduction   sesion-1Entity framework introduction   sesion-1
Entity framework introduction sesion-1Usama Nada
 
2011 july-gtug-high-replication-datastore
2011 july-gtug-high-replication-datastore2011 july-gtug-high-replication-datastore
2011 july-gtug-high-replication-datastoreikailan
 
NeXML - phylogenetic data as XML
NeXML - phylogenetic data as XMLNeXML - phylogenetic data as XML
NeXML - phylogenetic data as XMLRutger Vos
 
Alfresco xenit webinar fred boosting your desktop - 9 march 2011
Alfresco xenit webinar fred boosting your desktop - 9 march 2011Alfresco xenit webinar fred boosting your desktop - 9 march 2011
Alfresco xenit webinar fred boosting your desktop - 9 march 2011Alfresco Software
 
01 persistence and domain modeling
01 persistence and domain modeling01 persistence and domain modeling
01 persistence and domain modelingthirumuru2012
 
06 association of value types
06 association of value types06 association of value types
06 association of value typesthirumuru2012
 
Lucene Bootcamp -1
Lucene Bootcamp -1 Lucene Bootcamp -1
Lucene Bootcamp -1 GokulD
 
Creation of a Test Bed Environment for Core Java Applications using White Box...
Creation of a Test Bed Environment for Core Java Applications using White Box...Creation of a Test Bed Environment for Core Java Applications using White Box...
Creation of a Test Bed Environment for Core Java Applications using White Box...cscpconf
 
Developing Actors in Azure with .net
Developing Actors in Azure with .netDeveloping Actors in Azure with .net
Developing Actors in Azure with .netMarco Parenzan
 
Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)David McCarter
 
Building a Testable Data Access Layer
Building a Testable Data Access LayerBuilding a Testable Data Access Layer
Building a Testable Data Access LayerTodd Anglin
 

Ähnlich wie Mapping Java Objects with JPA (20)

NeXML
NeXMLNeXML
NeXML
 
Node js techtalksto
Node js techtalkstoNode js techtalksto
Node js techtalksto
 
Flowdock's full-text search with MongoDB
Flowdock's full-text search with MongoDBFlowdock's full-text search with MongoDB
Flowdock's full-text search with MongoDB
 
Entity framework introduction sesion-1
Entity framework introduction   sesion-1Entity framework introduction   sesion-1
Entity framework introduction sesion-1
 
2011 july-gtug-high-replication-datastore
2011 july-gtug-high-replication-datastore2011 july-gtug-high-replication-datastore
2011 july-gtug-high-replication-datastore
 
NeXML - phylogenetic data as XML
NeXML - phylogenetic data as XMLNeXML - phylogenetic data as XML
NeXML - phylogenetic data as XML
 
Alfresco xenit webinar fred boosting your desktop - 9 march 2011
Alfresco xenit webinar fred boosting your desktop - 9 march 2011Alfresco xenit webinar fred boosting your desktop - 9 march 2011
Alfresco xenit webinar fred boosting your desktop - 9 march 2011
 
Hibernate
HibernateHibernate
Hibernate
 
01 persistence and domain modeling
01 persistence and domain modeling01 persistence and domain modeling
01 persistence and domain modeling
 
Hibernate 3
Hibernate 3Hibernate 3
Hibernate 3
 
Entity Framework
Entity FrameworkEntity Framework
Entity Framework
 
06 association of value types
06 association of value types06 association of value types
06 association of value types
 
Lucene Bootcamp -1
Lucene Bootcamp -1 Lucene Bootcamp -1
Lucene Bootcamp -1
 
Anarchist guide to titanium ui
Anarchist guide to titanium uiAnarchist guide to titanium ui
Anarchist guide to titanium ui
 
G0361034038
G0361034038G0361034038
G0361034038
 
M1803058993
M1803058993M1803058993
M1803058993
 
Creation of a Test Bed Environment for Core Java Applications using White Box...
Creation of a Test Bed Environment for Core Java Applications using White Box...Creation of a Test Bed Environment for Core Java Applications using White Box...
Creation of a Test Bed Environment for Core Java Applications using White Box...
 
Developing Actors in Azure with .net
Developing Actors in Azure with .netDeveloping Actors in Azure with .net
Developing Actors in Azure with .net
 
Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)
 
Building a Testable Data Access Layer
Building a Testable Data Access LayerBuilding a Testable Data Access Layer
Building a Testable Data Access Layer
 

Kürzlich hochgeladen

So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 

Kürzlich hochgeladen (20)

So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 

Mapping Java Objects with JPA

  • 1. Mapping Objects With JPA Java Persistence API 2.0 Aaron Schram University of Colorado at Boulder Thursday, March 31, 2011
  • 2. Me PhD Candidate at the University of Colorado Prior to returning to CU I held several software engineering positions Mocapay, Inc. (Mobile Payments) Rally Software Development (Agile Tooling) BEA Systems (Weblogic Portal, Now Oracle) Lockheed Martin (IS & GS) Thursday, March 31, 2011
  • 4. History A result of the JSR 317 Expert Group Members included Sun Microsystems, Inc. Oracle BEA Systems* IBM VMWare Thursday, March 31, 2011
  • 5. History Cont... Developed as a replacement for EJB 2 entity beans Version 2.0 was released Dec 10th, 2009 Covers 2 areas of Object Relational Mapping (ORM) Object relational metadata Java Persistence Query Language (JPQL) Thursday, March 31, 2011
  • 6. History Cont... JPA 2.0 included consensus approval for new features Expanded ORM functionality Criteria query API Standardization of query hints Standardization of metadata for DDL generation Validation support Thursday, March 31, 2011
  • 7. It’s Just A Specification* JPA is a specification used to detail what a reference provider should conform to when providing ORM functionality It’s actually more than just a specification A finalized Java Specification Request will include a reference implementation Since JPA is a finalized JSR an implementation is provided There are many JPA reference implementations Hibernate, EclipseLink, OpenJPA Thursday, March 31, 2011
  • 8. Hibernate The most popular JPA vendor is Hibernate (JBoss) JPA 1.0 was heavily influenced by Gavin King, the creator of Hibernate Much of what exists in JPA is adopted directly from the Hibernate project Many key concepts such as mapping syntax and central session/entity management exist in both Thursday, March 31, 2011
  • 9. Key Concepts JPA utilizes annotated Plain Old Java Objects (POJOs) Define an EntityBean for persistence @Entity Define relationships between beans @OneToOne @OneToMany @ManyToOne @ManyToMany Thursday, March 31, 2011
  • 10. Key Concepts Cont... Primitive types and wrappers are mapped by default String, Long, Integers, Double, etc. Mappings can be defined on instance vars or on accessor methods of the POJO Supports inheritance and embedding EntityManger is used to manage the state and life cycle of all entities within a give persistence context Primary keys are generated and accessed via @Id annotation Thursday, March 31, 2011
  • 12. Office-Employees Example This was a common interview question at one of my previous employers Thursday, March 31, 2011
  • 13. Question: How could you model an employee management system using an ORM? Thursday, March 31, 2011
  • 14. In the interview we Question Details would build the whole application Design an application that allows a customer to view all employees that physically reside in a specific office Each employee may only reside in one office Employees must have First name, last name, phone number, id Each office must have Here, we’ll just build Name, postal address, id out the model tier Any ORM will do, we’ll use JPA... Thursday, March 31, 2011
  • 17. From Model to Code Our model contains four classes Office Employee DomainObject PostalAddress Office and Employee inherit from DomainObject DomainObject holds on to best practice attributes such as id, creation date, modified date, version, etc. Thursday, March 31, 2011
  • 18. From 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 Employee We’ll show a use of @Embedded and @Embeddable for the Office-PostalAddress relationship As well as inheritance using @MappedSuperclass Thursday, March 31, 2011
  • 20. This class is not to be directly persisted DB generated Id For optimistic locking Store as datetime Call these methods before creation and modification Thursday, March 31, 2011
  • 22. Eligible for persistence Embed PostalAddress in the same table as Office Thursday, March 31, 2011
  • 24. Allow this object to be embedded by other objects State is an Enum that will be treated as a String (varchar) Thursday, March 31, 2011
  • 26. Eligible for persistence Defines the many to one association with Office Thursday, March 31, 2011
  • 27. Explanation @Embeddable and @Embedded Allows for the attributes of an embedded class to be stored in the same table as the embedding class @Enumerated Allows for the value of an Enum to be stored in a column in the class’s database table @MappedSuperclass Allows for all attributes of the superclass to be utilized by the subclasses Duplicates all superclass attributes on subclass tables Thursday, March 31, 2011
  • 29. The Database JPA is capable of generating the underlying database for the developer Most aspects of the generation are available for customization The defaults are generally good enough Any @Entity causes the generation of a database table. Our generated tables are: Office table Employee table Thursday, March 31, 2011
  • 30. Office Table Field Type id bigint(20) createDate datetime modifiedDate datetime version int(11) name varchar(255) addressOne varchar(255) addressTwo varchar(255) city varchar(255) state varchar(255) zipCode varchar(255) Thursday, March 31, 2011
  • 31. Employee Table Field Type id bigint(20) createDate datetime modifiedDate datetime version int(11) firstName varchar(255) lastName varchar(255) FK to location varchar(255) Office phoneNumber varchar(255) office_id bigint(20) Thursday, March 31, 2011
  • 33. Take Aways JPA is a specification that a developer can code to in order to easily leverage ORM technologies There are a wide variety of vendors that implement the specification Coding to the spec allows the developer to be flexible in their choice of vendor implementations with limited ripple throughout the codebase JPA greatly simplifies persistence of POJOs through a small set of easily utilized annotations Thursday, March 31, 2011
  • 34. Questions? Aaron Schram aaron.schram@colorado.edu Thursday, March 31, 2011