Chapter 4. Collections and Associations

Similar documents
Chapter 3. Harnessing Hibernate

Chapter 6. Custom Value Types

Chapter 7. The Annotations Alternative

Chapter 2. Introduction to Mapping

Chapter 9. A Look at HQL

Advanced Web Systems 9- Hibernate annotations, Spring integration, Aspect Oriented Programming. A. Venturini

Chapter 13. Hibernate with Spring

Step By Step Guideline for Building & Running HelloWorld Hibernate Application

Installing MySQL. Hibernate: Setup, Use, and Mapping file. Setup Hibernate in IDE. Starting WAMP server. phpmyadmin web console

HIBERNATE - ONE-TO-ONE MAPPINGS

HIBERNATE - MANY-TO-ONE MAPPINGS

HIBERNATE - COMPONENT MAPPINGS

HIBERNATE - SORTEDSET MAPPINGS

Unit 6 Hibernate. List the advantages of hibernate over JDBC

JAVA SYLLABUS FOR 6 WEEKS

JAVA. Duration: 2 Months

HIBERNATE - INTERCEPTORS

Java Object/Relational Persistence with Hibernate. David Lucek 11 Jan 2005

1 st Step. Prepare the class to be persistent:

Lightweight J2EE Framework

Core Java Contents. Duration: 25 Hours (1 Month)

JAVA. 1. Introduction to JAVA

Advanced Programming Languages Effective Java Item 1. Spring 2015 Chungnam National Univ Eun-Sun Cho

The Object-Oriented Paradigm. Employee Application Object. The Reality of DBMS. Employee Database Table. From Database to Application.

Introduction to Relational Database Management Systems

Complete Java Contents

Topic 10: The Java Collections Framework (and Iterators)

JPA. Java persistence API

Peers Techno log ies Pv t. L td. Core Java & Core Java &Adv Adv Java Java

5/23/2015. Core Java Syllabus. VikRam ShaRma

Introduction to Session beans. EJB - continued

JAVA SYLLABUS FOR 6 MONTHS

Mappings and Queries. with. Hibernate

Generating A Hibernate Mapping File And Java Classes From The Sql Schema

Java Magistère BFA

Object Persistence and Object-Relational Mapping. James Brucker

Advanced Programming Generics Collections

CSE 530A. Lab 3. Washington University Fall 2013

Core Java Syllabus. Overview

Database Application Architectures

Hibernate Overview. By Khader Shaik

International Journal of Advance Research in Engineering, Science & Technology HIBERNATE FRAMEWORK FOR ENTERPRISE APPLICATION

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Collections. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Learn Java/J2EE Basic to Advance level by Swadeep Mohanty

Wentworth Institute of Technology COMP1050 Computer Science II Spring 2017 Derbinsky. Collections & Maps. Lecture 12. Collections & Maps

Java Data Structures Collections Framework BY ASIF AHMED CSI-211 (OBJECT ORIENTED PROGRAMMING)

Building Java Programs

Table of Contents. Tutorial API Deployment Prerequisites... 1

Teneo: Integrating EMF & EclipseLink

EJB 3 Entity Relationships

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

Hibernate OGM Architecture

Java Programming Unit 8. Selected Java Collec5ons. Generics.

Object-Oriented Programming with Java

U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2018 TRIMESTER 2 COMP 103 PRACTICE EXAM

EJB 3 Entity Relationships

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

Pieter van den Hombergh Richard van den Ham. February 8, 2018

27/04/2012. Objectives. Collection. Collections Framework. "Collection" Interface. Collection algorithm. Legacy collection

Building Java Programs

Collections (Java) Collections Framework

Hibernate in close action. INF5750/ Lecture 3 (Part III)

CSE 143 Au03 Final Exam Page 1 of 15

IBM DB2 UDB V7.1 Family Fundamentals.

Java Persistence API (JPA)

Interview Questions I received in 2017 and 2018

Chapter 1. Installation and Setup

Model Solutions. COMP 103: Test April, 2013

What data persistence means? We manipulate data (represented as object state) that need to be stored

Interfaces. An interface defines a set of methods. An interface declaration contains signatures, but no implementations.

Java Collections Framework reloaded

HIBERNATE MOCK TEST HIBERNATE MOCK TEST I

Strings and Arrays. Hendrik Speleers

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

Hibernate Interview Questions

Exploring EJB3 With JBoss Application Server Part 6.3

