Total members 11893 |It is currently Thu Nov 14, 2024 5:52 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka






 Project Name:   Persist in DataBase with AspectJ (AOP)
 Programmer:   Mariano Lorente
 Type:   DataBase
 Technology:  Java, AspectJ, JDBC, Aspect Object Programming (AOP)
 IDE:   Eclipse
 Description:   Evidently this is not the definitive solution to give persistence to an object of a class, but it approaches us one of the multiple forms to do it.

Maybe you throw in lack the code of the CapaDatos class, but it is provided in zip of the example, as well as the one of other classes or interfaces nonmentioned.

Attachment:
aspecto.jpg
aspecto.jpg [ 4.07 KiB | Viewed 7067 times ]

html code
<TABLE ID="Table1" width="100%" cellpadding="0" cellspacing="0" border=0>
<tr>
<td class="txtnormal" width="510">
<H1>Persisto luego existo</H1>
<br>
<P align="justify"><b>Sumario:</b><BR>
One of the multiple forms to give persistence in data base to an object of a class.
</P>
<P><b>Autor:</b><br>
Mariano Lorente</P>
<P><b>Fecha:</b><br>
30 de Agosto 2004</P>
<P><b>Nivel:</b><br>
Medio
</P>
<P><b>Entorno:</b><br>
Eclipse, Java, AspectJ, JDBC, Aspect Object Programming (AOP)<br>
</P>
<br>
</td>
<td>
</td>
</tr>
<tr>
<td class="txtnormal">
<BR>
<BR>
<br>
<ul class=download>
<li><a href='Persist/Persist_src.zip'>Download source files - Kb</a></li>
<li><a href='Persist/Persist_demo.zip'>Download demo project - Kb</a></li>
</ul>
<p><img src='Persist/aspecto.jpg' height='135' width='120'></p>
<b>Indice:</b><br>
<A href="#C1"><img src="/Images/Line.gif" border="0" width="3" height="12" />
Introduction</A><br>
<A href="#C2"><img src="/Images/Line.gif" border="0" width="3" height="12" />
Aspect abstract</A><br>
<A href="#C3"><img src="/Images/Line.gif" border="0" width="3" height="12" />
Aspect clase Cliente</A><br>
<A href="#C4"><img src="/Images/Line.gif" border="0" width="3" height="12" />
Test</A><br>
<A href="#C5"><img src="/Images/Line.gif" border="0" width="3" height="12" />
Conclusion</A><br>
<br>
<br>

<A class="ContentTitle" name="C1">Introduction</A>
<p align="justify">
The power to in a while provide dice to an object of a class persistence, can make see the capacity us that has the
Aspects. Since we can give this behavior to the objects of a class, that does not have it, if to have to change its
codification.<br>
<br>
There will be people who will understand that the persistence is not an aspect of a class, since a part inherent of
the class at issue can be considered and therefore it is not possible to be understood like an aspect, mainly when
speech of business objects. But the certain thing is that when we spoke of ' objects of negotiated
we say this same one and not ' objects of businesses persistentes'. As it is understood a business object contains
the logic of the business of supports this class. Another aspect is as this information in a data base keeps to make
it persistent.<br>
<br>
Let us consider another situciación, we suppose that we have developed several systems of persistence, one that stores
the data in XML, another one in flat text files, another one in data base.<br>
<br>
Now we put the following example of migration.<br>
<br>
In a while dice we have developed an application in which we have a class of Configuration
that stores the data of configuration of the application, due to the initial use of this application we have considered
which we are going to keep these data from configuration in a file XML, but with time this application has grown and
now it is that the configuration data we needed them to have a network since is necessary that they share all the corporative
applications in installed her, for it we have chosen to keep the data in data base since all the equipment will be able
to have access to the data base. If the class were programmed with aspects Configuration
it would not undergo any change since the only thing that we would change would be the aspect to apply to this class.<br>
<br>
Next we are going to see the way that has been followed to give persistence to the objects of a class.
</p>
<br>
<br>

