210 likes | 568 Views
Hibernate. Tools, Criteria API, Search, Shards. Martin Valkanov. Contents. Hibernate Tools Schema export, Entity model diagram Criteria API Restrictions Find by example DetachedCriteria Projections Class property. Contents. 3. Hibernate Projects Search Shards. Hibernate Tools.
E N D
Hibernate Tools, Criteria API, Search, Shards Martin Valkanov
Contents Hibernate Tools Schema export, Entity model diagram 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: <taskdef name="schemaexport" classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"/> <schemaexport config="bin/hibernate.cfg.xml" quiet="no" text="yes" drop="no" delimiter=";" output="generated/schemaExport.sql" />
Entity Model Diagram Use the hbm2doc Ant task and GraphViz: <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask classpathref="toolslib" /> <hibernatetool destdir="generated"> <configuration configurationfile="bin/hibernate.cfg.xml" /> <hbm2doc destdir="generated/hbm_doc"> <property key="dot.executable" value="/usr/local/bin/dot" /> </hbm2doc> </hibernatetool>
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("amount", new Long(200)), Restrictions.like("description", "маса", MatchMode.ANYWHERE))); criteria.createCriteria("merchant").add(Restrictions.eq("url", "http://www.mebeli-georgiev.com/")); List<PaymentModel> 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().enableLike(MatchMode.ANYWHERE)); List<PaymentModel> 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<PaymentModel> 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("customer"), "customer"); criteria.setProjection(projectionList); criteria.setResultTransformer(Transformers.aliasToBean(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<PaymentModel> 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