Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

31
Morphia JPA for MongoDB

description

Morphia is MongoDB’s JPA-like Object-Document Mapping (ODM). However, there is no object-relational impedence mismatch since documents fit the concept of object-orientation much better — as this talk shows. On a practical level we will take a look at the available annotations and how to map your objects as well as how to easily query data in MongoDB. The talk concludes with some tips and the lesson we’ve learned from using Morphia in production for three years.

Transcript of Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Page 1: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

MorphiaJPA for MongoDB

Page 2: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Philipp Krenn@xeraa

Page 3: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Who uses

JPA?

Page 4: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Who uses

MongoDB?

Page 5: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Who uses

Morphia?

Page 6: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Like JPA for MongoDB

...but better

Page 7: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

@OneToMany(mappedBy = "destCustomerId")@ManyToMany@Fetch(FetchMode.SUBSELECT)@JoinTable(name = "customer_dealer_map", joinColumns = { @JoinColumn(name = "customer_id", referencedColumnName = "id")}, inverseJoinColumns = { @JoinColumn(name = "dealer_id", referencedColumnName = "id")})private Collection<Client> dealers;

Page 8: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn
Page 9: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Relations vs Objects

Page 10: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

MongoDB

Page 11: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Table = CollectionSchemaless

Row = DocumentJSON

Page 12: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

{ "name": "Philipp", "isAlive": true, "age": 30, "height_cm": 181.5, "address": { "city": "Vienna", "postalCode": "1190" }, "phoneNumbers": [ { "type": "mobile", "number": "+43 123 4567890" } ], "children": []}

Page 13: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Morphia

Page 14: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Object-Document Mapping

POJO + AnnotationsObject-Relational Mapping

Page 15: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

FeaturesLightweight

Type safe & preserving

Page 16: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Required librarieshttps://github.com/mongodb/

mongo-java-driver (2.12.4)+

https://github.com/mongodb/morphia (0.108)

Page 17: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Show me some codehttps://github.com/xeraa/

morphia-demo

Page 18: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

TODO: final class diagram

Page 19: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Annotations

Page 20: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

@Entity

@Entity( value = "company", noClassnameStored = true)public class CompanyEntity {

Tip: Avoid dashes in the collection name

Page 21: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Queries

Page 22: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn
Page 23: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Patterns

Page 24: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Base Class

Page 25: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Converters

Page 26: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Auto Increments

Page 27: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Encryption

Page 28: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn
Page 29: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Future

Page 30: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn
Page 31: Javantura v2 - Morphia - painfree JPA for MongoDB - Philipp Krenn

Thanks!Questions?

Now, later today, or @xeraa