chapter 4. collections and...
TRANSCRIPT
1 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Chapter 4. Collections and Associations
Collections
Associations
2 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Java Variable and Collection
class Person {
Long id;
Address address;
}
class Address {
Long id;
Set<Person> persons = new HashSet<Person>;
}
3 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Java Collections
Java Collections
Implementations
Interfaces
HashTable
ResizableArray Tree Linked
ListHash Table +Linked List
Set HashSet TreeSet LinkedHashSetList ArrayList LinkedList
QueueDeque ArrayDeque LinkedListMap HashMap TreeMap LinkedHashMap
4 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Bidirectional Associations
5 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Bidirectional Many-to-One/One-to-Many Associations 1/3
Person
id
Address
id*persons
1address
PersonAddress
class Person {Long id;Address address;
}
class Address {Long id;Set<Person> persons =
new HashSet<Person>;}
create table Address (addressId bigint not null primary key
)
create table Person (personId bigint not null primary key,addressId bigint not null
)
6 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Bidirectional Many-to-One/One-to-Many Associations 2/3
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"class="Address"column="addressId"not-null="true" />
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<set name="persons" inverse="true"><key column="addressId"/><one-to-many class="Person"/>
</set>
</class>
class Person {Long id;Address address;
}
class Address {Long id;Set<Person> persons =
new HashSet<Person>;}
create table Address (addressId bigint not null primary key
generated by default as identity)
create table Person (personId bigint not null primary key
generated by default as identity,addressId bigint not null
)
7 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping (detail)
Bidirectional Many-to-One/One-to-Many Associations 3/3
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"class="Address"column="addressId"not-null="true" />
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<set name="persons" inverse="true"><key column="addressId"/><one-to-many class="Person"/>
</set>
</class>
class Person {Long id;Address address;
}
class Address {Long id;Set<Person> persons =
new HashSet<Person>;}
create table Address (addressId bigint not null primary key
generated by default as identity)
create table Person (personId bigint not null primary key
generated by default as identity,addressId bigint not null
)
8 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Bidirectional Many-to-Many Associations 1/2
Person
id
Address
id*persons
*addresses
PersonAddress
create table PersonAddress ( personId bigint not null,addressId bigint not null,primary key (personId, addressId)
)
class Person {Long id;Set<Address> addresses =
new HashSet<Address>;}
class Address {Long id;Set<Person> persons =
new HashSet<Person>;}
create table Address (addressId bigint not null primary key
)
create table Person (personId bigint not null primary key,
)
9 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Bidirectional Many-to-Many Associations 2/2
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses" table="PersonAddress"><key column="personId"/><many-to-many class="Address"
column="addressId"/></set>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<set name="persons" table="PersonAddress"inverse="true">
<key column="addressId"/><many-to-many class="Person"
column="personId"/></set>
</class>
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 Person {Long id;Set<Address> addresses =
new HashSet<Address>;}
class Address {Long id;Set<Person> persons =
new HashSet<Person>;}
10 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Bidirectional One-to-One Associations on a Foreign Key 1/2
Person
id
Address
id1person
1address
PersonAddress
class Person {Long id;Address address;
}
class Address {Long id;Person person;
}
create table Address (addressId bigint not null primary key
)
create table Person (personId bigint not null primary key,addressId bigint not null
)
11 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Bidirectional One-to-One Associations on a Foreign Key 2/2
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
class="Address"
column="addressId"
unique="true"
not-null="true" />
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<one-to-one name="person"
property-ref="address"/>
</class>
property-ref (optional):the name of a property of the associated class that
is joined to the primary key of this classIf not specified, the primary key of the associated class is used.
class Person {Long id;Address address;
}
class Address {Long id;Person person;
}
create table Address (addressId bigint not null primary key
)
create table Person (personId bigint not null primary key,addressId bigint not null
)
12 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Bidirectional One-to-One Associations on a Primary Key 1/2
Person
id
Address
id1person
1address
PersonAddress
class Person {Long id;Address address;
}
class Address {Long id;Person person;
}
create table Address (personId bigint not null primary key
)
create table Person (personId bigint not null primary key,
)
13 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Bidirectional One-to-One Associations on a Primary Key 2/2
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<one-to-one name="address" cascade=“all”/>
</class>
class Person {Long id;Address address;
}
class Address {Long id;Person person;
}
create table Address (personId bigint not null primary key
)
create table Person (personId bigint not null primary key,
)
<class name="Address">
<id name="id" column="personId">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<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
14 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Unidirectional Associations
15 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Unidirectional Many-to-One Associations 1/2
class Person {Long id;Address address;
}
class Address {Long id;
}
create table Address (addressId bigint not null primary key
)
create table Person (personId bigint not null primary key,addressId bigint not null
)
Person
id
Address
id*persons
1address
PersonAddress
16 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Unidirectional Many-to-One Associations 2/2
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address" column="addressId" not-null="true" />
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
class Person {Long id;Address address;
}
class Address {Long id;
}
create table Address (addressId bigint not null primary key
)
create table Person (personId bigint not null primary key,addressId bigint not null
)
17 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Unidirectional One-to-Many Associations (unusual case) 1/2
Person
id
Address
id1person
*addresses
PersonAddress
class Person {Long id;Set<Address> addresses;
}
class Address {Long id;
}
create table Address (addressId bigint not null primary key,personId bigint not null
)
create table Person (personId bigint not null primary key
)
18 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Unidirectional One-to-Many Associations (unusual case) 2/2
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses">
<key column="personId" not-null="true"/>
<one-to-many class="Address"/>
</set>
</class>
<class name="Address">
<id name="id" column=“addressId">
<generator class=“native">
</id>
</class>
class Person {Long id;Set<Address> addresses;
}
class Address {Long id;
}
create table Address (addressId bigint not null primary key,personId bigint not null
)
create table Person (personId bigint not null primary key
)
19 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Unidirectional Many-to-Many Associations 1/2
Person
id
Address
id*persons
*addresses
PersonAddress
create table PersonAddress ( personId bigint not null,addressId bigint not null,primary key (personId, addressId)
)
class Person {Long id;Set<Address> addresses =
new HashSet<Address>;}
class Address {Long id;
}
create table Address (addressId bigint not null primary key
)
create table Person (personId bigint not null primary key,
)
20 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Unidirectional Many-to-Many Associations 2/2
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses" table="PersonAddress">
<key column="personId"/>
<many-to-many class="Address" column="addressId" />
</set>
</class>
<class name="Address">
<id name="id" column=“addressId">
<generator class=“native">
</id>
</class>
create table PersonAddress ( personId bigint not null,addressId bigint not null,primary key (personId, addressId)
)
class Person {Long id;Set<Address> addresses =
new HashSet<Address>;}
class Address {Long id;
}
create table Address (addressId bigint not null primary key
)
create table Person (personId bigint not null primary key,
)
21 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Unidirectional One-to-One Associations on a Foreign Key 1/2
Person
id
Address
id1person
1address
PersonAddress
class Person {Long id;Address address;
}
class Address {Long id;
}
create table Address (addressId bigint not null primary key
)
create table Person (personId bigint not null primary key,addressId bigint not null
)
22 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Unidirectional One-to-One Associations on a Foreign Key 2/2
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
class="Address"
column="addressId"
unique="true"
not-null="true" />
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
class Person {Long id;Address address;
}
class Address {Long id;
}
create table Address (addressId bigint not null primary key
)
create table Person (personId bigint not null primary key,addressId bigint not null
)
23 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Unidirectional One-to-One Associations on a Primary Key 1/2
Person
id
Address
id1person
1address
PersonAddress
class Person {Long id;
}
class Address {Long id;Person person;
}
create table Address (personId bigint not null primary key
)
create table Person (personId bigint not null primary key,
)
24 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping
Unidirectional One-to-One Associations on a Primary Key 2/2
<class name="Person">
<id name="id" column="personId">
<generator class=“native“/>
</id>
</class>
class Person {Long id;
}
class Address {Long id;Person person;
}
create table Address (personId bigint not null primary key
)
create table Person (personId bigint not null primary key,
)
<class name="Address">
<id name="id" column="personId"><generator class=“foreign“>
<param name=“property”>person</param>
</generator>
</id>
<one-to-one name=“person" constrained=“true”/>
</class>
25 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Association and Mapping Exercise
hibernate-369-mkyong-win
– 01-stock-quickstart-xml
– 03-stock-onetomany-xml
– 05-stock-manytomany-xml
– 08-stock-onetoone-xml
26 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
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" …>
<id name="id" …>
…
</id>
…
</class>
</hibernate-mapping>
ex04/src/main/java/com/oreilly/hh/data/Company.hbm.xml:
<hibernate-mapping>
<class name="com.oreilly.hh.data.Company" …>
<id name="id" …>
…
</id>
…
</class>
</hibernate-mapping>
27 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Exercise #4: test
$ gradle test
:ex04:test
…
Results: SUCCESS (3 tests, 3 successes, 0 failures, 0 skipped)
BUILD SUCCESSFUL
28 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Java Collections vs Database Relationships …
Bidirectional Many-to-Many Associations
Artist
idname
Track
idtitlefilePathplayTimeaddedvolume
0..*artists
0..*tracks
track_artists
class Artist {
Integer id;
String name;
Set<Track> tracks;
}
class Track {
Integer id;
String title;
String filePath;
time playTime;
date added;
short volume;
Set<Artist> artists;
}
UML
Java
29 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Java Collections vs Database Relationships …
Unidirectional Many-to-Many Associations
Artist
idname
Track
idtitlefilePathplayTimeaddedvolume
0..*tracks
track_artists
class Artist {
Integer id;
String name;
Set<Track> tracks;
}
class Track {
Integer id;
String title;
String filePath;
time playTime;
date added;
short volume;
}
UML
Java
30 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Java Collections vs Database Relationships
Many-to-Many Associations and Database Relationships
create table ARTIST (
id integer primary key,
name varchar(62)
)
create table TRACK (
id integer primary key,
title varchar(62),
filePath varchar(62),
playTime time,
added date,
volume smallint
)
UML
Tablecreate table TRACK_ARTISTS (
artist_id integer,
track_id integer,
foreign key (artist_id) references artist(id),
foreign key (track_id) references track(id)
)
Artist
idname
Track
idtitlefilePathplayTimeaddedvolume
0..*tracks
track_artists0..*
artists
31 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Mapping Collections (for Bidirectional Many-to-Many Association)
Artist
idname
Track
idtitlefilePathplayTimeaddedvolume
0..*tracks
track_artists0..*
artists
<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>
32 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Mapping Collections (for Unidirectional Many-to-Many Association)
Artist
idname
Track
idtitlefilePathplayTimeaddedvolume
0..*tracks
track_artists
<set name="tracks" table="TRACK_ARTISTS">
<key column="ARTIST_ID“ />
<many-to-many class="com.oreilly.hh.data.Track" column="TRACK_ID"/>
</set>
33 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
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>
<id name="id" type="int" column="ARTIST_ID">
<generator class="native"/>
</id>
<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>
34 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
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>
35 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
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>
36 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
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 = (Artist)query.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 void addTrackArtist(Track track, Artist artist) {
track.getArtists().add(artist);
}
...
37 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
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>
...
38 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Persisting Collections (CreateTest.java) …...
public static void 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(), (short)0);
addTrackArtist(track, getArtist("PPK", true, session));
session.save(track);
track = new Track("Video Killed the Radio Star", "vol2/album611/track12.mp3",
Time.valueOf("00:03:49"), new HashSet<Artist>(), new Date(), (short)0);
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(), (short)0);
addTrackArtist(track, getArtist("Laurie Anderson", true, session));
session.save(track);
39 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
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(), (short)0);
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(), (short)0);
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(), (short)0);
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",
Time.valueOf("00:00:10"), new HashSet<Artist>(), new Date(), (short)0);
session.save(track);...
40 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Persisting Collections : What just happened?
$ gradle ctest
$ gradle db
41 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
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();
}
...
42 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Retrieving Collections (QueryTest.java)
public class QueryTest {
...
public static String listArtistNames(Set<Artist> artists) { ... }
public static void 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());
}
}
...
}
}
43 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Retrieving Collections (Hibernate.cfg.xml)
...
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
...
44 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
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: "Video 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
45 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Using Bidirectional Associations (QueryTest2.java)// Example 4-12. Source for QueryTest2.java
public class QueryTest2 extends JPanel {
...
private void 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();
}
}
46 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Using Bidirectional Associations (build.gradle)
...
task qtest2(dependsOn: classes, type: JavaExec) {
main = 'com.oreilly.hh.QueryTest2'classpath = sourceSets.main.runtimeClasspath
}
$ gradle qtest2
47 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Working with Simpler Collections …
48 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Working with Simpler Collections …
Collections of associations to other objects
collections of simple values, like strings, numbers, and nonpersistentvalue classes– want to record some number of comments about each track in the database– Track.hbm.xml
– gradle schema
...
<set name="comments" table="TRACK_COMMENTS">
<key column="TRACK_ID"/>
<element column="COMMENT" type="string"/>
</set>
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;
49 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Working with Simpler Collections …
Add Comment Set at the end of each Constructor of CreateTest.java :
Then assign a comment on the following line :
track = new Track("Test Tone 1", "vol2/singles/test01.mp3",
Time.valueOf("00:00:10"), new HashSet<Artist>(),
new Date(), (short)0, new HashSet<String>());
track.getComments().add("Pink noise to test equalization");
50 / 51Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Working with Simpler Collections …
Add another loop after the track println( ) in QueryTest.java to print the comments for the track :
gradle qtest
for (String comment : aTrack.getComments()) {
System.out.println(" Comment: " + comment);
}
...
Track: "Test Tone 1" 00:00:10
Comment: Pink noise to test equalization
51 / 51Prof. 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 Tutorial– http://www.mkyong.com/tutorials/hibernate-tutorials/