Java Persistence API (JPA) Entities

Similar documents
Domain-Driven Design Activity

Module 8 The Java Persistence API

The class Object. Lecture CS1122 Summer 2008

Distributed Systems Recitation 1. Tamim Jabban

Introduction to JPA. Fabio Falcinelli

Distributed Systems Recitation 1. Tamim Jabban

JPA Entities. Course Multi Tier Business Applications with Java EE. Prof. Dr. Eric Dubuis Berner Fachhochschule Biel. Berner Fachhochschule

Polymorphism. return a.doublevalue() + b.doublevalue();

C12a: The Object Superclass and Selected Methods

EJB 3 Entities. Course Multi Tier Business Applications with Java EE. Prof. Dr. Eric Dubuis Berner Fachhochschule Biel. Berner Fachhochschule

CMP 436/774. Introduction to Java Enterprise Edition. Java Enterprise Edition

Java EE Architecture, Part Three. Java EE architecture, part three 1(69)

Entities are classes that need to be persisted, usually in a relational database. In this chapter we cover the following topics:

Developing Applications with Java EE 6 on WebLogic Server 12c

Painless Persistence. Some guidelines for creating persistent Java applications that work

CO Java EE 6: Develop Database Applications with JPA

EJB 3 Entity Relationships

TECHNICAL WHITEPAPER. Performance Evaluation Java Collections Framework. Performance Evaluation Java Collections. Technical Whitepaper.

Java EE Architecture, Part Three. Java EE architecture, part three 1(57)

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Canonical Form. No argument constructor Object Equality String representation Cloning Serialization Hashing. Software Engineering

Inheritance (Part 5) Odds and ends

Table of Contents EJB 3.1 and JPA 2

Introduction to Session beans. EJB - continued

Holon Platform JPA Module - Reference manual. Version 5.2.1

Java: advanced object-oriented features

Java Classes, Inheritance, and Interfaces

JPA. Java persistence API

COM1020/COM6101: Further Java Programming

JPA - ENTITY MANAGERS

Table of Contents. I. Pre-Requisites A. Audience B. Pre-Requisites. II. Introduction A. The Problem B. Overview C. History

EJB 3 Entity Relationships

Introduction to Java Enterprise Edition For Database Application Developer

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #4. Review: Methods / Code

CS2110: Software Development Methods. Maps and Sets in Java

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Chapter 11: Collections and Maps

PIC 20A Number, Autoboxing, and Unboxing

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Assignment 2 (Solution)

Teneo: Integrating EMF & EclipseLink

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Courses For Event Java Advanced Summer Training 2018

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Thu 10/26/2017. Created RESTful Web Service, JavaDB, Java Persistence API, Glassfish server in NetBeans 8

Integrating Spring Boot with MySQL

Object-Relational Mapping is NOT serialization! You can perform queries on each field!

JPA The New Enterprise Persistence Standard

WHAT IS EJB. Security. life cycle management.

INHERITANCE. Spring 2019

Holon Platform JPA Datastore Module - Reference manual. Version 5.2.1

COE318 Lecture Notes Week 9 (Week of Oct 29, 2012)

Java Magistère BFA

COE318 Lecture Notes Week 8 (Oct 24, 2011)

JSR-303 Bean Validation. Emmanuel Bernard Doer JBoss, a Division of Red Hat

Creating an Immutable Class. Based on slides by Prof. Burton Ma

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

CS 3410 Ch 20 Hash Tables

Object-Relational Mapping

JAVA WRAPPER CLASSES

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Exam Questions 1Z0-895

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

Java SE7 Fundamentals

How to be a Good Bean

Java EE Architecture, Part Three. Java EE architecture, part three 1(24)

<Insert Picture Here> Productive JavaEE 5.0 Development

public static boolean isoutside(int min, int max, int value)

Java Object Model. Or, way down the rabbit hole

SSE3052: Embedded Systems Practice

Oracle Exam 1z0-898 Java EE 6 Java Persistence API Developer Certified Expert Exam Version: 8.0 [ Total Questions: 33 ]

Model Driven Architecture with Java

CS2110: Software Development Methods. Maps and Sets in Java

Objects and Classes. Lecture 10 of TDA 540 (Objektorienterad Programmering) Chalmers University of Technology Gothenburg University Fall 2017

Copyright Descriptor Systems, Course materials may not be reproduced in whole or in part without prior written consent of Joel Barnum

Topics in Enterprise Information Management

Web Application Development Using Spring, Hibernate and JPA

(a) Write the signature (visibility, name, parameters, types) of the method(s) required

Metadata driven component development. using Beanlet

Oracle EXAM - 1Z Java EE 6 Enterprise JavaBeans Developer Certified Expert Exam. Buy Full Product.

Produced by. Agile Software Development. Eamonn de Leastar

The 1st Java professional open source Convention Israel 2006

Produced by. Agile Software Development. Eamonn de Leastar

Fundamental Java Methods

Abstract Classes and Interfaces

Dali Java Persistence Tools. User Guide Release for Eclipse

