L16 Object Relational Mapping and NoSQL

30
Lecture 16 Object-Relational Mapping and NoSQL

description

Hin síðari ári hafa komið fram lausnir sem varpa gögnum sjálfkrafa í töflugagnagrunna. Þetta er svoköllum Object-Relational Mapping tól. Þar sem slík vörpun er svo algeng þá má nota svo tól til að framkvæma vörpunina sjálfkrafa. Tilvik af klasa í minni er sjálfkrafa varpað á töflu í gunninum. Eitt tól sem gerir þetta er Hibernate og munum við skoða það nánar. Við skoðum einnig NoSQL grunna en þessir grunnar hafa komið fram með 21. allar vefkerfum sem þurfa að skala yfir mjög margar nóður. Þar hentar Releational grunnurinn illa og önnur módel hafa verið reynd.

Transcript of L16 Object Relational Mapping and NoSQL

  • 1. Lecture 16 Object-Relational Mapping and NoSQL

2. Reading Hibernate (Wikipedia) ORMHate Introduction to NoSQL 3. Agenda Object Relational Mapping ORM NoSQL Hibernate 4. Object Relational Mapping Copyright 2008 lafur Andri Ragnarsson 5. Object Relational Mapping (ORM) Use a mapping layer to map between objects and tables Mapping a data representation from an object model to a relational data model with a SQL-based schema Mapping requires metadata XML Authoring and maintaining metadata is less work than maintaining SQL 6. Advantages of ORM Can radically reduce the amount of code you need to write 30% compared to JDBC for server side application More Productivity Applications are easier to maintain Fosters thinking about an OO domain model 7. Disadvantages of ORM Some loss of control over the persistence process May be more difficult to tune queries Performance characteristics of the tool may affect your applications performance 8. When to use ORM? Well-suited to ORM Read-modify-write lifecycle Little requirement for stored procedures Poorly suited to ORM Window on data application Significant use of stored procedures Write centric apps, where data is seldom read 9. When to use ORM? Typical server-side applications are fairly well suited for ORM 90%-95% of applications But there are always some special cases Mix and match as needed 10. Hibernate 11. Hibernate Object/relational mapping tool A persistence service that stores Java objects in relational databases Provides an object oriented view of existing relational data Uses reflection and XML mapping files to persist POJOs No changes to business domain objects The goal is to relieve the developer from a significant amount of common data persistence-related programming tasks 12. Architecture High-level architecture Properties file define data access Mapping definition maps classes to tables 13. Architecture 14. Database Properties File hibernate.properties Contains information to access the database Username and password URL Database driver Hibernate will automatically read the file from the classpath hibernate.connection.username=andri hibernate.connection.password=abc123 hibernate.connection.url=jdbc:jtds:sqlserver://honn.ru.is:1433 hibernate.connection.driver_class=net.sourceforge.jtds.jdbc.Driver 15. Mapping File File hibernate-mapping In the same package as Nemandi class 16. Using Hibernate Usually an application will Create a single Configuration Build a single instance of SessionFactory Then instantiate Session objects Configuration cfg = new Configuration(); cfg.addClass(theClass); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); 17. Using Hibernate Configuration Allows the application to specify properties and mapping documents to be used when creating a SessionFactor SessionFactory Factory class to create Session objects Session Interface that represents a transaction The main function is to offer create, read and delete operations for instances of mapped entity classes 18. Example NemandiGateway public interface NemandiGateway { public Nemandi findNemandi(String kennitala); public Collection getNemendur(); public void addNemandi(Nemandi nemandi); } 19. Example NemandiData Constructor creates the configuration and the factory Variable factory is used when a Session is needed public class NemandiData implements NemandiGateway { SessionFactory factory = null; public NemandiData() { Configuration cfg = new Configuration(); cfg.addClass(Nemandi.class); factory = cfg.buildSessionFactory(); } 20. Example NemandiData findNemandi public Nemandi findNemandi(String kennitala) { Session session = factory.openSession(); Nemandi nem = (Nemandi)session.get(Nemandi.class, kennitala); session.close(); return nem; } 21. Example NemandiData getNemendur Uses the Hibernate Query Language, HQL public Collection getNemendur() { Session session = factory.openSession(); List l = session.createQuery( "SELECT n FROM is.ru.honn.domain.Nemandi AS n").list(); session.close(); return l; } 22. Example NemandiData addNemandi public void addNemandi(Nemandi nemandi) { Session session = factory.openSession(); Transaction tx = session.beginTransaction(); session.save(nemandi); tx.commit(); session.close(); } 23. NoSQL Databases Copyright 2008 lafur Andri Ragnarsson 24. Data Model 25. Key-Value Basically Map They are also referred to as "schema on read" as one entry may be a String, another a Date, a third PNG image Key-Value stores offer no indexes, and no querying capabilities Examples: Property files, Dynamo, Riak, Redis, Aerospike, Voldemort 26. Document Store Mesh of documents Documents can have any structure no schema or implicit schema Json is popular Examples MongoDB, MarkLogic, CouchDB, RavenDB, 27. Tablestyle Database The value stored is sort of a transposed table in it self Sort of like Map> Such structures are extremely useful to store sparse data in Examples: Cassandra, Hadoop/HBase, Apache Fink, Monet 28. Graph Databases Graph or network structure of data: Entities and facts about these in the relations on them Graph Databases resembles Prolog in their use to infer new insights based upon facts and rules Really good at relation magic, e.g. if you have a "friends" relation on "user" entities Examples: Neo4J, GraphBase, Trinity 29. Summary Object Relational Mapping ORM Useful tools that work in most cases Still controversial Hibernate An example of a mapping tool One of the possible options NoSQL New bread of 21. century web databases