1 / 21

Hibernate

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.

georgio
Download Presentation

Hibernate

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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.SchemaExport --config=hibernate.cfg.xml --text --create --output=schemaExport.sql

  6. 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" />

  7. 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>

  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(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();

  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).ignoreCase().enableLike(MatchMode.ANYWHERE)); List<PaymentModel> result = criteria.list();

  12. 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);

  13. Detached Criteria • and when restored – applied to another Hibernate session: DetachedCriteria dc = (DetachedCriteria) SerializationHelper.deserialize(bytes); List<PaymentModel> result = dc.getExecutableCriteria(session).list();

  14. 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));

  15. 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));

  16. Hibernate Projects Unified data access programming style

  17. 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

  18. 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();

  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

More Related