Title: Hibernate
1Hibernate
Tools, Criteria API, Search, Shards
Martin Valkanov
2Contents
- Hibernate Tools
- Schema export, Entity model diagram
- Criteria API
- Restrictions
- Find by example
- DetachedCriteria
- Projections
- Class property
3Contents
- 3. Hibernate Projects
- Search
- Shards
4Hibernate Tools
Ant tasks and IDE integration
5Schema Export
- Use the Schema Export command line tool to
generate schema creation DDL
java -cp ... org.hibernate.tool.hbm2ddl.SchemaExpo
rt --confighibernate.cfg.xml --text --create
--outputschemaExport.sql
6Schema Export
lttaskdef name"schemaexport" classname"org.hibern
ate.tool.hbm2ddl.SchemaExportTask"/gt ltschemaexpor
t config"bin/hibernate.cfg.xml" quiet"no" text
"yes" drop"no" delimiter"" output"generate
d/schemaExport.sql" /gt
7Entity Model Diagram
- Use the hbm2doc Ant task and GraphViz
lttaskdef name"hibernatetool" classname"org.hiber
nate.tool.ant.HibernateToolTask
classpathref"toolslib" /gt lthibernatetool
destdir"generated"gt ltconfiguration
configurationfile"bin/hibernate.cfg.xml"
/gt lthbm2doc destdir"generated/hbm_doc"gt ltprope
rty key"dot.executable" value"/usr/local/bin/dot
" /gt lt/hbm2docgt lt/hibernatetoolgt
8Entity Model Diagram
- Or create a new Hibernate Configuration in
Eclipse, right-click on an entity and choose
Open Mapping Diagram
9Criteria API
Building queries without clumsy string
manipulation
10Restrictions
- Simple and powerful query building
Criteria criteria session.createCriteria(Payment
ModelImpl.class) criteria.add(Restrictions.like("
description", "????????", MatchMode.START)) crit
eria.add(Restrictions.or(Restrictions.gt("amount",
new Long(200)), Restrictions.like("description",
"????", MatchMode.ANYWHERE))) criteria.createCri
teria("merchant").add(Restrictions.eq("url",
"http//www.mebeli-georgiev.com/")) ListltPayment
Modelgt result criteria.list()
11Find by Example
- A search prototype entity can be used to model
restrictions on the properties set
PaymentModel example new OnlineShoppingModelImpl
() example.setDescription("????") Criteria
criteria session.createCriteria(PaymentModelImpl
.class) criteria.add(Example.create(example).ign
oreCase().enableLike(MatchMode.ANYWHERE)) ListltPa
ymentModelgt result criteria.list()
12Detached Criteria
- A detached criteria can be
- serialized to a byte stream
- sent over the wire
DetachedCriteria dc DetachedCriteria.forClass(Pa
ymentModelImpl.class) dc.add(Restrictions.gt("amo
unt", new Long(500))) dc.add(Restrictions.eq("cus
tomer.id", customer.getId())) byte bytes
SerializationHelper.serialize(dc)
13Detached Criteria
- and when restored applied to another Hibernate
session
DetachedCriteria dc (DetachedCriteria)
SerializationHelper.deserialize(bytes) ListltPaym
entModelgt result dc.getExecutableCriteria(sessio
n).list()
14Projections
Criteria criteria session.createCriteria(Payment
ModelImpl.class) ProjectionList projectionList
Projections.projectionList() projectionList.add(P
rojections.rowCount(), "count") projectionList.ad
d(Projections.sum("amount"), "totalAmount") proje
ctionList.add(Projections.groupProperty("customer"
), "customer") criteria.setProjection(projectionL
ist) criteria.setResultTransformer(Transformers
.aliasToBean(CustomerPaymentInfo.class))
15Class property
- Entity class can also be used in Criteria's
restrictions
Criteria criteria session.createCriteria(Payment
ModelImpl.class) criteria.add(Restrictions.eq("c
lass", OnlineShoppingModelImpl.class))
16Hibernate Projects
Unified data access programming style
17Hibernate Search
- Integration with Apache Lucene indexing
- Indexed fields are annotated
_at_Field(indexIndex.TOKENIZED, storeStore.NO)? pri
vate String getDescription() ///
- Index update is hooked via event listeners
post-insert, post-update and post-delete
18Hibernate Search
- Access to Lucene API while benefitting from the
Hibernate Session
String fullTextQuery "description(\"?????
????\"3 ????)" FullTextSession
fullTextSession Search.createFullTextSession(aSe
ssion) QueryParser queryParser new
QueryParser("description", new StandardAnalyzer())
Query query fullTextSession.createFullTextQuer
y( queryParser.parse(fullTextQuery),
PaymentModelImpl.class) ListltPaymentModelgt
result query.setMaxResults(50).list()
19Hibernate Shards
- Application data is partitioned into several data
sources. - Associated entities are stored in the same shard.
- Custom SessionFactory and Session implementation.
20Hibernate Shards
- ShardStrategy
- ShardSelectionStrategy - Where to persist a new
entity - ShardResolutionStrategy - Determine the shards on
which an object might live - ShardAccessStrategy - Parallel, Sequential,
LoadBalancedSequential
21Hibernate