Você está na página 1de 3

JPA Mini Book First Steps and detailed concepts Reasons that led to the creation of JPA

One of the problems of the Object Orientation is how to map the objects as the database requires. It is possible to have a class with the name Car but its data is persisted in a table named TB_CAR. The table name is just the be innin of the problems! what if the Car class has the attribute "name# but in the database $ou find the column %TR_&A'(_CAR) The basic *ava framewor+ to access the database is the *,BC. -nfortunatel$! with *,BC! a lot of hand wor+ is needed to convert a database quer$ result into *ava classes. Bellow it is a code that shows how to transform a *,BC quer$ result into Car objects. / 0 1 2 3 4 5 6 7 / 8 / / / 0 / 1 / 2 / 3 / 4 / 5 / 6 / 7 0 8 0
import java.sql.*; import java.util.LinkedList; import java.util.List; public class MainWithJDBC public static void main!"trin#$% ar#s& thro's ()ception Class.*or+ame!,or#.hsqldb.jdbcDriver,&; Connection connection - .. #et a valid connection "tatement statement - connection.create"tatement!&; /esult"et rs - statement.e)ecute0uer1!,"(L(C2 3,4d3,5 3,+ame3, 6/7M 3,Car3,,&; List8Car9 cars - ne' LinkedList8Car9!&; 'hile!rs.ne)t!&& Car car - ne' Car!&; car.set4d!rs.#et4nt!,4d,&&; car.set+ame!rs.#et"trin#!,+ame,&&; cars.add!car&; : *or !Car car ; cars& "1stem.out.println!,Car id; , < car.#et4d!& < , Car +ame; , < car.#et+ame!&&; : connection.close!&; : :

/ 0 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 1 8 In the code above it is possible to see all the code needed to transform a *,BC quer$ result into classes. Ima ine if this class had 18 attributes9 To ma+e the scenario even worse! ima ine if a class with 18 attributes and with a relationship to another class that has 18 attributes. (. . a Car class ma$ have a list of :erson and the class :erson has 18 attributes to be populated. Other disadvanta e of *,BC is its portabilit$. The quer$ s$nta; will chan e from one database to another. <ith Oracle database the command RO<&-' is used to limit the amount of returned lines! but with %ql%erver the command is TO:. Application portabilit$ is a problematic issue when database native queries are used. There are solutions to this +ind of problem! e. . a file with the quer$ could be stored outside the ear file. To each database vendor a sql specific file would be required. It is possible to find other problems when developin with *,BC li+e. update a database table and its relationships! do not leave an$ orphans records or an eas$ wa$ to use hierarch$.

What is JPA? What is a JPA Implementation?


The *:A was created as a solution to the problems listed in the previous pa e. The *:A ideal is to wor+ with the *ava classes without the needin of +nowin the database specific details= *:A will do the wor+ for the developer. An eas$ definition to *:A is. "A roup of specifications >a lot of te;ts! re ulari?ations and *ava Interfaces@ to define how a *:A implementation should behave#. It is possible to find

some *:A implementations on internet! e. . Aibernate! Open*:A! (clipseBin+ and the "new born# Batoo. The *:A implementations are free to add e;tra codes! annotations that are not present in the *:A specification! but it must implements all *:A rules. The first *:A solution to help in the application portabilit$ is the wa$ of mappin the database table into the class. <e will see ahead that it is possible to map the column of the tables table into *ava classes! and it will be no problem if the database column name is different of the class attribute name. *:A created a database lan ua e named *:CB to do the database queries. The advanta e of the *:CB is that the quer$ can be e;ecuted in all databases. /"(L(C2 id5 name5 color5 a#e5 doors 6/7M Car The quer$ above could be translated to the *:CB below. /"(L(C2 c 6/7M Car c &otice that the result of the quer$ above is "c#! that means! a car object and not the fieldsDvalues found in the database table. *:A will create the object automaticall$. If $ou want to see several wa$s to run a database quer$ with *:A clic+ here. *:A will be responsible to translate de *:CB quer$ to the database native quer$= the developer will not need to worr$ about which is the required sql database s$nta;.

Você também pode gostar