Hibernate Annotation 李日貴 (jini) jakarta99 AT gmail.com SoftLeader Tech. Corp. Taiwan Java Annotation Lesson 1.

Slides:



Advertisements
Similar presentations
Persistence Jim Briggs 1. 2 Database connectivity: JDBC Java Database Connectivity An API for connecting Java programs (applications, applets and servlets)
Advertisements

WaveMaker Visual AJAX Studio 4.0 Training
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Access Lecture 1 Database Overview and Creating Tables Create an Employee Table.
Session-01. Hibernate Framework ? Why we use Hibernate ?
Word Up! Using Lucene for full-text search of your data set.
The Java Persistence API Edel Sherratt. Contents Revisit applications programming Using Java Persistence API.
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
Object Persistence and Object-Relational Mapping James Brucker.
Hibernatification! Roadmap for Migrating from Plain Old SQL on JDBC to JPA on Hibernate Duke Banerjee Senior Developer, DrillingInfo.com.
Java Persistence API Maciej Adamiak. Agenda -Entity, -Entity Operations, -Query Language.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Enterprise Object Framework. What is EOF? Enterprise Objects Framework is a set of tools and resources that help you create applications that work with.
Training - Day 3 OJB. What is OR Mapping? OR Mapping is the mapping of relational database tables to objects (Java Objects in our case) Many OR Mapping.
Maven for building Java applications By Nalin De Zoysa
1 The Seven Relationship Types Seven types of relationships can exist between entity beans. There are four types of cardinality: one-to- one, one-to-many,
JPA Java Persistence API. Introduction The Java Persistence API provides an object/relational mapping facility for managing relational data in Java applications.
Part 06 – A More Complex Data Model Entity Framework and MVC NTPCUG Tom Perkins.
IS-907 Java EE JPA: Simple Object-Relational Mapping.
Introduction to Entities
Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Chapter 4 Introduction to MySQL. MySQL “the world’s most popular open-source database application” “commonly used with PHP”
CS 157B: Database Management Systems II February 4 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
Topic : JPA Kaster Nurmukan. Overview of JPA EntityManager.
OK lets start, who am I ?. Nayden Gochev (a.k.a. JOKe) Java Spring Android Hybris GWT EJB JSF RMI JAX-RS Struts JMS JPA Hibernate C# ASP.NET TestStudio.
1 Mapping Persistent Objects Entities represent data in the database, so changes to an entity bean result in changes to the database. That's ultimately.
EJB 3.0 Persistence Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
Intoduction to NHibernate. Agenda Overview of NHibernate Models and Mappings Configuration Sessions and Transactions Queries.
Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.
Topic : Hibernate 2: Object Persistence and ORM Kaster Nurmukan.
(1) Introduction to Models using the Play Framework Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University.
Hibernate Introduction - 1 Present by Eric Yu. BeanSoft | 2 Content Understanding object/relational persistence Introduction Architecture The core interfaces.
Java Data Persistence Using Hibernate Jack Gardner October 2004.
Object relational database managmement systems (ORDBMS) Adapted by Edel Sherratt from originals by Nigel Hardy.
A U.S. Department of Energy Office of Science Laboratory Operated by The University of Chicago Argonne National Laboratory Office of Science U.S. Department.
Middleware Technology (J2EE/EJB) Entity Bean (JBoss EJB 3.0 tutorial)
Topic : Hibernate 3:Advanced ORM Kaster Nurmukan.
Visual Programing SQL Overview Section 1.
Intoduction to NHibernate. Agenda Overview of NHibernate Models and Mappings Configuration Sessions and Transactions Queries.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
Hibernate Basics. Basics What is persistence? Persistence in Object Oriented Applications? Paradigm Mismatch.  The Problem of Granularity.  The Problem.
The Java Persistence API ©SoftMoore ConsultingSlide 1.
Java Persistence API part 1 INFORMATICS ENGINEERING – UNIVERSITY OF BRAWIJAYA Eriq Muhammad Adams J
Java Persistence API (JPA) Relationships. Kinds of relationships UML associations and aggregations (ER non- identifying relationships, shared semantics)
Mapping Objects ↔Relational DB. The Problem with Databases Databases store data in rows in tables, which are not like objects. We can simulate object.
Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February
JPA The New Enterprise Persistence Standard Mike Keith
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
JPA in Vaadin CSCI 3130 Winter What is JPA?  Java Persistence API  Allows for “easy” storage of Java Objects  Is a type of Object Relational.
Hibernate Thuy, Le Huu. Pentalog VN. Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic.
Creating E/R Diagrams with SQL Server Management Studio, Writing SQL Queries D0ncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer.
CS422 Principles of Database Systems Object-Relational Mapping (ORM) Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA (I) Chengyu Sun California State University, Los Angeles.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
Object-Relational Mapping. Contents  Default Mapping Rules  Elementary Mapping  Embeddables  Relationship Mapping  Inheritance Mapping.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA Chengyu Sun California State University, Los Angeles.
CS6320 – Java Persistence API
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Advanced Java Programming
Hibernate (JPA) Code First Entity Relations
Database Applications (15-415) ORM - Part I Lecture 11, February 11, 2018 Mohammad Hammoud.
JPA: Mapping to an existing database
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
JDBC II IS
Presentation transcript:

