20
1 / 20 Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University Database Programming Chapter 2. Introduction to Mapping <hibernate-mapping> <class> </class> </hibernate-mapping>

Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

1 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

Chapter 2. Introduction to Mapping

<hibernate-mapping>

<class>

</class>

</hibernate-mapping>

Page 2: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

2 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

Class and Table Generation of Track

<hibernate-mapping>

<class name=“Track” table=“TRACK”>

</class>

</hibernate-mapping>

id title filePath …

TRACK

Track.hbm.xml

Page 3: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

3 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

Data Bean (Track.java)

package com.oreilly.hh.data;

import java.util.Date;

public class Track implements java.io.Serializable {

private int id;

private String title;

private String filePath;

private Date playTime;

private Date added;

private short volume;

public Track() {

}

public Track(String title, String filePath, short volume) {

...

}

public Track(String title, String filePath, Date playTime, Date added, short volume) {

...

}

public int getId() {

return this.id;

}

protected void setId(int id) {

this.id = id;

}

// Getters and Setters

Page 4: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

4 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

The Mapping Document for Tracks (Track.hbm.xml) …

<?xml version="1.0“ encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.oreilly.hh.data.Track" table="TRACK">

<meta attribute="class-description">

Represents a single playable track in the music database.

@author Jim Elliott (with help from Hibernate)

</meta>

<id name="id" type="integer" column="TRACK_ID">

<meta attribute="scope-set">protected</meta>

<generator class="native"/>

</id>

<property name="title" type="string" not-null="true"/>

<property name="filePath" type="string" not-null="true"/>

. . .

Page 5: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

5 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

The Mapping Document for Tracks (Track.hbm.xml)

<property name="playTime" type="time">

<meta attribute="field-description">Playing time</meta>

</property>

<property name="added" type="date">

<meta attribute="field-description">When the track was created</meta>

</property>

<property name="volume" type="short" not-null="true">

<meta attribute="field-description">How loud to play the track</meta>

</property>

</class>

</hibernate-mapping>

Page 6: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

6 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

The Mapping Elements …

<hibernate-mapping>

– The mapping document is an XML document having <hibernate-mapping> as the root element which

contains all the <class> elements.

<class>

– The <class> elements are used to define specific mappings from a Java classes to the database tables.

– The Java class name is specified using the name attribute of the class element and the database table name

is specified using the table attribute.

<meta>

– The <meta> element is optional element and can be used to create the class description.

<id>

– The <id> element maps the unique ID attribute in class to the primary key of the database table.

– The name attribute of the id element refers to the property in the class and the column attribute refers to the

column in the database table.

– The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL

data type.

<generator>

– The <generator> element within the id element is used to automatically generate the primary key values.

To set the class attribute of the generator element to native let hibernate pick up either identity, sequence or

hilo algorithm to create primary key depending upon the capabilities of the underlying database.

Page 7: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

7 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

The Mapping Elements

<property>

– The <property> element is used to map a Java class property to a column in the database table.

– The name attribute of the element refers to the property in the class and the column attribute refers to the

column in the database table.

– The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL

data type.

(Excerpt from http://www.tutorialspoint.com/hibernate/hibernate_mapping_files.htm)

Page 8: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

8 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

Built-in Basic Mapping Types of Hibernate (summary)

integer, long, short, float, double, character, byte, boolean, yes_no, true_false

– Type mappings from Java primitives or wrapper classes to appropriate (vendor-specific) SQL column

types. boolean, yes_no and true_false are all alternative encodings for a Java boolean or java.lang.Boolean.

string

– A type mapping from java.lang.String to VARCHAR (or Oracle VARCHAR2).

date, time, timestamp

– Type mappings from java.util.Date and its subclasses to SQL types DATE, TIME and TIMESTAMP (or

equivalent).

binary

– Maps byte arrays to an appropriate SQL binary type.

text

– Maps long Java strings to a SQL LONGVARCHAR or TEXT type.

image

– Maps long byte arrays to a SQL LONGVARBINARY.

clob, blob

– Type mappings for the JDBC classes java.sql.Clob and java.sql.Blob. These types can be inconvenient for

some applications, since the blob or clob object cannot be reused outside of a transaction. Driver support is

patchy and inconsistent.

(Excerpt from http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch05.html#mapping-types)

Page 9: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

9 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

Built-in Basic Mapping Types of Hibernate

(Excerpt from http://www.tutorialspoint.com/hibernate/hibernate_mapping_types.htm)

Page 10: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

10 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

Built-in Basic Mapping Types of Hibernate …

(Excerpt from http://www.tutorialspoint.com/hibernate/hibernate_mapping_types.htm)

Java 8 (hibernate 5: hibernate-core, hibernate-java8)• java.time.LocalDate

• java.time.LocalTime

• java.time.LocalDateTime

Page 11: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

11 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

Built-in Basic Mapping Types of Hibernate …

(Excerpt from http://www.tutorialspoint.com/hibernate/hibernate_mapping_types.htm)

Page 12: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

12 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

Database Connection Information for HSQLDB

hibernate.properties :

hibernate.dialect=org.hibernate.dialect.HSQLDialect

hibernate.connection.driver_class=org.hsqldb.jdbcDriver

hibernate.connection.url=jdbc:hsqldb:build/music

hibernate.connection.username=sa

hibernate.connection.password=

hibernate.connection.shutdown=true HSQLDB only

# create the database schema on startup

# {update, validate, create, create-drop }

hibernate.hbm2ddl.auto = update

Page 13: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

13 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

Database Connection Information for H2

hibernate.properties :

hibernate.dialect=org.hibernate.dialect.H2Dialect

hibernate.connection.driver_class=org.h2.Driver

hibernate.connection.url=jdbc:h2:file:./build/music

hibernate.connection.username=sa

hibernate.connection.password=

# create the database schema on startup

# {update, validate, create, create-drop }

hibernate.hbm2ddl.auto = update

Page 14: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

14 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

The logging configuration file (logback.xml)

logback.xml

Page 15: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

15 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

Displaying the Schema in Console

build.gradle :

task schema(dependsOn: classes, type: JavaExec) {

main = 'org.hibernate.tool.hbm2ddl.SchemaExport'

args '--config=hibernate.cfg.xml', '--text', '--format', '--delimiter=;'

//args '--create'

classpath = sourceSets.main.runtimeClasspath

}

$ gradle schema

:ch02-mapping:schema

drop table TRACK if exists;

create table TRACK (

TRACK_ID integer generated by default as identity,

title varchar(255) not null,

filePath varchar(255) not null,

playTime time,

added date,

volume smallint not null,

primary key (TRACK_ID)

);

BUILD SUCCESSFUL

Page 16: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

16 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

Building the Schema in DB

$ gradle ctest ( hibernate.cfg.xml)

Page 17: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

17 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

Generated Table ‘Track’

$ gradle db

Page 18: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

18 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

Exercise #2: Use Employee.java

ex02/src/main/java/com/oreilly/hh/data/Employee.java:

package com.oreilly.hh.data;

import java.util.Date;

public class Employee implements java.io.Serializable {

private Long id;

private String name;

public Employee() {

}

// getters and setters

}

Page 19: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

19 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

Exercise #2: Generate Employee.hbm.xml and test

$ gradle test

:ex02-final:test

com.oreilly.hh.EmployeeTest > testCount PASSED

com.oreilly.hh.EmployeeTest > testCollection PASSED

Results: SUCCESS (2 tests, 2 successes, 0 failures, 0 skipped)

BUILD SUCCESSFUL

ex02/src/main/java/com/oreilly/hh/data/Employee.hbm.xml :

:

<hibernate-mapping>

<class name="com.oreilly.hh.data.Employee" …>

<id name="id" …>

</id>

</class>

</hibernate-mapping>

Page 20: Chapter 2. Introduction to Mapping - KOCWcontents.kocw.net/KOCW/document/2016/hanbat/kimyoungchan/2.pdf · Introduction to Mapping

20 / 20Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University

Database Programming

Materials for Further Study

Hibernate Home

– http://www.hibernate.org/

Hibernate Manual

– Hibernate Getting Started Guide 3.6

• http://docs.jboss.org/hibernate/core/3.6/quickstart/en-US/html/

– Hibernate Reference Documentation 3.6

• http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/

• http://docs.jboss.org/hibernate/core/3.6/reference/en-US/pdf/hibernate_reference.pdf

– Hibernate Reference Documentation 4.3, and 5.0

Hibernate Tools Manual

– Hibernate Tools Reference Guide

• http://docs.jboss.org/tools/latest/en/hibernatetools/html/index.html

• http://docs.jboss.org/tools/latest/en/hibernatetools/pdf/Hibernatetools_Reference_Guide.pdf

– JBoss Tools Documentation

• http://docs.jboss.org/tools/latest/

Hibernate Tutorial

– http://www.mkyong.com/tutorials/hibernate-tutorials/