<A class="ContentTitle" name="C2">Aspect abstract</A>
<p align="justify">
In the first place first that has been developed it has been an abstract aspect called Persistence that goes has
to allow to extend it with other aspects. This abstract aspect contains all the points of abstract cuts that define
the behavior of the aspect in a while given.<br>
<br>
The declaration of the points of abstract cuts is due to sobrewrite in the aspect that extends the abstract aspect,
capturing the execution of the tie points that wish for the execution of the point of wished cut.<br>
<br>
This abstract aspect as well extends the CapaDatos class
that contains all the functionality to give persistence to an object.<br>
<br>
When extending the CapaDatos class we accede to the functionality
of this class from the aspect, just as if one had declared all
its functions within the own aspect.</p>
<br>
<pre>
<b>Figura 1.</b> Persistencia.aj

package persistencia_Aspect;

import java.sql.*;
/**
* @author [email protected]
*/
abstract aspect Persistencia extends CapaDatos {

declare parents : FormularioCliente implements ObjetosRelacion;

Persistencia() {
super();
System.out.println("constructor Persistencia");
}

abstract pointcut quieroLeer();
abstract pointcut quieroGrabar();
abstract pointcut quieroBorrar();

after() : quieroLeer() {
leer();
}

before() : quieroGrabar() {
grabar();
}

before() : quieroBorrar() {
borrar();
}

public void FormularioCliente.addObjetoRelacion(Object obj) {
addObjeto((Cliente)obj);
}
}
</pre>
<p align=justify>
Lo primero que se puede apreciar en la declaración del aspecto es el siguiente código:<br>
<pre>
declare parents : FormularioCliente implements ObjetosRelacion.
</pre>
<br>
Here this indicating itself to him that the FormularioCliente class
(In one more a developed implementation a
form would be a class representing bases) goes has to implement the
ObjetosRelacion interface.<br>
<br>
At the end of the declaration of the aspect the following code can be seen:
<pre>
public void FormularioCliente.addObjetoRelacion(Object obj) {
addObjeto((Cliente)obj);
}
</pre>
<p align="justify">
Where the method has been implemented that contains the ObjetosRelación interface.<br>
Not being the intention of this article treating the relations between objects,
a basic implementation and of the
relations between objects has been made minimum.<br>
<br>
Will be an aspect the relations of objects?<br>
<br>
Next we are going to see the most important part of the declaration of the aspect. </p>
<pre>
abstract pointcut quieroLeer();
abstract pointcut quieroGrabar();
abstract pointcut quieroBorrar();

after() : quieroLeer() {
leer();
}

before() : quieroGrabar() {
grabar();
}

before() : quieroBorrar() {
borrar();
}
</pre>
<p align="justify">
Three points of abstract cuts are declared, that will be due to sobrewrite in the aspects
that the present one extends.<br>
As we can see this does not prevent that they are possible already to be declared
advice for these points of cut.
Within these advice is called to the methods contained within the CapaDatos class
since we have extended to him
and for that reason we can make use of them.</p>
<br>
<br>

<A class="ContentTitle" name="C3">Aspecto Cliente</A>
<p align="justify">
One of the idea at the time of carrying out this small project, has been to have
to write the smaller code possible to
give persistence to the objects of a certain class. And a code similar to the
PersistenciaCliente aspect will be
the one that we will have to write to give persistence to the objects
that we wish.</p>
<pre>
<b>Figura 2. </b> PersistenciaCliente.aj

package persistencia_Aspect;

