Embed
Email

Hibernate

Document Sample
Hibernate
Shared by: HC111209103135
Categories
Tags
Stats
views:
2
posted:
12/9/2011
language:
pages:
21
Hibernate

Tools, Criteria API, Search, Shards









Martin Valkanov

Contents

1. Hibernate Tools

• Schema export, Entity model diagram

2. Criteria API

• Restrictions

• Find by example

• DetachedCriteria

• Projections

• Class property

Contents

3. Hibernate Projects

• Search

• Shards

Hibernate Tools

Ant tasks and IDE integration

Schema Export

• Use the Schema Export command line

tool to generate schema creation DDL:

java -cp ...

org.hibernate.tool.hbm2ddl.SchemaExport

--config=hibernate.cfg.xml

--text --create --output=schemaExport.sql

Schema Export

• Or use the Ant task:









Entity Model Diagram

• Use the hbm2doc Ant task and GraphViz:



















Entity Model Diagram

• Or create a new Hibernate Configuration

in Eclipse, right-click on an entity and

choose „Open Mapping Diagram“

Criteria API

Building queries without clumsy

string manipulation

Restrictions

• Simple and powerful query building:

Criteria criteria =

session.createCriteria(PaymentModelImpl.class);

criteria.add(Restrictions.like("description",

"Комплект", MatchMode.START));



criteria.add(Restrictions.or(Restrictions.gt("amoun

t", new Long(200)),

Restrictions.like("description", "маса",

MatchMode.ANYWHERE)));



criteria.createCriteria("merchant").add(Restriction

s.eq("url", "http://www.mebeli-georgiev.com/"));



List result = criteria.list();

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).ignoreCase().e

nableLike(MatchMode.ANYWHERE));

List result = criteria.list();

Detached Criteria

• A detached criteria can be:

• serialized to a byte stream

• sent over the wire

DetachedCriteria dc =

DetachedCriteria.forClass(PaymentModelImpl.class);

dc.add(Restrictions.gt("amount", new Long(500)));

dc.add(Restrictions.eq("customer.id",

customer.getId()));



byte[] bytes = SerializationHelper.serialize(dc);

Detached Criteria

• and when restored – applied to another

Hibernate session:

DetachedCriteria dc = (DetachedCriteria)

SerializationHelper.deserialize(bytes);



List result =

dc.getExecutableCriteria(session).list();

Projections

• To do a group by query:

Criteria criteria =

session.createCriteria(PaymentModelImpl.class);

ProjectionList projectionList =

Projections.projectionList();

projectionList.add(Projections.rowCount(),

"count");

projectionList.add(Projections.sum("amount"),

"totalAmount");

projectionList.add(Projections.groupProperty("custo

mer"), "customer");

criteria.setProjection(projectionList);



criteria.setResultTransformer(Transformers.aliasToB

ean(CustomerPaymentInfo.class));

Class property

• Entity class can also be used in Criteria's

restrictions:

Criteria criteria =

session.createCriteria(PaymentModelImpl.class);



criteria.add(Restrictions.eq("class",

OnlineShoppingModelImpl.class));

Hibernate Projects

Unified data access programming style

Hibernate Search

• Integration with Apache Lucene indexing

• Indexed fields are annotated:

@Field(index=Index.TOKENIZED, store=Store.NO)‫‏‬

private String getDescription() {

///

}



• Index update is hooked via event

listeners: post-insert, post-update and

post-delete

Hibernate Search

• Access to Lucene API while benefitting

from the Hibernate Session:

String fullTextQuery = "description:(+\"кафяв

маса\"~3 +шкаф)";



FullTextSession fullTextSession =

Search.createFullTextSession(aSession);

QueryParser queryParser = new

QueryParser("description", new StandardAnalyzer());

Query query = fullTextSession.createFullTextQuery(

queryParser.parse(fullTextQuery),

PaymentModelImpl.class);



List result =

query.setMaxResults(50).list();

Hibernate Shards

• Application data is partitioned into

several data sources.

• Associated entities are stored in the same

shard.

• Custom SessionFactory and Session

implementation.

Hibernate Shards

• ShardStrategy

• ShardSelectionStrategy - Where to persist

a new entity

• ShardResolutionStrategy - Determine the

shards on which an object might live

• ShardAccessStrategy - Parallel,

Sequential, LoadBalancedSequential

Hibernate









Demos


Related docs
Other docs by HC111209103135
�Chopin Lives
Views: 0  |  Downloads: 0
PERITONEAL DIALYSIS
Views: 3  |  Downloads: 0
Critical Essay #3
Views: 9  |  Downloads: 0
PowerPoint Presentation
Views: 0  |  Downloads: 0
DIVISION OF ENVIRONMENTAL CHEMISTRY
Views: 0  |  Downloads: 0
Workshop outcomes
Views: 1  |  Downloads: 0
"BE HEALTHY DELAWARE DAY"
Views: 0  |  Downloads: 0
NPA-OPS 40A ETOPS
Views: 9  |  Downloads: 0
Prejudice: Disliking Others
Views: 0  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!