Friday, February 18, 2011

Object Relationship Mapping (ORM)


Introduction
  • Relational Database: In a relational database, data is related together from table to table via special columns called primary and foreign keys.
  • OOPs : We relate entities via references to each other through composition and aggregation.
  • Object Relational Mapping (ORM) : Defines a mapping strategy to relate Java objects to relational tables.
  • JPA : Defines relationships using fields and annotations in the entity classes. JPA mappings are established via annotations. 


Major Relationship Types Supported by JPA


1.  One-to-many

  • Relate a row in a parent table to one or more rows in a child table.
  • Relationships can either be defined as bi-directional or uni-directional.
  • Two tables are related in a one-to-many (1—M) relationship if for every row in the first table, there can be zero, one, or many rows in the second table
  • For every row in the second table there is exactly one row in the first table.
  • The one-to-many relationship is also referred to as a parent-child or master-detail relationship.
  • One-to-many relationships are the most commonly modeled relationship.
  • eg: Student and Course
    • One student can apply for one or more courses.
2.  One-to-one
  • A single row in one table is related to a single row in another table.
  • Two tables are related in a one-to-one (1—1) relationship if, for every row in the first table, there is at most one row in the second table.
  • True one-to-one relationships seldom occur in the real world. 
  • This type of relationship is often created to get around some limitation of the database management software rather than to model a real-world situation.
  • eg: Student and Address
    • One Student can have exactly one Address
3.  Many-to-Many
  • Rows from each table are related to rows in another table.
  • Two tables are related in a many-to-many (M—M) relationship when for every row in the first table, there can be many rows in the second table
  • For every row in the second table, there can be many rows in the first table.
  • Many-to-many relationships can't be directly modeled in relational database programs
  • These types of relationships must be broken into multiple one-to-many relationships
  • eg: Student and Teachers
    • One Student is taught by many Teachers and one Teacher has many Students.
4.  Many-to-One
  • A reference from a child entity back to its parent.
  • eg: Course and Student
    • Many Courses are applied by one Student.
5.  Inheritance
  • JPA supports object-based inheritence and provides several physical models to map this onto a database.


Introduction to JPA


Introduction
  • Saving : During database transactions, Java Objects are saved into database as table rows.
  • Retrieving : Table rows are returned and converted to Java objects.
  • Impedance mismatch
    • a) Database tables, rows, and columns are relational and have database types. 
    • b)Java objects can be object oriented and have Java types and behaviors.
    • It is vital that a Java programmer must spend a lot of time in SQL and JDBC programming.
  • Advantage : Java Persistence API (JPA) helps developers to avoid the task of SQL and JDBC programming. 
  • OOP : Java classes and their fields are mapped to database tables and columns. This mapping will help the programmers to just concentrate on Object Oriented principles.
Two Main Tasks of JPA

1. Object Relation Mapping (ORM)

  • Mapping : Entity classes and their properties (fields) are mapped to relational database tables and columns.
  • Relationship : Relationships (one-to-one, one-to-many, many-to-one, one-to-many, hierarchical) between the entities are mapped to primary key and foreign key columns in the database tables.
2. Operations on data

  • CRUD : Four main operations in any application are create, read, update and delete.
  • Translation : JPA translates Java operations into database operations.
  • Persistence : Translation of operations persist Java objects into database tables with JPA generating JDBC calls.
  • SQL statements : During Persistence SQL statements are automatically created behind the scene by JPA.

Philosophy of JPA
  • Two Sides of the Same Coin : Java objects and database rows represent same piece of data.
  • Intermediator : JPA is the intermediator among Java Objects and database rows.
  • Single  Operation : Operations on Java objects are now equivalent to database operations.  
  • Perfect Marriage : Java objects and database rows are no more two, but one. Developer deals with Java objects and JPA translates them into database rows. 

Tuesday, April 27, 2010

WEB-INF & META-INF

What are WEB-INF and META-INF directories?
  • Both directories exist in a Java Web Application (J2EE) which runs on a Tomcat Server.
META-INF
  • It is automatically created while we create a war file.
  • After deploying and extracting the war file in tomcat server (webapps director), theoretically it is safe to delete.
Note:
  1. While we create a jar file, an unbrowsable META-INF directory which contains the manifest (list of contents) is also created automatically. War file is has the same nature as jar file, so that when we create a war file, we get a META-INF directory automatically.
  2. War (Web Archive) file is a compressed file. It is a collection of 1) Java Server Pages (JSP), 2) Servlets, 3) Java Classes, 4) XML files, 5) Tag Libraries, 6) Static web pages (HTML and related files).
WEB-INF
  • It is a mandatory directory (vital component) and java web application will not work without it!!!.
  • Name of the directory has to be in the capital letters separated with a hyphen.
  • This directory is created automatically while a war file of the web application is created.
  • Under normal condition, any change in this directory is not required.
  • This directory contains a hierarchy in which 1) the necessary configuration information for the web application, and 2) all the class files for the servlets and classes that are called up by the JSPs (Java Server Pages)
  • WEB-INF is a mirror META-INF, but the former doesn't include any documentations!
  • The application deployment descriptor file (web.xml) is a standard XML file under this directory.
  • WEB-INF comes under the root directory of the application.
  • Anything comes in the root directory excluding WEB-INF are publicly available and can be accessed using URL from the browser.
  • WEB-INF directory is a private area of the web application, any files under WEB-INF directory cannot be accessed directly from browser by specifying the URL like http://somesite/WEB-INF/someresource.html. Web container will not server the content of this directory.

Note:
  1. Any resources like JSPs or HTML document that don’t wish to be accessible directly from web browser, should place it under WEB-INF directory.
  2. A java web application has the directory structure shown below J2EE Web Application Directory Structure
  3. WEB-INF/web.xml : web.xml is called the web application deployment descriptor. This is a XML file that defines servlets, servlet mappings, listeners, filters, welcome files etc. Deployment descriptor is a heart of any J2EE web application, so every web application must have a web.xml deployment descriptor directly under WEB-INF folder.
  4. WEB-INF/classes : The classes directory is used to store compiled servlet and other classes of the application. If your classes are organized into packages, the directory structure must be reflected directly under WEB-INF/classes directory. The classes directory is automatically included in CLASSPATH.
  5. WEB-INF/lib : Lib directory is used to store the jar files. If application has any bundled jar files, or if application uses any third party libraries such as log4j, JDBC drivers which is packaged in jar file, than these jar files should be placed in lib directory.
  6. All unpacked classes and resources in the /WEB-INF/classes directory, plus classes and resources in JAR files under the /WEB-INF/lib directory are included in classpath and made visible to the containing web application.



Monday, March 22, 2010

My Journey

'My Journey' through different concepts developed by human beings starts here. Each concept is emerged from the wisdom given by 'The Truth', whom most of us call God.

I love technology, because it always wonders me! I admire the efforts and enduring hard work of the human beings towards each and every finding happens.

Ultimately, it is the Attitude!

I always believe that 'Attitude is Everything'!!!

I would like to share my findings here in this www-pallikkoodam-org. I might not be right always but you can correct me whenever you want!