Professional Documents
Culture Documents
Mappings and Queries With Hibernate
Mappings and Queries With Hibernate
with
Hibernate
Mappings
Collection mapping
Association mapping
Component mapping
Revision
Hibernate is an object-relational mapping framework
Maps persistence operations between object models to
relational
l ti lddatabases
t b
Core elements in a Hibernate application are:
Yourr Java
Yo Ja a objects
The Hibernate object mapping files (Event.hbm.xml)
The Hibernate configuration file (Hibernate.cfg.xml)
Classes working with the Hibernate API (Session, Transaction)
// other properties
Collection declared
private Set<String> emails;
as interface type
// get and set methods
}
Hibernate mapping
<class
l name=no.uio.inf5750.example.model.Person
i i f5750 l d lP ttable=persons>
bl
</class>
Actual content of set
Indexed collections
All ordered collection mappings need an index column in
the collection table to persist the sequence
Index
I d off List
Li t is
i always
l off type
t Integer,
I t index
i d off Map
M can
be of any type
Indexed collection mapping
public class Person
{
private int id;
// other properties
List is an ordered private List<String> phoneNumbers;
type of collection
// get and set methods
}
<class
class name
name=no.uio.inf5750.example.model.Person
no.uio.inf5750.example.model.Person table
table=persons>
persons
List mapped to table
<!-- other properties -->
</class>
Association mapping
Hibernate lets you easily specify all kinds of associations
between objects
Unidirectional one
one-to-many
to many
Unidirectional many-to-many
Bidirectional one-to-manyy
Bidirectional many-to-many
</class>
Unidirectional many
many-to-many
to many
public class Event public class Person
{ N {
private int id; private int id;
N
Set of Persons private Set<Person> persons; // other properties
} }
<class name=no.uio.inf5750.example.model.Event
p table=events>
Foreign key Event // id and other properties
</class>
Bidirectional one
one-to-many
to many
Event reference...
reference public class Event public class Person
{ N {
private int id; private int id;
1
Set of Persons private Set<Person> persons; private Event event;
} }
</class>
Specifies which
join table to use <class name
name=no
no.uio.inf5750.example.model.Person
uio inf5750 example model Person table
table=persons>
persons >
for the association
<join table=events_persons inverse=true>
<key column=person_id/>
<many-to-one column=event_id name=event/>
</join>
Refers to property
in Java class </class>
Bidirectional many-to-many
many to many
Set of Events...
Events public class Event public class Person
{ N {
private int id; private int id;
N
Set of Persons private Set<Person> persons; private Set<Event> events;
} }
</class>
Queries
The Query interface
The Hibernate Query Language (HQL)
The Query interface
You need a query when you dont know the identifiers of
the objects you are looking for
Used
U d mainly
i l tto execute
t Hib
Hibernate
t QQuery L
Language
queries
Obtained from a Hibernate Session instance
Provides functionality for:
Parameter binding to named query parameters
Retrieving lists of objects or unique objects
Limiting
g the number of retrieved objects
j
from Person
from Person as p
Size clause:
Q
Query obtained
bt i d ffrom Session
S i String hql = "from Person where age = :age";
query.setMaxResults( maxResults );
return query.list();
Max nr of objects
j restriction
}
return ((Person)) q
query.uniqueResult();
y q ();
}
uniqueResult offers a
shortcut if you know a single
object will be returned
Resources
Books on Hibernate
Christian Bauer and Gavin King: Hibernate in Action
James Elliot: Hibernate A Developer
Developerss notebook
Justin Gehtland, Bruce A. Tate: Better, Faster, Lighter Java