You are on page 1of 23
Hibernate - odwzorowania, zastosowania Michał Śliwa
Hibernate - odwzorowania, zastosowania
Michał Śliwa
Treść 1. Projektowanie baz danych 2. Odwzorowania obiektowo-relacyjne 3. Zastosowania
Treść
1. Projektowanie baz danych
2. Odwzorowania obiektowo-relacyjne
3. Zastosowania
Projekt bazy danych Diagram klas:
Projekt bazy danych
Diagram klas:
Odwzorowania obiektowo-relacyjne Przypadki: 1. Klasa 2. Asocjacja (many-to-one) - jednokierunkowa - dwukierunkowa
Odwzorowania obiektowo-relacyjne
Przypadki:
1.
Klasa
2.
Asocjacja (many-to-one)
- jednokierunkowa
- dwukierunkowa
1. List
2. Set
4.
Asocjacja (many-to-many)
5.
Generalizacja
Klasa ASampleClass.java package com.manning.hq.apdxA; /** * @hibernate.class table="sample_class" */ public
Klasa
ASampleClass.java
package com.manning.hq.apdxA;
/**
* @hibernate.class table="sample_class"
*/
public class ASampleClass {
private Long id;
/** @hibernate.id generator-class="native" */
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
}
ASampleClass.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.manning.hq.apdxA">
<class name="com.manning.hq.apdxA.ASampleClass"
table="sample_class">
<id name="id" column="id" type="java.lang.Long">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>
Asocjacja (many-to-one) Event.java public class Event implements Serializable { private Location location; /** *
Asocjacja (many-to-one)
Event.java
public class Event implements Serializable {
private Location location;
/**
* @hibernate.many-to-one column="location_id"
*/
public Location getLocation() {return location;}
public void setLocation(Location location) {this.location = location;}
}
Location.java
public class Location implements Serializable{ }
Event.hbm.xml
<class name="com.manning.hq.apdxA.Event" table="events">
<many-to-one name="location" column="location_id"
class="com.manning.hq.apdxA.Location" />
</class>
jednokierunkowa
Asocjacja (many-to-one) Location.java public class Location implements Serializable{ private Set events = new
Asocjacja (many-to-one)
Location.java
public class Location implements Serializable{
private Set events = new LinkedHashSet();
/**
* @hibernate.set
* @hibernate.collection-key column="location_id"
* @hibernate.collection-one-to-many class="com.manning.hq.apdxA.Event"
* @return
*/
public Set getEvents() { return events; }
public void setEvents(Set events) { this.events = events; }
}
Location.hbm.xml
<class name="com.manning.hq.apdxA.Location" table="locations">
<set name="events">
<key column="location_id"/>
<one-to-many class="com.manning.hq.apdxA.Event"/>
</set>
</class>
dwukierunkowa
Asocjacja (one-to-many) Speaker.java public class Speaker implements Serializable { private List sessions = new
Asocjacja (one-to-many)
Speaker.java
public class Speaker implements Serializable {
private List sessions = new ArrayList();
/**
* @hibernate.list
* @hibernate.collection-key column="speaker_id"
* @hibernate.collection-index column="session_index"
* @hibernate.collection-one-to-many
class="com.manning.hq.apdxA.EventSession"
*/
public List getSessions() { return sessions; }
public void setSessions(List sessions) {this.sessions = sessions;}
}
Speaker.hbm.xml
<class name="com.manning.hq.apdxA.Speaker" table="speakers">
<list name="sessions">
<key column="speaker_id"/>
<index column="session_index" />
<one-to-many class="com.manning.hq.apdxA.EventSession" />
</list>
</class>
Asocjacja (many-to-many) Event.java public class Event implements Serializable { private Set speakers = new
Asocjacja (many-to-many)
Event.java
public class Event implements Serializable {
private Set speakers = new LinkedHashSet();
/**
* @hibernate.set table="as_event_to_attendee"
* @hibernate.collection-key column="event_id"
* @hibernate.collection-many-to-many
* class="com.manning.hq.apdxA.Attendee"
* column="attendee_id"
*/
public Set getAttendees() { return attendees; }
public void setAttendees(Set attendees) {this.attendees = attendees;}
}
Event.hbm.xml
<class name="com.manning.hq.apdxA.Event" table=”events”>
<set name="attendees" table="as_event_to_speaker">
<key column="event_id" />
<many-to-many class="com.manning.hq.apdxA.Attendee"
column="attendee_id" />
</set>
</class>
Generalizacja Event.java /** * @hibernate.class table="events" discriminator-value="Event" *
Generalizacja
Event.java
/**
* @hibernate.class table="events" discriminator-value="Event"
* @hibernate.discriminator column="class_name"
*/
public class Event implements Serializable {
private Long id;
/**
* @hibernate.id generator-class="native" column="id"
*/
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
}
ConferenceEvent.java
/**
*
@hibernate.subclass discriminator-value="ConferenceEvent"
*/
public class ConferenceEvent extends Event{ }
NetworkingEvent.java
/**
*
@hibernate.subclass discriminator-value="NetworkingEvent"
*/
public class NetworkingEvent extends Event{ }
Generalizacja Event.hbm.xml <class name="com.manning.hq.apdxA.Event" table="events"> <id
Generalizacja
Event.hbm.xml
<class name="com.manning.hq.apdxA.Event" table="events">
<id name="id" column="id" type="java.lang.Long">
<generator class="native" />
</id>
<subclass name="com.manning.hq.apdxA.ConferenceEvent"
discriminator-value=" ConferenceEvent " />
<subclass name="com.manning.hq.apdxA.NetworkingEvent"
discriminator-value="NetworkingEvent" />
</class>
Zastosowania 1. Przykład CRUD 2. HQL 3. Criteria
Zastosowania
1. Przykład CRUD
2. HQL
3. Criteria
Przykładowy projekt
Przykładowy projekt
hibernate.cfg.xml
hibernate.cfg.xml
Dom
Dom
Pokoj
Pokoj
HibernateUtil
HibernateUtil
create retrieve
create
retrieve
update delete
update
delete
HQL Criteria
HQL
Criteria
Podsumowanie 1. Od projektu do odwzorowania 2. Najczęstsze błędy
Podsumowanie
1. Od projektu do odwzorowania
2. Najczęstsze błędy
Literatura 1. „Hibernate Quickly”, Manning 2006 2. „Hibernate in Action”, Manning 2005 3.
Literatura
1. „Hibernate Quickly”, Manning 2006
2. „Hibernate in Action”, Manning 2005
3. http://www.hibernate.org
Pytania ?
Pytania
?