Your SlideShare is downloading. ×
0
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Java Persistence Frameworks for MongoDB

13,572

Published on

2 Comments
18 Likes
Statistics
Notes
  • You should also look at http://jongo.org/
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Strange that you don't even give the slightest mention to DataNucleus JDO/JPA which has supported JDO/JPA persistence to MongoDB since a long time before Hibernate OGM, and supports way more features than what you present in this presentation. It's even mentioned in the MongoDB docs from what I remember ...
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
13,572
On Slideshare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
266
Comments
2
Likes
18
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. MongoDB Berlin 2013:Java Persistence Frameworks for MongoDBTobias.Trelle@codecentric.de / @codecentric AG @tobiastrelle
  • 2. Tobias Trelle - Senior IT Consultant @ codecentric AG (official 10gen partner) official - Conference talks on MongoDB - MongoDB user group Düsseldorf/Germany - Authoring a German book on MongoDBcodecentric AG
  • 3. Where have all my tables gone … ORM is dead long live ODMcodecentric AG
  • 4. Agenda− MongoDB Java D Driver− Spring Data MongoDB− Morphia− Hibernate OGMcodecentric AG
  • 5. Use Casecodecentric AG
  • 6. Mongo Java Drivercodecentric AG
  • 7. MongoDB Drivers− One wire protocol for all client languages− A driver implementation per language− Responsibilities: − Converting language dependent data structures BSON − Generating ObjectId for _id field− Overview: http://www.mongodb.org/display/DOCS/Drivers www.mongodb.org/display/DOCS/Driverscodecentric AG
  • 8. MongoDB Java Driver− One JAR w/o further dependencies ependencies: <dependency> <groupId>org.mongodb</ </groupId> <artifactId>mongo-java java-driver</artifactId> <version>2.10.0</version version> </dependency>− github: https://github.com/mongodb/mongo github.com/mongodb/mongo-java-drivercodecentric AG
  • 9. Java Driver: Connect to MongoDBimport com.mongodb.MongoClient com.mongodb.MongoClient;// Default: localhost:27017mongo = new MongoClient();// Sharding: mongos servermongo = new MongoClient("mongos01" 4711); "mongos01",// Replica setmongo = new MongoClient(Arrays. Arrays.asList( new ServerAddress("replicant01" 10001), "replicant01", new ServerAddress("replicant02" 10002), "replicant02", new ServerAddress("replicant03" 10003) "replicant03", ));codecentric AG
  • 10. Java Driver: Database / Collectionimport com.mongodb.DB com.mongodb.DB;import com.mongodb.DBCollection com.mongodb.DBCollection;DB db = mongo.getDB mongo.getDB("test");DBCollection collection = db.getCollection("foo"); db.getCollectioncodecentric AG
  • 11. Java Driver: Documentsimport com.mongodb.BasicDBObject com.mongodb.BasicDBObject;import com.mongodb.DBObject com.mongodb.DBObject;// insert documentDBObject doc = new BasicDBObject();doc.put("date", new Date());doc.put("i", 42);collection.insert(doc doc);codecentric AG
  • 12. Java Driver: Queriesimport com.mongodb.DBCursor com.mongodb.DBCursor;DBCursor cursor;cursor = collection.find(); // all documents ();// documents w/ {i: 42}cursor = collection.find( ( new BasicDBObject("i i", 42) );document = cursor.next(); ();...codecentric AG
  • 13. Java Driver: Order Use CaseDB db = mongo.getDB("test");DBCollection collection = db.getCollection("order" "order");DBObject order;List<DBObject> items = new ArrayList<DBObject>(); >();DBObject item;// orderorder = new BasicDBObject();order.put("date", new Date());order.put("custInfo" , "Tobias Trelle");order.put("items", items);// itemsitem = new BasicDBObject();item.put("quantity", 1);item.put("price", 47.11);item.put("desc", "Item #1");items.add(item);item = new BasicDBObject();item.put("quantity", 2);item.put("price", 42.0);item.put("desc", "Item #2");items.add(item);collection.insert(order);codecentric AG
  • 14. Spring Data MongoDBcodecentric AG
  • 15. Spring Data MongoDB – Fact Sheet Vendor VMware / SpringSource License Apache License Version 2.0 License, Documentation http://www.springsource.org/spring-data/mongodb http://www.springsource.org/spring Main Features • Repository Support • Object/Document Mapping Document • Templatingcodecentric AG
  • 16. Spring DataCommon patterns for RDBMS and NoSQL data stores Spring Data CrudRepository PagingAndSortingRepository Spring Data Spring Data Spring Data Spring Data JPA MongoDB Neo4j … JpaRepository MongoRepository GraphRepository MongoTemplate Neo4jTemplate Embedded REST JPA Mongo Java Driver JDBC RDBMS MongoDB Neo4j …Quelle: http://www.infoq.com/articles/spring http://www.infoq.com/articles/spring-data-introcodecentric AG
  • 17. Spring Data MongoDBTemplating − Resource abstraction − Configure connections to mongod / mongos node(s) − Collection lifecycle ( create, drop) − Map/Reduce / AggregationObject Mapping − Annotation based: @Document, @Field, @Index etc. , − Classes are mapped to collections, Java Objects to documents ,Repository Support − Queries are derived from methods signatures − Geospatial Queriescodecentric AG
  • 18. Spring Data MongoDB TemplateConfiguration <!-- Connection to MongoDB server -- --> <mongo:db-factory host="localhost" port port="27017" dbname="test" /> <!-- MongoDB Template --> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory ref="mongoDbFactory"/> mongoDbFactory" </bean>Usage @Autowired MongoTemplate template; template.indexOps(Location.class).ensureIndex ensureIndex( new GeospatialIndex("position") ); )codecentric AG
  • 19. Spring Data MongoDB: Object Mappingpublic class Order { @Id private String id; private Date date; @Field("custInfo") private String customerInfo; List<Item> items; ...}public class Item { private int quantity; private double price; @Field("desc") private String description description; ...}codecentric AG
  • 20. Spring Data MongoDB: Repository Supportpublic interface OrderRepository extends MongoRepository<Order, String> { <Order, findByItemsQuantity(int quantity); List<Order> findByItemsQuantity findByItemsPriceGreaterThan(double price); List<Order> findByItemsPriceGreaterThan}codecentric AG
  • 21. Spring Data MongoDB: Repository Support− Main Concept: use the signature of a method to derive the query (at runtime)− Base Implementations / abstractions for − CRUD operations − Paging − Sortingcodecentric AG
  • 22. Spring Data MongoDB: Additional Goodies− Map/Reduce / Aggregation framework− Index Management− Support for GridFS− Geopspatial indexes / queries− Optimistic Lockingcodecentric AG
  • 23. Hibernate OGMcodecentric AG
  • 24. Hibernate OGM MongoDB – Fact Sheet Vendor JBoss / Redhat License GNU LGPL, Version 2.1 , Documentation http://www.hibernate.org/subprojects/ogm.html Main Features • JPA API (Subset Subset) • JPQL Query Languagecodecentric AG
  • 25. Hibernate OGM− Implements JPA API (subset)− JP-QL query are translated to native QL datastore queries− Supports Infinispan, EhCache, MongoDB ,codecentric AG
  • 26. Hibernate OGMArchitectureSource:http://docs.jboss.org/hibernate/ogm/4.0/reference /en-US/html/ogm-architecture.html#d0e409codecentric AG
  • 27. Hibernate OGM MongoDB: Configuration<persistence version="2.0" …> <persistence-unit name="primary"> <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence HibernateOgmPersistence</provider> <class>hibernate.Order</class> <class>hibernate.Item</class> <properties> <property name="hibernate.ogm.datastore.provider hibernate.ogm.datastore.provider" value="org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider" "org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider"/> <property name="hibernate.ogm.mongodb.database value=„odm"/> hibernate.ogm.mongodb.database" <property name="hibernate.ogm.mongodb.host value=„localhost"/> hibernate.ogm.mongodb.host" <property name="hibernate.ogm.mongodb.port value=„27017"/> hibernate.ogm.mongodb.port" </properties> </persistence-unit></persistence>codecentric AG
  • 28. Hibernate OGM MongoDB: Object Mapping@Entity@NamedQuery( name="byItemsQuantity", query = "SELECT o FROM Order o JOIN o.items i WHERE i.quantity = :quantity" )public class Order { @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid2" "uuid2") @Id private String id; private Date date; @Column(name = "custInfo") private String customerInfo customerInfo; @ElementCollection private List<Item> items;codecentric AG
  • 29. Hibernate OGM MongoDB: Object Mapping@Embeddablepublic class Item { private int quantity; private double price; @Column(name="desc") private String description description; ...codecentric AG
  • 30. Hibernate OGM: Summary− Very early beta− Only persist / merge / remove nly− No query support (yet) o− Uses relational APIcodecentric AG
  • 31. Hibernate OGM: OgmEntityManagercodecentric AG
  • 32. Morphiacodecentric AG
  • 33. Morphia – Fact Sheet Developer Scott Hernandez, James Green License Apache License Version 2.0 License, Documentation https://github.com/jmkgreen/morphia/wiki/Overview Main Features • Object/Document Mapping Document • Custom Query API • DAO supportcodecentric AG
  • 34. Morphia: Object Mappingpublic class Order { @Id private ObjectId id; private Date date; @Property("custInfo") private String customerInfo; @Embedded List<Item> items; ...}public class Item { private int quantity; private double price; @Property("desc") private String description description; ...}codecentric AG
  • 35. Morphia: Queriespublic class OrderDao extends BasicDAO BasicDAO<Order, ObjectId> { List<Order> findByItemsQuantity(int quantity) { return find( createQuery().filter("items.quantity items.quantity", quantity)) .asList(); } findByItemsPriceGreaterThan(double price) { List<Order> findByItemsPriceGreaterThan return find( createQuery().field("items.price items.price").greaterThan(price) ) .asList(); } …}codecentric AG
  • 36. Morphia: Custom query syntax – why? Morphia Mongo Query = $eq $ !=, <> $neq $ >, <, >=,<= $gt, $ $lt, $gte, $lte in, nin $in, $ $nin elem $elemMatch $ … ….codecentric AG
  • 37. Judge yourself …Spring Data MongoDBhttps://github.com/ttrelle/spring-data-exampleshttps://github.com/ttrelle/springHibernate OGM MongoDBhttps://github.com/ttrelle/hibernate github.com/ttrelle/hibernate-ogm-examplesMorphiahttps://github.com/ttrelle/morphia github.com/ttrelle/morphia-mongodb-examplescodecentric AG
  • 38. Which one should I use? High Abstraction Spring Data MongoDB JPA Hibernate OGM MorphiaEnterprise CustomStandard API MongoDB Java JDBC Driver Low Abstraction codecentric AG
  • 39. German MongoDB User Groups (MUGs)MUG Düsseldorf MUG Berlinhttps://www.xing.com/net/mongodb-dus http://www.meetup.com/MUGBerlin/@MongoDUS @MUGBerlinMUG Frankfurt/Main Hamburg MUGhttps://www.xing.com/net/mongodb-ffm https://www.xing.com/net/mugh@MongoFFMMUG Münchenhttp://www.meetup.com/Muenchen-MongoDB MongoDB-User-Group/@mongomuccodecentric AG
  • 40. QUESTIONS?Tobias Trellecodecentric AGMerscheider Str. 142699 Solingentel +49 (0) 212.233628.47fax +49 (0) 212.233628.79mail Tobias.Trelle@codecentric.detwitter @tobiastrellewww.codecentric.deblog.codecentric.de/en/author/tobias-trelle trellewww.xing.com/net/mongodb-duscodecentric AG

×