Hibernate Annotation 李日貴 (jini) jakarta99 AT gmail.com SoftLeader Tech. Corp. Taiwan Java Annotation Lesson 1

Hibernate Introduction Opensources –LGPL ( you can use in commercial ) Object / Relational mappings Object-oriented Query Language Transparent persistence Automatic primary key generation Cache architecture High Performance

Hibernate Stacks Hibernate Core Hibernate Annotations Hibernate EntityManager Hibernate Shards Hibernate Validator Hibernate Search

Now.. Make Dev-Env. Download from hibernate.org –hibernate Core –hibernate annotations Build dev IDE ( with JavaSE ) –Eclipse (suggestion) –NetBeans Connect the Database –Hibernate supports lots of DBs. –

Setup your Database Postgresql – MySQL – Oracle MS-SQL Server IBM DB2

Dependency Libs hibernate3.jar ehache.jar c3p0.jar hibernate-annotation.jar slf4j*, log4j.. JDBC- postgresql.jar others..

Create the Test Database Open pgAdminIII and Login it Create new database “testdb” Create a SEQ ‘seq_student’ Create new Table Create table student ( id bigint PRIMARY KEY DEFAULT NEXTVAL('seq_student'), sno character varying(10), name character varying(10), sex character(1), regdate timestamp )

Build a Hello World Connect DB by Hibernate –hibernate.cfg.xml Write a Student Persistence Object –Student.java Write a Student hibernate mapping def. –Student.hbm.xml Call DAO by JUnit for printing “Hello ”+student.getName();

Practice I Try to make a teacher Entity teacher columns –id bigint –name character varying(50) –subject character varying(200)

Annotation Driven Modify configuration for annotation –Mapping to the classes Focus on your Persistence Object

Define public class Student implements Serializable { … }