import java.sql.*;
/**
* @author [email protected]
*/
aspect PersistenciaCliente extends Persistencia {

PersistenciaCliente() {
System.out.println("constructor PersistenciaCliente");
}

pointcut quieroLeer() : execution(* *.leer_click());
pointcut quieroGrabar() : execution(* *.grabar_click());
pointcut quieroBorrar() : execution(* *.borrar_click());

public String[] clave() {
String[] a = new String[] {"IdCliente={$getId}"};
return a;
}

public String sqlIdentityField() {
return "{$setId}";
}

public String sqlSelect() {
return "SELECT IdCliente{$setId}, Nombre{$setNombre}, Email{$setEmail} FROM Cliente";
}

public String sqlUpdate() {
return "UPDATE Cliente SET Nombre='{$getNombre}', Email='{$getEmail}'";
}

public String sqlInsert() {
return "INSERT INTO Cliente(Nombre, Email) VALUES('{$getNombre}', '{$getEmail}')";
}

public String sqlDelete() {
return "DELETE FROM Cliente";
}
}
</pre>
<p align="justify">
Once sight all the code that composes the PersistenciaCliente aspect
we are going to enter to develop the detail of
the same one.<br>
<br>
In the following code:
</p>
<pre>
pointcut quieroLeer() : execution(* *.leer_click());
pointcut quieroGrabar() : execution(* *.grabar_click());
pointcut quieroBorrar() : execution(* *.borrar_click());
</pre>
<p align="justify">
we can suspect that it is the code that on writes the points of abstract cuts
defined in the aspect Persistence.
And we suspected well because he is that what does. As it indicates the first
point of cut we want that the cut
point is executed when the execution of the method is captured leer_click.<br>
<br>
As we can appreciate has not declared no advice with which the code of the advices of the abstract aspect will
be executed Persistence.<br>
<br>
The rest of methods is not more than on writing of methods of the CapaDatos
class since the abstract aspect Persistence extends
this class and the PersistenciaCliente aspect extends the aspect Persistence.<br>
<br>
Note: the words that accompany sentence SQL and key it is not more than a
form to indicate in that functions we want to establish the values or of which
functions we want to recover the values. Thus we are parseando the values of the
fields of the layer from data to the fields of the business object.<br>
<br>
Let us take as example the following sentence:<br>
<i>SELECT IdCliente{$setId}, Nombre{$setNombre}, Email{$setEmail} FROM Cliente</i><br>
<br>
In previous sentence SQL it is being indicated to him that it recovers of the data base
the IdCliente, Name and
email of the table Client. What no longer {is normal is the following code$$setId>}
, evidently this is not code
SQL but it goes has to serve to us to make the parseo between the data of the data
base and the functions of the
business object Client. Thus we will establish or recover the values of the methods of
the business object.</p>
<br>
<br>

<A class="ContentTitle" name="C4">Test</A>
<p align="justify">
Finally single it is to see the class that goes has to allow to us to make the
opportune tests. It is necessary to
mention the following code formulario.addObjetoRelacion(cliente)
since object relates them to the
FormularioCliente class. Account is necessary to occur that the
FormularioCliente class does not contain the
method at issue and that this provides through the abstract
aspect Persistence to him.</p>
<pre>
package persistencia_Aspect;
/**
* @author [email protected]
*/
import java.lang.reflect.*;
public class Test {
public static void main(String[] args) {
FormularioCliente formulario=new FormularioCliente();

Cliente cliente=new Cliente();
cliente.setId(1);
Cliente cliente2=new Cliente();
cliente2.setId(2);

formulario.addObjetoRelacion(cliente);
formulario.addObjetoRelacion(cliente2);
formulario.leer_click();


System.out.println("nombre del 1: " + cliente.getNombre() +
" ruta: " + cliente.getEmail());
System.out.println("nombre del 2: " + cliente2.getNombre() +
" ruta: " + cliente2.getEmail());

cliente.setNombre("nombre del 1 cambiado");
formulario.grabar_click();


cliente2.setNombre("nombre del 2 cambiado");
Cliente cliente3=new Cliente();
cliente3.setNombre("nuevo");
cliente3.setEmail("Nueva");

formulario.addObjetoRelacion(cliente3);
formulario.borrar_click();
}
}
</pre>
<br>
<br>

<A class="ContentTitle" name="C5">Conclusión</A>
<p align="justify">
Evidently this is not the definitive solution to give persistence to an object of
a class, but it approaches us one
of the multiple forms to do it.<br>
<br>
Maybe you throw in lack the code of the CapaDatos class, but it is provided
in zip of the example, as well as the
one of other classes or interfaces nonmentioned.
</p>
<br>
<br>
<P align="right">
</P>
<br>
</td>
<td align="right" valign="middle" width="130"></td>
</tr>
</TABLE>





Attachments:
Persist_src.zip [7.86 KiB]
Downloaded 875 times
Persist_demo.zip [37.93 KiB]
Downloaded 816 times

_________________
Please recommend my post if you found it helpful. ,
java,j2ee,ccna ,ccnp certified .
Author:
Expert
User avatar Posts: 838
Have thanks: 2 time

updated.


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Persist in DataBase with AspectJ (AOP)
 2 different database     -  
 database connection     -  
 information about database     -  
 List all database in php     -  
 Selecting a Database in php     -  
 Close database connections in php     -  
 POPULATING DROPDOWN IN MY DATABASE USING PHP     -  
 How to connect database with Applets     -  
 Creating a Database in MySQL     -  
 dump Mysql database     -  



Topic Tags

Java Projects, Java JDBC
cron






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com