JDO XML MetaData Reference (v5.2)

Chapter 10 Collections

PERSİSTENCE OBJECT RELATİON MAPPİNG

Call: JSP Spring Hibernate Webservice Course Content:35-40hours Course Outline

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

What is Transaction? Why Transaction Management Required? JDBC Transaction Management in Java with Example. JDBC Transaction Management Example

CSE 8B Final Exam Fall 2015

[Ref: Core Java Chp 13, Intro to Java Programming [Liang] Chp 22, Absolute Java Chp 16, docs.oracle.com/javase/tutorial/collections/toc.

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

HIBERNATE MOCK TEST HIBERNATE MOCK TEST IV

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. June 8, 2017

Daffodil DB. Design Document (Beta) Version 4.0

CSC 1351: Final. The code compiles, but when it runs it throws a ArrayIndexOutOfBoundsException

Exceptions and Design

Class Libraries. Readings and References. Java fundamentals. Java class libraries and data structures. Reading. Other References

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Thursday 22 nd May Time: 14:00 16:00

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

Linked Lists. References and objects

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Hibernate in Action by Christian Bauer and Gavin King Chapter 6. Copyright 2004 Manning Publications

indx.qxd 11/3/04 3:34 PM Page 339 Index

Hibernate Quickly by Patrick Peak and Nick Heudecker Chapter 3

Transcription:

Chapter 4. Collections and Associations Collections Associations 1 / 51

Java Variable and Collection class Person { Address address; class Address { Set<Person> persons = new HashSet<Person>; 2 / 51

Java Collections Java Collections Implementat ions Interfaces Hash Table Resizable Array Tree Linked List Hash Table + Linked List Set HashSet TreeSet LinkedHashSet List ArrayList LinkedList Queue Deque ArrayDeque LinkedList Map HashMap TreeMap LinkedHashMap 3 / 51

Association and Mapping Birectional Associations 4 / 51

Association and Mapping Birectional Many-to-One/One-to-Many Associations 1/3 Person * PersonAddress persons 1 address Address class Person { Address address; class Address { Set<Person> persons = new HashSet<Person>; create table Person ( personid bigint not null primary key, addressid bigint not null create table Address ( addressid bigint not null primary key 5 / 51

Association and Mapping Birectional Many-to-One/One-to-Many Associations 2/3 class Person { Address address; class Address { Set<Person> persons = new HashSet<Person>; create table Person ( personid bigint not null primary key generated by default as entity, addressid bigint not null create table Address ( addressid bigint not null primary key generated by default as entity <class name="person"> < name="" column="personid"> <generator class="native"/> </> <many-to-one name="address" class="address" column="addressid" not-null="true" /> </class> <class name="address"> < name="" column="addressid"> <generator class="native"/> </> <set name="persons" inverse="true"> <key column="addressid"/> <one-to-many class="person"/> </set> </class> 6 / 51

Association and Mapping (detail Birectional Many-to-One/One-to-Many Associations 3/3 class Person { Address address; class Address { Set<Person> persons = new HashSet<Person>; create table Person ( personid bigint not null primary key generated by default as entity, addressid bigint not null create table Address ( addressid bigint not null primary key generated by default as entity <class name="person"> < name="" column="personid"> <generator class="native"/> </> <many-to-one name="address" class="address" column="addressid" not-null="true" /> </class> <class name="address"> < name="" column="addressid"> <generator class="native"/> </> <set name="persons" inverse="true"> <key column="addressid"/> <one-to-many class="person"/> </set> </class> 7 / 51

Association and Mapping Birectional Many-to-Many Associations 1/2 Person * PersonAddress persons * addresses Address class Person { Set<Address> addresses = new HashSet<Address>; class Address { Set<Person> persons = new HashSet<Person>; create table Person ( personid bigint not null primary key, create table Address ( addressid bigint not null primary key create table PersonAddress ( personid bigint not null, addressid bigint not null, primary key (personid, addressid 8 / 51

Association and Mapping Birectional Many-to-Many Associations 2/2 class Person { Set<Address> addresses = new HashSet<Address>; class Address { Set<Person> persons = new HashSet<Person>; create table Person ( personid bigint not null primary key create table PersonAddress ( personid bigint not null, addressid bigint not null, primary key (personid, addressid create table Address ( addressid bigint not null primary key <class name="person"> < name="" column="personid"> <generator class="native"/> </> <set name="addresses" table="personaddress"> <key column="personid"/> <many-to-many class="address" column="addressid"/> </set> </class> <class name="address"> < name="" column="addressid"> <generator class="native"/> </> <set name="persons" table="personaddress" inverse="true"> <key column="addressid"/> <many-to-many class="person" column="personid"/> </set> </class> 9 / 51

Association and Mapping Birectional One-to-One Associations on a Foreign Key 1/2 Person 1 PersonAddress person 1 address Address class Person { Address address; class Address { Person person; create table Person ( personid bigint not null primary key, addressid bigint not null create table Address ( addressid bigint not null primary key 10 / 51

Association and Mapping Birectional One-to-One Associations on a Foreign Key 2/2 class Person { Address address; class Address { Person person; create table Person ( personid bigint not null primary key, addressid bigint not null create table Address ( addressid bigint not null primary key <class name="person"> < name="" column="personid"> <generator class="native"/> </> <many-to-one name="address" </class> class="address" column="addressid" unique="true" not-null="true" /> <class name="address"> < name="" column="addressid"> <generator class="native"/> </> <one-to-one name="person" </class> property-ref="address"/> property-ref (optional: the name of a property of the associated class that is joined to the primary key of this class 11 / 51 If not specified, the primary key of the associated class is used.

Association and Mapping Birectional One-to-One Associations on a Primary Key 1/2 Person 1 PersonAddress person 1 address Address class Person { Address address; class Address { Person person; create table Person ( personid bigint not null primary key, create table Address ( personid bigint not null primary key 12 / 51

Association and Mapping Birectional One-to-One Associations on a Primary Key 2/2 class Person { Address address; class Address { Person person; create table Person ( personid bigint not null primary key, create table Address ( personid bigint not null primary key <class name="person"> < name="" column="personid"> <generator class="native"/> </> <one-to-one name="address" cascade= all /> </class> <class name="address"> < name="" column="personid"> <generator class="foreign"> <param name="property">person</param> </generator> </> <one-to-one name="person" constrained="true"/> </class> constrained (optional: specifies that a foreign key constraint on the primary key of the mapped table and references the table of the associated class 13 / 51

Association and Mapping Unirectional Associations 14 / 51

Association and Mapping Unirectional Many-to-One Associations 1/2 Person * PersonAddress persons 1 address Address class Person { Address address; class Address { create table Person ( personid bigint not null primary key, addressid bigint not null create table Address ( addressid bigint not null primary key 15 / 51

Association and Mapping Unirectional Many-to-One Associations 2/2 class Person { Address address; class Address { create table Person ( personid bigint not null primary key, addressid bigint not null create table Address ( addressid bigint not null primary key <class name="person"> < name="" column="personid"> <generator class="native"/> </> <many-to-one name="address" column="addressid" not-null="true" /> </class> <class name="address"> < name="" column="addressid"> <generator class="native"/> </> </class> 16 / 51

Association and Mapping Unirectional One-to-Many Associations (unusual case 1/2 Person 1 PersonAddress * person addresses Address class Person { Set<Address> addresses; class Address { create table Person ( personid bigint not null primary key create table Address ( addressid bigint not null primary key, personid bigint not null 17 / 51

Association and Mapping Unirectional One-to-Many Associations (unusual case 2/2 class Person { Set<Address> addresses; class Address { create table Person ( personid bigint not null primary key create table Address ( addressid bigint not null primary key, personid bigint not null <class name="person"> < name="" column="personid"> <generator class="native"/> </> <set name="addresses"> <key column="personid" not-null="true"/> <one-to-many class="address"/> </set> </class> <class name="address"> < name="" column= addressid"> <generator class= native"> </> </class> 18 / 51

Association and Mapping Unirectional Many-to-Many Associations 1/2 Person * PersonAddress * persons addresses Address class Person { Set<Address> addresses = new HashSet<Address>; class Address { create table Person ( personid bigint not null primary key, create table Address ( addressid bigint not null primary key create table PersonAddress ( personid bigint not null, addressid bigint not null, primary key (personid, addressid 19 / 51

Association and Mapping Unirectional Many-to-Many Associations 2/2 class Person { Set<Address> addresses = new HashSet<Address>; create table Person ( personid bigint not null primary key, class Address { create table Address ( addressid bigint not null primary key create table PersonAddress ( personid bigint not null, addressid bigint not null, primary key (personid, addressid <class name="person"> < name="" column="personid"> <generator class="native"/> </> <set name="addresses" table="personaddress"> <key column="personid"/> <many-to-many class="address" column="addressid" /> </set> </class> <class name="address"> < name="" column= addressid"> <generator class= native"> </> </class> 20 / 51

Association and Mapping Unirectional One-to-One Associations on a Foreign Key 1/2 Person 1 PersonAddress person 1 address Address class Person { Address address; class Address { create table Person ( personid bigint not null primary key, addressid bigint not null create table Address ( addressid bigint not null primary key 21 / 51

Association and Mapping Unirectional One-to-One Associations on a Foreign Key 2/2 class Person { Address address; class Address { create table Person ( personid bigint not null primary key, addressid bigint not null create table Address ( addressid bigint not null primary key <class name="person"> < name="" column="personid"> <generator class="native"/> </> <many-to-one name="address" class="address" column="addressid" unique="true" not-null="true" /> </class> <class name="address"> < name="" column="addressid"> <generator class="native"/> </> </class> 22 / 51

Association and Mapping Unirectional One-to-One Associations on a Primary Key 1/2 Person 1 PersonAddress person 1 address Address class Person { class Address { Person person; create table Person ( personid bigint not null primary key, create table Address ( personid bigint not null primary key 23 / 51

Association and Mapping Unirectional One-to-One Associations on a Primary Key 2/2 class Person { class Address { Person person; create table Person ( personid bigint not null primary key, create table Address ( personid bigint not null primary key <class name="address"> <class name="person"> < name="" column="personid"> <generator class= native /> </> </class> < name="" column="personid"> <generator class= foreign > <param name= property >person</param> </generator> </> <one-to-one name= person" constrained= true /> </class> 24 / 51

Association and Mapping Exercise hibernate-369-mkyong-win 01-stock-quickstart-xml 03-stock-onetomany-xml 05-stock-manytomany-xml 08-stock-onetoone-xml 25 / 51

Exercise #4: Generate hbm.xml of Employee and Company ex04/src/main/java/com/oreilly/hh/data/employee.hbm.xml: <hibernate-mapping> <class name="com.oreilly.hh.data.employee" > < name="" > </> </class> </hibernate-mapping> ex04/src/main/java/com/oreilly/hh/data/company.hbm.xml: <hibernate-mapping> <class name="com.oreilly.hh.data.company" > < name="" > </> </class> </hibernate-mapping> 26 / 51

Exercise #4: test $ gradle test :ex04:test Results: SUCCESS (3 tests, 3 successes, 0 failures, 0 skipped BUILD SUCCESSFUL 27 / 51

Java Collections vs Database Relationships Birectional Many-to-Many Associations Track UML name Artist 0..* artists track_artists 0..* tracks title filepath playtime added volume Java class Artist { Integer ; String name; Set<Track> tracks; class Track { Integer ; String title; String filepath; time playtime; date added; short volume; Set<Artist> artists; 28 / 51

Java Collections vs Database Relationships Unirectional Many-to-Many Associations Track UML name Artist track_artists 0..* tracks title filepath playtime added volume Java class Artist { Integer ; String name; Set<Track> tracks; class Track { Integer ; String title; String filepath; time playtime; date added; short volume; 29 / 51

Java Collections vs Database Relationships Many-to-Many Associations and Database Relationships Track UML name Artist 0..* artists track_artists 0..* tracks title filepath playtime added volume Table create table ARTIST ( integer primary key, name varchar(62 create table TRACK_ARTISTS ( artist_ integer, track_ integer, foreign key (artist_ references artist(, foreign key (track_ references track( create table TRACK ( integer primary key, title varchar(62, filepath varchar(62, playtime time, added date, volume smallint 30 / 51

Mapping Collections (for Birectional Many-to-Many Association name Artist 0..* artists track_artists 0..* tracks Track title filepath playtime added volume <set name="tracks" table="track_artists" inverse="true"> <key column="artist_id /> <many-to-many class="com.oreilly.hh.data.track" column="track_id"/> </set> <set name="artists" table="track_artists"> <key column="track_id"/> <many-to-many class="com.oreilly.hh.data.artist" column="artist_id"/> </set> 31 / 51

Mapping Collections (for Unirectional Many-to-Many Association name Artist track_artists 0..* tracks Track title filepath playtime added volume <set name="tracks" table="track_artists"> <key column="artist_id /> <many-to-many class="com.oreilly.hh.data.track" column="track_id"/> </set> 32 / 51

Mapping Collections (Artist.hbm.xml <?xml version="1.0 encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC...> <hibernate-mapping> <class name="com.oreilly.hh.data.artist" table="artist"> <meta attribute="class-description">... </meta> < name="" type="int" column="artist_id"> <generator class="native"/> </> <property name="name" type="string"> <column name="name" not-null="true" unique="true" index="artist_name"/> </property> <set name="tracks" table="track_artists" inverse="true"> <key column="artist_id"/> <many-to-many class="com.oreilly.hh.data.track" column="track_id"/> </set> </class> </hibernate-mapping> 33 / 51

Mapping Collections (Track.hbm.xml <?xml version="1.0 encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC...> <hibernate-mapping> <class name="com.oreilly.hh.data.track" table="track">... <set name="artists" table="track_artists"> <key column="track_id"/> <many-to-many class="com.oreilly.hh.data.artist" column="artist_id"/> </set>... </class> </hibernate-mapping> 34 / 51

Mapping Collections (hibernate.cfg.xml hibernate.cfg.xml : <?xml version="1.0 encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC...> <hibernate-configuration> <session-factory>... <mapping resource="com/oreilly/hh/data/track.hbm.xml"/> <mapping resource="com/oreilly/hh/data/artist.hbm.xml"/> </session-factory> </hibernate-configuration> 35 / 51

Persisting Collections (CreateTest.java public class CreateTest { public static Artist getartist(string name, boolean create, Session session { Query query = session.getnamedquery("com.oreilly.hh.artistbyname"; query.setstring("name", name; Artist found = (Artistquery.uniqueResult(; if (found == null && create { found = new Artist(name, new HashSet<Track>(; session.save(found; return found; /** * Utility method to associate an artist with a track */ private static vo addtrackartist(track track, Artist artist { track.getartists(.add(artist;... 36 / 51

Persisting Collections (Artist.hbm.xml Artist.hbm.xml :... <query name="com.oreilly.hh.artistbyname"> <![CDATA[ from Artist as artist where upper(artist.name = upper(:name ]]> </query>... Track.hbm.xml :... <query name="com.oreilly.hh.tracksnolongerthan"> <![CDATA[ from Track as track where track.playtime <= :length ]]> </query>... 37 / 51

Persisting Collections (CreateTest.java... public static vo main(string args[] throws Exception {... try { // Create some data and persist it tx = session.begintransaction(; Track track = new Track("Russian Trance", "vol2/album610/track02.mp3", Time.valueOf("00:03:30", new HashSet<Artist>(, new Date(, (short0; addtrackartist(track, getartist("ppk", true, session; session.save(track; track = new Track("Veo Killed the Radio Star", "vol2/album611/track12.mp3", Time.valueOf("00:03:49", new HashSet<Artist>(, new Date(, (short0; addtrackartist(track, getartist("the Buggles", true, session; session.save(track; track = new Track("Gravity's Angel", "vol2/album175/track03.mp3", Time.valueOf("00:06:06", new HashSet<Artist>(, new Date(, (short0; addtrackartist(track, getartist("laurie Anderson", true, session; session.save(track; 38 / 51

Persisting Collections (CreateTest.java... track = new Track("Adagio for Strings (Ferry Corsten Remix", "vol2/album972/track01.mp3", Time.valueOf("00:06:35", new HashSet<Artist>(, new Date(, (short0; addtrackartist(track, getartist("william Orbit", true, session; addtrackartist(track, getartist("ferry Corsten", true, session; addtrackartist(track, getartist("samuel Barber", true, session; session.save(track; track = new Track("Adagio for Strings (ATB Remix", "vol2/album972/track02.mp3", Time.valueOf("00:07:39", new HashSet<Artist>(, new Date(, (short0; addtrackartist(track, getartist("william Orbit", true, session; addtrackartist(track, getartist("atb", true, session; addtrackartist(track, getartist("samuel Barber", true, session; session.save(track; track = new Track("The World '99", "vol2/singles/pvw99.mp3", Time.valueOf("00:07:05", new HashSet<Artist>(, new Date(, (short0; addtrackartist(track, getartist("pulp Victim", true, session; addtrackartist(track, getartist("ferry Corsten", true, session; session.save(track; track = new Track("Test Tone 1", "vol2/singles/test01.mp3", session.save(track;... Time.valueOf("00:00:10", new HashSet<Artist>(, new Date(, (short0; 39 / 51

Persisting Collections : What just happened? $ gradle ctest $ gradle db 40 / 51

Retrieving Collections (QueryTest.java package com.oreilly.hh;... public class QueryTest {... public static String listartistnames(set<artist> artists { StringBuilder result = new StringBuilder(; for (Artist artist : artists { result.append((result.length( == 0? "(" : ", "; result.append(artist.getname(; if (result.length( > 0 { result.append(" "; return result.tostring(;... 41 / 51

Retrieving Collections (QueryTest.java public class QueryTest {... public static String listartistnames(set<artist> artists {... public static vo main(string args[] throws Exception {... try { // Print the tracks that will fit in seven minutes List tracks = tracksnolongerthan(time.valueof("00:07:00", session;... for ( Track atrack : tracks { System.out.println("Track: \"" + atrack.gettitle( + "\" " + listartistnames(atrack.getartists( + atrack.getplaytime(; 42 / 51

Retrieving Collections (Hibernate.cfg.xml... <!-- Echo all executed SQL to stdout --> <property name="show_sql">false</property>... 43 / 51

Retrieving Collections : QueryTest Output $ gradle qtest :ch04:compilejava UP-TO-DATE :ch04:processresources UP-TO-DATE :ch04:classes UP-TO-DATE :ch04:qtest Track: "Russian Trance" (PPK 00:03:30 Track: "Veo Killed the Radio Star" (The Buggles 00:03:49 Track: "Gravity's Angel" (Laurie Anderson 00:06:06 Track: "Adagio for Strings (Ferry Corsten Remix" (Ferry Corsten, William Orbit, Samuel Ba rber 00:06:35 Track: "Test Tone 1" 00:00:10 Comment: Pink noise to test equalization BUILD SUCCESSFUL Total time: 2.299 secs 44 / 51

Using Birectional Associations (QueryTest2.java // Example 4-12. Source for QueryTest2.java public class QueryTest2 extends JPanel {... private vo updatetracks(string name { model.removeallelements(; // Clear out previous tracks if (name.length( < 1 return; // Nothing to do try { // Ask for a session using the JDBC information we've configured Session session = sessionfactory.opensession(; try { Artist artist = CreateTest.getArtist(name, false, session; if (artist == null { // Unknown artist model.addelement("artist not found"; return; // List the tracks associated with the artist for (Track atrack : artist.gettracks( { model.addelement("track: \"" + atrack.gettitle( + "\", " + atrack.getplaytime(; finally { session.close(; catch (Exception e { System.err.println("Problem updating tracks:" + e; e.printstacktrace(; 45 / 51

Using Birectional Associations (build.gradle... task qtest2(dependson: classes, type: JavaExec { main = 'com.oreilly.hh.querytest2' classpath = sourcesets.main.runtimeclasspath $ gradle qtest2 46 / 51

Working with Simpler Collections 47 / 51

Working with Simpler Collections Collections of associations to other objects collections of simple values, like strings, numbers, and nonpersistent value classes want to record some number of comments about each track in the database Track.hbm.xml... <set name="comments" table="track_comments"> <key column="track_id"/> <element column="comment" type="string"/> </set> gradle schema create table TRACK_COMMENTS (TRACK_ID integer not null, COMMENT varchar(255; alter table TRACK_COMMENTS add constraint FK105B26882DCBFAB5 foreign key (TRACK_ID references TRACK; 48 / 51

Working with Simpler Collections Add Comment Set at the end of each Constructor of CreateTest.java : track = new Track("Test Tone 1", "vol2/singles/test01.mp3", Time.valueOf("00:00:10", new HashSet<Artist>(, new Date(, (short0, new HashSet<String>(; Then assign a comment on the following line : track.getcomments(.add("pink noise to test equalization"; 49 / 51

Working with Simpler Collections Add another loop after the track println( in QueryTest.java to print the comments for the track : for (String comment : atrack.getcomments( { System.out.println(" Comment: " + comment; gradle qtest... Track: "Test Tone 1" 00:00:10 Comment: Pink noise to test equalization 50 / 51

Materials for Further Study Hibernate Home http://www.hibernate.org/ Hibernate Manual Hibernate Getting Started Gue 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 Tutorial http://www.mkyong.com/tutorials/hibernate-tutorials/ 51 / 51