Hibernate - PowerPoint PPT Presentation

About This Presentation
Title:

Hibernate

Description:

... 'merchant').add(Restrictions.eq('url', 'http://www.mebeli-georgiev.com ... criteria.setResultTransformer(Transformers.aliasToBean(CustomerPaymentInfo. class) ... – PowerPoint PPT presentation

Number of Views:2124
Avg rating:3.0/5.0
Slides: 22
Provided by: dev78
Learn more at: https://www.devbg.org
Category:

less

Transcript and Presenter's Notes

Title: Hibernate


1
Hibernate
Tools, Criteria API, Search, Shards
Martin Valkanov
2
Contents
  • Hibernate Tools
  • Schema export, Entity model diagram
  • Criteria API
  • Restrictions
  • Find by example
  • DetachedCriteria
  • Projections
  • Class property

3
Contents
  • 3. Hibernate Projects
  • Search
  • Shards

4
Hibernate Tools
Ant tasks and IDE integration
5
Schema 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
6
Schema Export
  • Or use the Ant task

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
7
Entity 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
8
Entity Model Diagram
  • Or create a new Hibernate Configuration in
    Eclipse, right-click on an entity and choose
    Open Mapping Diagram

9
Criteria API
Building queries without clumsy string
manipulation
10
Restrictions
  • 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()
11
Find 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()
12
Detached 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)
13
Detached Criteria
  • and when restored applied to another Hibernate
    session

DetachedCriteria dc (DetachedCriteria)
SerializationHelper.deserialize(bytes) ListltPaym
entModelgt result dc.getExecutableCriteria(sessio
n).list()
14
Projections
  • To do a group by query

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))
15
Class 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))
16
Hibernate Projects
Unified data access programming style
17
Hibernate 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

18
Hibernate 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()
19
Hibernate Shards
  • Application data is partitioned into several data
    sources.
  • Associated entities are stored in the same shard.
  • Custom SessionFactory and Session implementation.

20
Hibernate Shards
  • ShardStrategy
  • ShardSelectionStrategy - Where to persist a new
    entity
  • ShardResolutionStrategy - Determine the shards on
    which an object might live
  • ShardAccessStrategy - Parallel, Sequential,
    LoadBalancedSequential

21
Hibernate
  • Demos
Write a Comment
User Comments (0)
About PowerShow.com