Java Fundamentals (II)

11 HashMap: Overriding equals ; JUnit; Vistors

CS 61B, Spring 1999 MT3 Professor M. Clancy

Rest Client for MicroProfile. John D. Ament

Fall 2017 Mentoring 9: October 23, Min-Heapify This. Level order, bubbling up. Level order, bubbling down. Reverse level order, bubbling up

Hashing. Reading: L&C 17.1, 17.3 Eck Programming Course CL I

1) Discuss the mutual exclusion mechanism that you choose as implemented in the chosen language and the associated basic syntax

Tutorials for Struts, EJB, xdoclet and eclipse.

ECE 122. Engineering Problem Solving with Java

Ch 7 Designing Java Classes & Class structure. Fields have data values define/describe an instance

Transcription:

Java Persistence API (JPA) Entities

JPA Entities JPA Entity is simple (POJO) Java class satisfying requirements of JavaBeans specification Setters and getters must conform to strict form Every entity must have primary key Annotation @Id marks the property that will be used as the entitie s primary key Entities may have simple business logic methods These methods should operate only with entities internal fields Complex business logic should be implemented in dedicated business components Java Persistence API - Entities 2

Example of an entity @Entity public class Account { @Id // The primary key @GeneratedValue(strategy=GenerationType.IDENTITY) private int accountnumber; @Column(name = "OWNER_NAME", nullable="false") private String ownername; // Persistent property private int balance; // Persistent too private transient int tmpvariable; // Not persistent! // Entities must have a public no-arg constructor public Account() {} // setters and getters; better with Lombok @Getter/@Setter... // Deposit a given amount (simple business method) public void deposit(int amount) { balance += amount; } } Java Persistence API - Entities 3

JPA requirements for entity classes JPA requires only two pieces of metadata when you are creating a persistent class: the @javax.persistence.entity annotation denotes that the class should be mapped to your database, the @javax.persistence.id annotation marks which property in your class will be used as the primary key. The persistence provider will assume that all other properties in your class will map to a column of the same name and of the same type. The table name will default to the unqualified name of the bean class Java Persistence API - Entities 4

Additional Requirements (Best Practices) Every independent entity additionally to JPA requirements should define: 1. business key 2. methods equals() and hashcode() that operate with business key 3. field annotated with @Version used for optimistic locking What is an indepenent entity? Life cycle of independent entity doesn t depend upon other entities Student and Univertity are independent entities Entity Study journal is dependent upon entity Student study journal cannot exist without a student Java Persistence API - Entities 5

Business Key Primary key is used by RDBMS to guarantee referential integrity of data its value must not be meaningful (for example: 32213523) it shouldn t be shown in user interface Conversely, business key is a meaningful property (maybe composite property) of the entity that uniquely identifies the entity among other entities in some domain Entity may have multiple business keys, for example, entity Student might have these business keys: student s number student s social security number student s personal code Java Persistence API - Entities 6

Business Key What properties of an entity comprise business key decide domain experts and/or logical data model designers In relational databases business keys will be: marked as NOT NULL covered by unique index Examples: Entity University university s title Entity Student student s number Entity Course course code Java Persistence API - Entities 7

Methods equals() and hashcode() These methods are used by Java Collections API Set, HashSet, List, ArrayList, Map, etc. There are three choices for each entity: 1. these methods are absent; 2. we leave the methods generated by IDE (NetBeans / Eclipse) such methods use entity s primary key (property annotated with @Id) 3. we override these methods and make them use only entity s business key Java Persistence API - Entities 8

Comparison of three choices https://developer.jboss.org/wiki/equalsandhashcode May be used in composite key? Multiple new instances in set? Equal to same entity from other EntityManager? Collections intact after saving to DB? Without methods equals() and hashcode() Methods use only @Id property Methods use business key No Yes Yes Yes No Yes No Yes Yes Yes No Yes Java Persistence API - Entities 9

Requirements Summary Every entity must have: Primary Key (@Id) Business Key This property will have unique index (NOT NULL, UNIQUE) in database equals() and hashcode() methods that use only business key Field annotated with @Version used for optimistic locking Java Persistence API - Entities 10

@Entity public class University implements Serializable { @Id @GeneratedValue(strategy= GenerationType.IDENTITY) private Integer id; private String title; @Version private int optlockversion; Example @Override public boolean equals(object obj) { if (obj == null) return false; if (getclass()!= obj.getclass()) return false; final University other = (University) obj;... get and set methods... @Override public int hashcode() { int hash = 3; hash = 59 * hash + (this.title!= null? this.title.hashcode() : 0); return hash; } } } if ((this.title == null)? (other.title!= null) :!this.title.equals( other.title)) { return false; } return true; Java Persistence API - Entities 11

Example with Lombok @Entity @Getter @Setter @EqualsAndHashCode(of={"title"}) public class University implements Serializable { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; @Column(name = "TITLE", nullable="false") private String title; @Column(name = "OPT_LOCK_VERSION") @Version private int optlockversion; } Java Persistence API - Entities 12