200 likes | 212 Views
Data Access Layers. Technion – Institute of Technology 236700. 1. Overview. We’ll focus on SQL-DAL today. 2. Evolution. 3. Relational DB. SQL stands for Structured Query Language An external Domain Specific Language (DSL) for data retrieval
E N D
Data Access Layers Technion – Institute of Technology 236700 1
Overview We’ll focus on SQL-DAL today 2
Relational DB • SQL stands for Structured Query Language • An external Domain Specific Language (DSL) for data retrieval • First published at 1974 by Donald D. Chamberlin & Raymond F. Boyce (IBM) • Data is organized in Tables • A table column is associated to a name and a type • A table row holds the actual data • Table may have relations between them: Students Courses Registration 4
SQL (basic) Queries • The 4 basic queries are: • Select – Retrieves row(s) from a table(s) • Insert – Inserts data into a table(s) • Update – Updates existing row(s) in a table(s) • Delete – Deletes row(s) from a table • Different SQL dialects • Slight differences between implementations • MySQL (Oracle), SQL Server (Microsoft), DB2 (IBM), Derby (Apache), etc. 5
Java Database Connectivity (JDBC) • An SPI developed by Sun Microsystems (rip), as part of JDK 1.1 (1997) • Intends to supply a unified DB access • Strives to make the underlying SQL-DB platform transparent (Implementation-wise) • Not always possible, due to the syntax dialects 6
Java Database Connectivity (JDBC) • Was a good start but: • Developers basically needs to maintain two data models: DB Tables + Code objects • A lot of “translation” code is needed: • Objects to SQL • Queries results to Objects • Abstraction level is very shallow • Complicated error-path • Queries may vary between DB implementations • API induces complicated code ! • Hard to keep it DRY 7
JDBC - Demo • Example • Students / Courses • Registration of students to courses • Take aways • Use try-with-resources block (Using in C#) • Note resources are actually final • ResultSet is a resource! (As well as Connection and Statement) • Still lots of boilerplate / use patterns • Preparing statements • Deciphering the results • Handling exceptions • Be careful of different SQL dialects • Join is hard 8
Things to lookout for • Inheritance • How do we maintain inheritance in Databases? • Objects relations • What happens if an object holds a list of objects? Objects to Relational translation is hard 9
Things to lookout for (cont.) • Entities Equality • How can we say if a row’s entity is the same as a certain Java object? • Pointers cannot be used as entities identifiers in the DB! • Example: Suppose multiple people with the same name exists. how can we tell them apart? • ID is not always available / makes sense • In a purely Java world this is easy: we have pointer to their objects • When DB are involved this is simply not true 10
Spring framework – JDBC template • A set of classes built on-top of JDBC to provide: • Resource creation / release • Better iteration over ResultSet • Parameter resolution from objects • Mapping rows to objects 11
Spring framework – JDBC template • A big step towards the right direction • API is much better • Less error handling • Short & Simple code • Framework does most of the “heavy lifting”: • Parameters from Java Beans • Java objects resolution using custom mappers 12
Spring framework – JDBC template • This is an example for an API evolution • First version introduced JdbcTemplate • Later, classes like NamedParameterJdbcTemplate wrapped JdbcTemplate but delegated most of the work to it • But still: • Queries may vary between different DB impl. • Manual objects resolution • Entity equality is still a concern 14
Object Relational Mapping • ORM Frameworks will: • Manage the DB structure • Map Classes to Tables • Map Objects to Rows • Maintain at most one object in memory per DB row • We will: • Not worry about Inheritance • Not worry about Entity Equality • Write less code • Write simpler code 15
Hibernate • Provides a declarative way of mapping objects to their corresponding data • Defines an external DSL named HQL, which is similar to SQL, only it deals with objects • Meaning, instead of querying tables for rows, we now query Classes for instances • For example, getting all students born after 1985: dob is a class member of type Date Student is a Java class 16
Hibernate • Simple configuration via Annotations • Specification via XML is also supported • Java Beans defines tables • Can also explicitly define (table/column names) • Subclasses objects extends the tables • Complex objects relations • Objects which points to other objects • Automatically maintains the relational tables • No need to worry about SQL dialects • As powerful as JDBC 17
Hibernate – Entity Equality • Mapped objects now need to declare an ID member • Hibernate resolves row-object identity 18
Hibernate – API • Common actions are part of the API: • Adding an object: • Getting result objects as a list: • Fluent API 19