Mapping simple –To specify a field or property of an entity that is not –TemporalType ( DATE, TIME, TIMESTAMP –Assumes that for a property or field mapped to an enumerated ( hibernate only )

@Column ( name=“columnName”, boolean unique() default false; boolean nullable() default true; boolean insertable() default true; boolean updatable() default true; String columnDefinition() default “”; String table() default “”; int length() default 255; int precision() default 0; // decimal precision int scale() default 0; ) // to generate create-table sql

@Embedded Objects Main public class Student impelements Serializable { ( …. ) Country bornIn; } Embedded Class public class Address implements Serializable {..

No annotations ? Single type Component Serializable Clob or Blob

Composited public class Book public String getBookName() public String getAuthorName() {…} public int getPages() {…} public BookSize getSize() {…} public int getWeight() {…} …. }

public class BookPK implements Serializable { public String getBookName() {..} public String getAuthorName() {..} ….. public boolean equals(Object obj) { … } public int hashCode() {… } }

Practice II Try to make teacher and student into annotation style ! Try to add a course Entity course columns –id bigint –subject character varying(10) –name character varying(50)

Mapping Inheritance –Single Table per Class Hierarchy Strategy –Table per Class Strategy –Joined Subclass annotation

Single Table create table Person ( id bigint, idno character varying(10), name character varying(50), studentcode …, teachercode …, …. ) @Inheritance(strategy=Inheritance public class Person { private long id; private String idno; private String name; public class Student extends Person { private String studentcode; }

Table per Class Strategy create table Person( idno character varying(10), name character varying(50), ) create table Student ( idno character varying(10), name character varying(50), studentcode …, …. ) create table Teacher ( idno character varying(10), name character varying(50), teachercode …, …. nceType.TABLE_PER_CLA SS) public class Person { private String idno; private String name; } public class Student extends Person { private String studentcode; } public class Teacher extends Person { private String teachercode; }

Table per Subclass create table Person( idno character varying(10), name character varying(50), ) create table Student ( studentcode …, …. ) create table Teacher ( teachercode …, …. nceType.JOINED) public class Person { private String idno; private String name; } public class Student extends Person { private String studentcode; } public class Teacher extends Person { private String teachercode; }

No specific table for the entity create table Student ( idno character varying(10), name character varying(50), studentcode …, …. updatetime …. ) create table Teacher ( idno character varying(10), name character varying(50), teachercode …, …. updatetime …. public class Person { private String idno; private String name; private Timestamp updatetime; } public class Student extends Person { private String studentcode; } public class Teacher extends Person { private String teachercode; }

Practice III Try to add a Person Entity Try to use JOINED strategy Person columns –id bigint –idno character varying(10) –name character varying(50)

Entity Relationships Unidirectional one-to-one –ex. Person 1--->1 Heart Bidirectional one-to-one –ex. Person 1 1 Passport Unidirectional one-to-many –ex. Person 1 --->* Phones Bidirectional one-to-many –aka. Bidirectional many-to-one –ex. Person 1 * Childs Unidirectional many-to-one –ex. Person *--->1 Company Unidirectional many-to-many –ex. Person *--->* Address Bidirectional many-to-many –ex. Person * * Orders

Unidirectional public class Person public String getIdno() { return public Heart getHeart() { return heart;} public class Heart public Long getId(); }

Bidirectional public class Person public String getIdno() { return idno; } private public Passport getPassport() { return passport;} public class Passport public Long public Person getOwner(); }

public class Person implements Serializable cascade = {CascadeType.PERSIST, CascadeType.MERGE} public Company getCompany() { return company; public class Company implements Serializable { … }

Collections Java Collection Framework –List –Set –Map Apache Jakarta Commons-Collection –Bag

Unidirectional public class public List getPhones() { return phones; public class Phone { // … no bidir }

Bidirectional public class Person public List getChilds() { return childs; public class Child public Person getParent() { return parent; }

Unidirectional with join public class name=“student”, joinColumns inverseJoinColumns public Set getStudents() { … public class Student { // }

public class Person implements Serializable targetEntity=org.ossf.course.vo.Order.class, cascade={CascadeType.PERSIST, CascadeType.MERGE} name=“Person_Order”, public Collection getOrders() { return orders; }

public class Order implements Serializable ( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = “orders”, targetEntity = org.ossf.course.vo.Person.class ) public Collection getPersons() { return persons; }

Fetching … FetchType.EAGER FetchType.LAZY –Lazy Initialization exception ? –Hibernate.initialize(node); –Hibernate.initialize(node.getChilden());

Cascading ALL PERSIST MERGE REMOVE REFRESH

Practice IV Try to make Teacher one-to-many Students relationship Try to make Courses many-to-many Teachers relationship

Summary Hibernate is easy and the leader of ORM Annotations can let you leave XML-hell We learned how to create an Entity We learned how to use Inheritance We learned how to generate relationship Well, everything should be simple Of course, java is simple and powerful !