Giter Site home page Giter Site logo

week4-orm-jpa's Introduction

#ORM (Object Relational Mapping) with JPA (Java Persistence API) ##Step by step:
Purpose: Simple walk through of JPA Steps:

  1. Create the project.
  2. Create a database and connect (for this demo I use mysql database: jpa1)
  3. Add the MYSQL JDBC Driver to the project libraries
  4. Create a new Persistance Unit (right click project and choose new..-> Persistence ...)
  • choose EclipseLink JPA 2.1 (Should be the default choice for the Persistence Provider)
  1. Create new file -> Entity Class.
  • Name it Person. and give it fields (ID, age, name)
  • Run this class to create a db table from the entities:
public class Creater {
    public static void main(String[] args) {
        Persistence.generateSchema("JPADemo1PU", null); //Change PU name
    }
}
  • Then create a class with an EntityManager based on the Persistence Unit See how running the persist() method creates a new table in the database called person with ID, Age and Name.
  • To get the created SQL from EclipseLink insert the following 2 property lines in to the persistence.xml. :

Resources:
Tutorialspoint on JPA Simple JPA
More detailed JPA - this one provides detailed information

Day 1: Introduction to ORM

Tutorial for JPA

  • Java Persistence API is a source to store business entities (java objects) as relational entities (db tables)

  • Challenges:

    • Table structure in db vs graph structure in objects (references containing references ...)
    • Inheritance: Objects with inheritance hierarchy to be mirrored in tables
  • JPA providers:

    • Hybernate
    • EclipseLink
    • Toplink
  • JPA architecture: javax.Persistence !alt text

  • Annotations

  • Persistence.xml

    • registers the database and specify the entity class
  • Getting started: set up the first project.

###Exercise 1 - Basic JPA Find the exercise here

##Day 2 - More JPA

  • JPA mapping directions
Cardinality Direction Example
One-to-one Unidirectional person and car
One-to-one Bidirectional person and spouse
One-to-many Unidirectional workplace and work station
Many-to-one/one-to-many Bidirectional student and teacher
Many-to-one Unidirectional student and school
Many-to-many Unidirectional person and phone
Many-to-many Bidirectional employee and company car
  • In relational database it is always a unidirectional relationship based on foreign keys
  • In OO Programming Objects can have reference to each other
//In the Student class
private Teacher teacher;
//In the Teacher class
private List<Student> students;

###Exercise 2 - Relationships Find the exercise here

##Day 3 - JPA and Inheritance

  • The 3 strategies for mapping inheritance in database tables

###Exercise 3 - Inheritance Find the exercise here

##Day 4 - JPQL

  • Java Persitence Query Language
  • Query the managed objects using the Entity Manager
  • Like SQL
  • Query Objects and fields instead of tables and collumns

###Exercise 4 - JPQL Find the exercise here

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.