Desde Cero - con Spring e Hibernate - draft 0.1 - Español

36
Cero con Spring e Hibernate Nicolás Cornaglia http://nikofactory.blogspot.com 2004

Transcript of Desde Cero - con Spring e Hibernate - draft 0.1 - Español

Page 1: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

�����Cero con Spring e Hibernate

Nicolás Cornaglia

http://nikofactory.blogspot.com 2004

Page 2: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� �������� �����������

� � ����

Contenidos

1� Introducción�����

2� Preparación del entorno�����2.1� Directorios�����2.2� Instalación SDK�����2.3� Instalación Eclipse�����

3� El proyecto 1° parte�����3.1� Análisis�����3.2� Creación del proyecto en eclipse�����3.3� Un aproximación a la arquitectura�����3.4� Test driven development����

3.4.1� Análisis������3.4.2� JUnit������3.4.3� El primer caso de prueba������

3.5� Cosmética�����3.5.1� Fonts������3.5.2� Tabs������3.5.3� JavaDocs������

3.6� Mas desarrollo�����3.6.1� Refinamiento de Project������3.6.2� Mas clases�����

4� Persistencia������4.1� Introducción a Hibernate������4.2� Xdoclet <doing…>������4.3� Ant <todo>������4.4� MySQL <todo>������4.5� Test Cases <todo>������

4.5.1� DBUnit <todo>����

5� Uniendo todo con Spring�����5.1� Inyección de dependencia�����5.2� Spring <todo>�����5.3� El Contexto <todo>�����

6� Coverage�����6.1.1� Emma <todo>������

7� Documentación������7.1.1� Javadoc <todo>������7.1.2� java2html <todo>������

8� ACEGI������

9� CVS������

Page 3: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� �������� �����������

� � ����

9.1� WinCVS <todo>������

10� Continuous Integration <todo>������

11� El proyecto 2° parte (Web)������11.1� Tomcat <todo>�����11.2� El proyecto <todo>�����11.3� Spring MVC <todo>�����11.4� Sitemesh <todo>�����11.5� Test Cases <todo>�����

11.5.1� jWebUnit <todo>��������

Page 4: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� �������� �����������

� � ����

1 Introducción

� ���������� ���������������������������������������������� ��������������� �����

��!�"���#���� �������$�%�#�%�������&���������������������#�%������������������ ���������

������������ ������������������#������������������������������������� ��'���������(���

!��������)����� ��������#������ ���������*�

+ ��������������,������������� ��������� ������������#� �� ������#������� �����-��

���� ������!������� *�������� �����-���������������������� ����������������� ������

./"/0#�����)��������������� ���!�����-�� ������� ������������������������ ���������

������� �������!�"���*�

1��� �������#������ ���������2� ���������� ������������ ��������������� ��������#� ���

�&���������� ������#������,����� ������� �.����3�������� ��(��� �0#�������� �����(��

�� �,��� #�,���������)����!� ����� �������&���������������������������������� �����������

���������������������������������������������������� ����������!�"���*�

4�� ���������5������6����"��7������� ����� �"���� �� �"�������#���8�������������� ��

������������#������ ���� �����-��������� ������������������������������� ��������*�

+����������������#����������� ������������������/����9��� ��

:http://raibledesigns.com/page/rd;#��������������������!�����������%� ����-�#� ����,�������� ������������������� ����������(��� �� �����%���-������+��6����

:http://raibledesigns.com/wiki/Wiki.jsp?page=AppFuse;� �5������<�%��:http://www.springlive.com;*�

Page 5: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ����������������������� �� ����

� � ����

2 Preparación del entorno

5����������,������ ���������=����"�������� �������� �#����� ��,��������� ��������������

/������ ������)�#� ����\����������������� ����� ������������<���2�>�4��2*�

2.1 Directorios

<����������������������������� ��������������������������������(������� �����������?�

��

1����������������� ,���������������� �������������� ,�����������������#����������,������

��� �����������!�����������work\������������������������������ ���������� �!����*����

%����������!�������-�#�������������� ����������� ������������������work\������ ����)�

���� ����,���)���� �����#������(��� ��C:\work\*�

���� ���������� �����������������#������������ ������� ����� ����������:����;#�

���� ��������� �������������������:�2������;� ��������������������:�;���� ����:�;�

:"��7�����;*�

2.2 Instalación SDK

1������������,���:������ ��)���)��)�� �;���������������������������� ���� �5@A����

$�%�*�1����� ����������� ��"������5���:http://java.sun.com/;#��������� �%���� ��.$�5���**��5@A0#�������� �������� ���������!������-�� ���������� �����work\downloads*�B��

�������������������������������&��� ����C����#� ��,���)������ �������� ����������

��������������� �#�����,������������������������������� ����������� �%���� �*�8� �,���

����������������&��,�����(����� ��%����-�������������������������������������������%�#�

�,���� ������� ������-��.�!! ���0#� ��,������������������������������� �5@A�������� �

����� �������!����������������*�

4���%�����(����� �*�2�#� ���(��������������������� �� �����work\jdks#��������������������

� ����������������� ��%����-���� �5@A*�5��)�������(���� ��%����-���**���D�����

�(��� �#� ������� ���������� ����������work\jdks\j2sdk1.4.2_05*�/������ �����

������������������������ ����,�����!��������%������������$�%������� ������,������

%����� ��������!��������%���������������������������!�������� ���������� ����*��

E����&������������ ��%����� �������������JAVA_HOME=work\jdks\j2sdk1.4.2_05� �

������������%JAVA_HOME%\bin��� ��%����� �������������PATH*�

2.3 Instalación Eclipse

� �F@��� ����������������(�������� �����:http://www.eclipse.org;#����,���F��� �$�F@�+�:http://www.jetbrains.com/idea/;� ����C�����:http://www.netbeans.org/;������������������%��� ��*�

Page 6: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ����������������������� �� ����

� � ���

<��������)�������(����� ��� �������� ��"��#������������������� �*�������

work\apps\eclipse#� �������������������������������eclipse.exe#���������� ��� �

����������.-data work\workspace0#������������� ����� ������ ������������������(��������

��������������������� �����*�

5���(���������� ���������������#� �������������� ��,���%������������ �����������?�

G�������������%����#�%������� �����������%��������!���������� ����?�Resources*�

Page 7: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ���������� ��� ������������

� � ����

3 El proyecto 1° parte

4������������� ��������%������� #������,��������������������� ����*��

F�������������#���������,������������� �����-������������%����������������������������!���

"����������������������� ��������������� �������!�"������� ������� �������� �����H1�

:�2������1����������;*�

�������������� �����:http://www.extremeprogramming.org;#�������������������� �����!������������������������������ �����!��������4����5��������� ������������������ ���

��� ������������� �����:F���������1 ������;� ��� ����� ��������� ���,�����������

�������� ��������� ���������!��� �:9� �����1 ������;*�����,������������������� �����-��

�!��������������%�����*�

5�������� �!���� ��#���������,��� ���4����5������������������������������� ��������#�

�������������� �� ������ �,������%����������������� ���!���������������*�

<��������������������'������������ �����4����5�������:)��������,���� ��������� ���

��������������������������.� ���0;#� ���������� ������������������������������#����

����������� #����������� ��� ��!������� ����*���������������������������������������

����������������������������������������������������.%� ���� 0#��&����������� ���� ����

�������� �������������������%����*�

<���4����5������������������ �������������������#�������������������������!�������������

���� �����������#���� ������ ��� ���������� ������*�I�����' ����#� ����������������������

����� �����#���%���������������������� �����-���������������� �� �����*�

1����(��� �#������� ���������-�����������)�����)��)��D���������������������������#�������

�����������D�����#���������������%� ����������DJ�K�JDL�D*�1��� ��,������ ������������

�������-�#� ���������� �����������������������������������D*�

3.1 Análisis

E� �%������������������%�#����������������� �����H1��������� ��������������� �����-��

H1#����� ��,��������������������!����������������������4����5���������� ������������

!����?�

5��� G�����-����������� ����*�� �������������������������� ����#���������� ��������#� �

!��)������������� �!��� �����-��:�����������;*�5��,���������������!���"��*�

5���G�����-����������������-�*�5������������������-������������� ����#�����!��)������

������� �!��*�

5���G�����-���������� ����*�5����������������� �����!���������� ����#���������!��)��*�

5��G�����-������������ *�� �G ������)����������� #����� ��,����������������������� ��� �

�������*�G������������������ #�������� �����#�� ��� ����#��������-�#�������������

������� �����������������*�

5�D�+�������-������������ ��������������-�*�4���%���������������,����������-��)�������

��������� #���������������� ��������*�

5���+�������-������������ �������� ����*�4���%���������������,����� �����)������������

���� #���������������� ��������*�

5���5����,��������� ���������� ������������������������ ����#������ ���!��������������#�

����� � ���������*�

Page 8: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ���������� ��� ������������

� � ����

G�������������%��#� ��!���������������� �����!�����������4����5��� �������#� ��,��� ���

� ����������� �����������������)���� �#� ����!���������2����������������������� ��

�����*�

1�����������������������!�������������������������������������� ����*�@����������

�)�����������������5��� � �������� ���������������-�*�1����������������������� �����������

H1#��������������������������������������������� ��#������������,������������������

��!���������������������������������������)��������������,��������������*�I������

������������������������ �����#� �������������!���� �������� �� �����#������,�������

���� ����������� #� ���������������������� ������*�

3.2 Creación del proyecto en eclipse

M������������ ��,���������������#�,������������� ���� ���������� ����*�

E����������� �����������%��������!���������� �����Resources������������������!�����#�

Java#������������ ������ ����'� ������������� ����?�File/New/Project/Java Project*�

G��� ��,������������� ������� �������� ������������!������ �������������� ����#���������

�����mwm?�

��� ����������������� ��������������������������src���������������!������?�

Page 9: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ���������� ��� ������������

� � ����

����!���������� ����(�������� ����,������������������ ��������������� ����� �������������

�� ������bin?�

���� ��,���!��� ��������� ��������-�#������������� ��������������� �������� �����������%��

Java?�

3.3 Un aproximación a la arquitectura

5�������,�������������� �����-��������������������"���:5��;#�,�����,����������������

������������������������� �����#������������#����*�:5��#�5��#�5��#����*;#�����(����,�������

� ��������� �������)���#������������������������ �������������?���� �����:5��;���

�������-��:5��;������� �:5�;���������:5�;� ����������� �� ��������� ������������������

� �����:5��;*�

����������������,������������ ������������2������������������������,�������������

�������������������?�

• G �����(�%������������������,�����*�

• 1����������������������������*�

• F����!���"��*�

• 4���������������������������������*�

Page 10: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ���������� ��� ������������

� � ����

3.4 Test driven development

� ������������ ����������������������public class Project{…}�������������#������

�����%������������������������2�����-����!�����������������!��������������� ���

��!�"���?�E@@���E�������%�����%� �������:http://www.testdriven.com;*�

��� ������������������-����� � ������������������!�������������#�E@@������ ����,���

�������������������� �������������������:����������;#� � �������������� ��-���������������

����������!���� ��*������!������� �������������:������������;� ������������������������

���������-����� ����� ���������� ���������� ��-������ ������� ���� �����:�����������;�

�������������������� �������?� ���� �����-��� ������ � ������������������*�

3.4.1 Análisis

NO���������������������������������������������������� �:5��;P��

O������������������� �������(����,���������(������� �,����������������!������� ���������

����!����������������� �����*�I�,������������ �����������������������������#�)�����

�)����.���3�0#�.!��)�����������0� �.!��)�����!��� �����-�0*�

5��������������)���� ��������������������-��"�������� ��������#�����������������#�����

������������!�����#�,���������������������� ������ ��,������������� �����Project#��� ��

��� � ����������������������������� ��������������������������������*�

1��������#�)�������������������������,������������������!������� ����*�1����� ��

������������������)�����������,���!��� ���� �����������������#� � ��,������ ���������

�����$4���*�

3.4.2 JUnit

$4����:)���?>>"""*(����*���;��������)�����������$�%������������������������� ����

�������-�#�,������%�������(����,�������� ����� ���!������� �����(�����'���������������� �

�(������������������������������ ��*�/����������! �2�-�#��2������ ���� ��������������� �

�(������ ����&��������3� ����#��2���������� ����� �������� ���������*�

/��)������ ���F@�������� ������%�����������������$4���#������������� ��� ����������

��������������������������$�%�*�

��� ����������#�����������,�������������� ����)�����������TestCase#�������� ���&������

�������test*���������������� ��-��������������#� ���� �����.+���������0������

����������� ����� ������� �������:assertNull(…)#�AssertEquals(…)*�E����&��

������������������������������������.5�����0#�������(����������!��������(�����

��!����������������������������� ��������������'�*�

3.4.3 El primer caso de prueba

5�����������������)���� ������������#���� ��������� ���!������� ������������ ����������

�����������������������������������*�

+�����,�������������������������%����������!�������� ��,��� ���������test������

������������������������#�������� ����'�File/New/Source Folder?

Page 11: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ���������� ��� ������������

� � �����

�������� �%�������&����,����������!����� ����������������� ���� ����#������ �botón

derecho/Properties/Java Build Path#��������������� ������������������Java Build

Path� ������� ����������test������������ ����#� ��������src*�

6��� ������������������� ����'�File/New/JUnit Test Case� ��� �������������������

����������������,��������������� �build path�� �*(������������������ ���(������� ���

� ��������������?�

<��������� ����� ������-��Yes#����������� ����(�������� ����������� ��������� ���

�������������� ����������������� ���������������?�

Page 12: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ���������� ��� ������������

� � �����

���� �Source Folder������������ �����&���������test#�������������������7����

com.mwm.model� �������������ProjectTest*�E����&��)�������,������������ ���

�&������setUp()� �tearDown()#���������� ��������������������*�

<���������������� ����������������Finish#�� ����� �����!��� ���� �����������%��Java������

� ����������?�

I�� ��-�������������������com\mwm\model\ProjectTest.java����� ����������?�

/*

* Created on Oct 14, 2004

*

Page 13: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ���������� ��� ������������

� � �����

* TODO To change the template for this generated file go to

* Window - Preferences - Java - Code Style - Code Templates

*/

package com.mwm.model;

import junit.framework.TestCase;

/**

* @author ncornag

*

* TODO To change the template for this generated type comment go to

* Window - Preferences - Java - Code Style - Code Templates

*/

public class ProjectTest extends TestCase {

/**

* @see junit.framework.TestCase#setUp()

*/

protected void setUp() throws Exception {

super.setUp();

}

/*

* @see TestCase#tearDown()

*/

protected void tearDown() throws Exception {

super.tearDown();

}

}

G����������)��������&������������������������ ��������-����������(������� ��� ����

Project#� � ����������-�������������������������������������*�

<��,�����������)���������������������������������� ������������������ ����������������

��������Project#�������� ������ ��&�����setUp()#��&�����,�������(�����������������

�(�������������&��������������*�E����&������%��)�������� ��&�����tearDown()������

. ������0� �����������,�������&������)�����)��)�� ���(�������� ���������� ���(�����-��

�� ������������&������������*�

<����#������������ ��&�����testNewProject()#����� ��!������� ��������������������

������������� ����������!����������������� ��� ����Project*�

� ����� �����!��� ����� �����������:������������������������ ������;?�

public class ProjectTest extends TestCase {

private Project project;

protected void setUp() throws Exception {

super.setUp();

project = new Project();

}

protected void tearDown() throws Exception {

super.tearDown();

project = null;

Page 14: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ���������� ��� ������������

� � �����

}

public void testNewProject() throws Exception {

project.setName("Mi primer proyecto");

project.setStartDate(new Date(0));

project.setEndDate(new Date(0));

assertEquals("Name", "Mi primer proyecto", project.getName());

assertNotNull("StartDate", project.getStartDate());

assertNotNull("EndDate", project.getEndDate());

}

}

<��,�����������)�����������������&���������������testNewProject()#������� ����� ��

�������������Project��������������������������������� ��&�����setUp()#������� ���������

�����������#� ������������� �����,��������������������� ����!������-�*�

1����%���!������ ���������� ���� ����#���� �������� ��&�����assertEquals(String arg0,

Object arg1, Object arg2)����$4���#����������������������� ���������������������

����������������%���� ����������������������#�� ���(����������������� �������������������

�� ���(�������������������� ��������*�

1���� ���!��)��#������(��� �#���� ���������assertNotNull(String arg0, Object arg1)#�

��������� ������������ ��������!������ �����������������#� ����� �����������%��������

� ���(������������������������ ����*�

5��� �������� ����&������.������0�!� �#�� ��&���������������!��� ������ �!� ���*�

1���� ��,��������������������������������� ��������������,��������,����������� �*�1����

�������� ������*�+)������������.�� ������0���)���������� ����Project#����� ����&������

����������������������� ���������������������#��� � ����������� ������������5��*�

1��� �������#������������������%��� ����:File/New/Class;#������ ���������-�����

��������������������!����� �����,�������������� ���������������?�

/*

* Created on Oct 14, 2004

*

* TODO To change the template for this generated file go to

* Window - Preferences - Java - Code Style - Code Templates

*/

package com.mwm.model;

import java.util.Date;

/**

* @author ncornag

*

* TODO To change the template for this generated type comment go to

* Window - Preferences - Java - Code Style - Code Templates

*/

public class Project {

//~ Properties =============================================================

private String name;

private Date startDate;

private Date endDate;

Page 15: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ���������� ��� ������������

� � �����

//~ Getter/Setters =========================================================

/**

* @return Returns the endDate.

*/

public Date getEndDate() {

return endDate;

}

/**

* @return Returns the name.

*/

public String getName() {

return name;

}

/**

* @return Returns the startDate.

*/

public Date getStartDate() {

return startDate;

}

/**

* @param endDate The endDate to set.

*/

public void setEndDate(Date endDate) {

this.endDate = endDate;

}

/**

* @param name The name to set.

*/

public void setName(String name) {

this.name = name;

}

/**

* @param startDate The startDate to set.

*/

public void setStartDate(Date startDate) {

this.startDate = startDate;

}

}

B��������������������������� ���)�����������������#� ��,���)������������ �����%��

� �������com.mwm.model�:� ����������7����,���� �������������������,�����(�� ����������

!������src;*�

5� ������,���������������������������!�������������#�� �������� ��� ����ProjectTest�

����� ����-����� �������)�#� �� �������� ������-���� ����'�Run/Junit Test*�5������

������� ��%�����$����#���� ����� ��������������%���,��������)��!������������ ����������

�����������:����������%��������� ��������;?�

Page 16: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ���������� ��� ������������

� � ����

5���������������,���������������)�������������������� ������������>����������

:�������>�������;���������������������� �����%�� ���� ����&�����#� ���������������-������ ��

,��������� ��������������������������,��� ����&������.��� �����0�!������� ��*�5���

�������#�� �)���� ��)��������������� ���!��������������E@@� � ����� �����-�����$4���#�

��������� �������������������� ������ ���������*�

3.5 Cosmética

+���%��)������������������������ ���� ����#��������������� ����!�������� ������

� ����#���������������������������������������� ������������� �,��������(����*�

3.5.1 Fonts

1������ ���������!��������������)������������� ��%���������������-������-����#������ ��

��� ���������������������#���� ������!��������#� ��!������������!������� �������*�

3.5.2 Tabs

<������������������������� ��!������ ����������������#� ��,�������������������������

%���� �����-��������������-����������!������������������������%��#� ������� ������������

������� ������-��Insert spaces for tab?�

Page 17: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ���������� ��� ������������

� � �����

3.5.3 JavaDocs

<���$�%�@������������!�������������������������������������-����#� ��� ���������

���%���� ���� ��������,���� ������������������-���������� �������&����#������������� �

�������������������������*�<��,���)��������)�����������!������ ���������� ������������

� ���� ������Window/Preferences/Java/Code Style/Code Templates/Comments?�

1���� ���� �����:Types;#���������������� ��7�� ����������� ��� ����(�%�#����������

�2�����-��.html#������������������������ ���������������������)�� ������ ��-�����!������

������ ���%���� ������� ���(�%�����*�E����&������������������������������� ������� �

����@author#�:�,����������������������������-������������� ����-��������� ��������

���������?��<a href="mailto:[email protected]">Nombre</a>;#� ������������������ ���

������ �GM5�:������������������ ����;*�

B����������������!���������������������� ���������������������-�#�������������� ���

�� �����?�����������#�����������#�����������*�

4���� ����������������������� ���(�%������ �� ��������� �������������&�������� ��-�����

���������������'����������2���#� �� �������� ���������: ������������������;���

���������� ��-��������� �����*�

��� ����&�������������������:Overriding methods;����������?�

/**

* ${see_to_overridden}

*/

Page 18: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ���������� ��� ������������

� � �����

��� ������%������)�%������� ����:Code/New Java Files;�������������� ��������������#�

,��������������?�

${package_declaration}

${typecomment}

${type_declaration}

I�����' ������ �Catch block body�����������(�� �����������������)���#� �������� ��

������� ��������?�

log.error("Error: " + ${exception_var}.getMessage());

���������������� �����Log4J������ ���������������������:������������������ ����;*�

1���' ����#������������� %������������!����� ��������������� ������������������ �����

������������������� �����Project� �ProjectTest�:Alt+Shift+J�������� �����������

�&�������� ���;#���������������&���������� �����������������!��)����������������

:Alt+Shift+F�������� ��-����;*�

3.6 Mas desarrollo

������&������������������ ���������������� � ��������������� ���������-�����:AF55;�,���

�������!����:E@@;#� ������������������������������ �� �������������� ���� �����%������ �

��� ����*�AF55����� ������ ������.A����F��5��� ��5�����0�:/����� ������ �����'����;�

3.6.1 Refinamiento de Project

G��������������)������������������� ������������������������,�����������:� �� ������

����)�����)�;������� ���� ����*�

1����(��� �#� �������!��������������������� ��������!����������������#����� ��,��������

��� ������������������������������*�1����� �#������������)���������&�����,����������

����������-�� ����������������������������������������� ��� ����Project*��

1����)������ ���������)����������������������������������&�����#����� ��,�����������

������������?��

�*� F��������������,����������� ����)������ ���,���,��������������������#�����������

�����������I+QBF����H1*�I+QBF����� ������ ������.I���+���R��Q�����B����

F�0�:B�� ��%��������������;�

�*� +��������� ����������������������������� ��� ���#���������&��������%���!�����-�*�

�*� 9�%��������������� ������ ������������&���������%���!�����-������� ���

���������� ����������*�

I+QBF���������!����,������)�����������3����������������� �����-�#� �� �����3��� ��

� ����Project#���������������%�������,������������������������������������*�I�

�����&�������������%��������������)��������������������&���������%�� ��#����� ��,���

�������������� ������-���#� �������������company� �description��� ��� ����Project? private String description;

private Company company;

����������������������������������� ����������#���� ��������� ��������������� ��������

��)�����%������������������*�

1����,��������' ����������������� ���������������#�����%���������������������������

����� ����Company#�,��������)���������������� �����������%������������#�� �������?�

package com.mwm.model;

Page 19: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ���������� ��� ������������

� � �����

/**

* <p>

* <a href="Company.java.html"><i>View Source</i></a>

* </p>

*

* @author Nicolas Cornaglia

* @version $Revision:$ $Date:$

*/

public class Company {

//~ Properties ===========================================================

private String name;

//~ Getters/Setters ======================================================

/**

* @return Returns the name.

*/

public String getName() {

return name;

}

/**

* @param name The name to set.

*/

public void setName(String name) {

this.name = name;

}

}

3.6.2 Mas clases

5��'�� ��,������������� ����� �5����������������������� �������������-��������������

�**��� ���� ����*�������������-�������������������������� �������#� �����������-�� � ���

!��)������������� ����!��*�

G����������������#���������������&�������������������������ProjectTest?�

/**

* Test put & remove a Iteration

* @throws Exception

*/

public void testPutRemoveIteration() throws Exception {

Long id = new Long(1);

Iteration iteration = new Iteration();

iteration.setId(id);

project.addIteration(iteration);

assertEquals("Iteration", iteration, project.getIteration(id));

assertEquals("Iteration project", project,

project.getIteration(id).getProject());

project.removeIteration(iteration);

assertNull("Iteration not removed", project.getIteration(id));

}

Page 20: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ���������� ��� ������������

� � ����

����������������������� ������������ �#� ��,�������2���������%��� ��� ����Iteration#� � ��

� ����Project���������� ����&������addIteration(…)#�getIteration(…)����

removeIteration(…)*�E����&��)�����������������������!�����������������-�#������

�������������������� �����%��������������������� ��� ���*�

B���,����������)����� ����������������������!������ ����������������,���)�����

����3���#������� ��� ����Iteration?�

public class Iteration {

//~ Properties ===========================================================

private Long id;

private String name;

private String description;

private Project project;

private Date startDate;

private Date endDate;

//~ Getter/Setters =======================================================

.

.

.

}

��������� ���� ���-������Project�:����������������%������������� ����������;#� � ���

�&������!� ������������������������ ��������������?�

private Map iterations = new HashMap();

public Iteration getIteration(Long id) {

return null;

}

public void addIteration(Iteration iteration) {

}

public void removeIteration(Iteration iteration) {

}

���� ��,������������ ������������������� ���#������������������ ��(��������?�

Page 21: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ���������� ��� ������������

� � �����

���,��� ����&������,���)������������������- ��� ���� ������ ��,����������������#� �

getIteration(id)���%�� %��null#�)��������,���$4���������������������������� �� �����

D�?�junit.framework.AssertionFailedError: Iteration expected:

com.mwm.model.Iteration@3ee284 but was: <null>#�����������,��� �������������

assertEquals("Iteration", iteration, project.getIteration(id));

)��!� ���*�

8����������������,����������&��������� ������ ���'�� ?�

/**

* @param id

* @return

*/

public Iteration getIteration(Long id) {

return (Iteration)iterations.get(id);

}

/**

* @param key

* @param iteration

*/

public void addIteration(Iteration iteration) {

iteration.setProject(this);

iterations.put(iteration.getId(), iteration);

}

/**

* @param key

*/

public void removeIteration(Iteration iteration) {

iterations.remove(iteration.getId());

}

������������� ��.�������0�������%������ ��(�������� ���������������?�

S����!������������������� ����������&�����,�������2����������%��#����)������ ���������� �

������������������ ����,��������� ��-����� ���� ����� ���� �����,����!������� ����?�

Page 22: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ���������� ��� ������������

� � �����

Page 23: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ��������������������

� � �����

4 Persistencia

4.1 Introducción a Hibernate

<���� �����������������������,�� ���,����������� ����������������� ������ ����������

�����*�5��������������%����-������ �!������������������ ����Project?�

package com.mwm.model;

public class Project {

private Long id;

private String name;

private Company company;

public Long getId() {

return id;

}

public void setId(Long id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Company getCompany() {

return company;

}

public void setCompany(Company company) {

this.company = company;

}

}

���� ������������������������?�� �������#�������!����������Company� ����������!����������

����������,���)�������������*�

� �������!������#�����������������Long#��������)������������� ,�������(���� ���������������

���������� ��� �%�������������� ���������������*������ ���������� �����������������������

��������%�������������>������������������� ,�����$�%�C���� � ��� ������������!������

��� ������������ �������������������!�����:��������������;����������������,���8���������

�������(�������Constructor.newInstance()*�

����������������������������������� �����������������#�)�����,�����������������

������!������������������������� ��������������������#�� ������������!������� � ������

������������$C� � �������� ������*�1��� ��,���������������������� ������������ �

!����������������� ��� �����!����������� ,���������������#��� ������ ��������������

���ProjectTest*�

<������������� ���#�Company#� ����!������������?�

package com.mwm.model;

public class Company {

private Long id;

Page 24: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ��������������������

� � �����

private String name;

public Long getId() {

return id;

}

public void setId() {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

NG����)������������,���8������������������������� ����P�@�!��������:����� ��������

����� �������)�����������;����!��)����H/<������ ����������������!��������� ��

��!������-�����������#� ���� ������� ��������!�����Session� �Transaction����8��������*�

� �!��)����H/<�:H/<�����������������;���!�������,���������� ������������������ ��

� ����Project����������������������� ��������� ����� ��project���� ���������������*�

1����Project�����������Project.hbm.xml����� �classpath?�

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 2.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class

name="com.mwm.model.Project"

table="project">

<id

name="id"

column="id">

<generator class="native"/>

</id>

<property

name="name"

column="name"/>

<many-to-one

name="company"

cascade="all"

column="company_id"/>

</class>

</hibernate-mapping>

I������Company�����������Company.hbm.xml?�

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 2.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class

name="com.mwm.model.Company"

Page 25: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ��������������������

� � �����

table="company">

<id

name="id"

column="id">

<generator class="native"/>

</id>

<property

name="name"

column="name"/>

</class>

</hibernate-mapping>

+���������,����2����������������classpath����!��)����hibernate.properties����� ���

������������������2�-���� ����������������:����������������� ��;#�������������������

��� ����� ���������� �?�

import net.sf.hibernate.Session;

import net.sf.hibernate.SessionFactory;

import net.sf.hibernate.Transaction;

import net.sf.hibernate.cfg.Configuration;

...

Configuration cfg = new Configuration();

cfg.addResource("Project.hbm.xml");

cfg.addResource("Company.hbm.xml");

SessionFactory sf = cfg.buildSessionFactory();

Session s = sf.openSession();

Transaction tx = s.beginTransaction();

Company company = new Company();

company.setName("Mi empresa");

Project project = new Project();

project.setName("MwM");

project.setCompany(company);

s.save(project);

tx.commit();

s.close();

���� ����� �8������������������ �������������������������5O<?�

insert into company (id, name)

values (1, “Mi empresa”);

insert into project (id, name, id_company)

values (1, “MwM”, 1);

G�������������%��#����� �����������������Project�8���������������������&��#�

�������������� ������#� �����(�����������������������Company*�

4.2 Xdoclet <doing…>

4������������������� �������������� �������������� ���#����� ������������������ ���

����������������8��������*�1����� ������������������������������������ ���������H@�� ���

Page 26: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ��������������������

� � ����

:)���?>>2��� ��*������!����*���>2��� ��>;#�����)����������������������-������-�����,���

�������� ������������-�����������������������*�

G���� �#���������������������������:���������;����!��������T������� ���!�������(�%�#�

����� ������������������ ��� ����������-������������� #������(��� �?�

/**

* <p>

* <a href="Project.java.html"> <i>View Source </i> </a>

* </p>

*

* @author Nicolas Cornaglia

* @version $Revision:$ $Date:$

*

* @hibernate.class table="project"

*/

public class Project {

����������� ����������@hibernate.class���Project.java#�(�������������� ����!�����-�����

��� ���#� ������������H@�� ���,������� ������������ ����������-����� ����������������

: ��%�������������� ���������;#��������� ������������������������ ������������

Project.hbm.xml?�

<class

name="com.mwm.model.Project"

table="project"

dynamic-update="false"

dynamic-insert="false"

>

<���T��������������!�����������������,��������������!������#�����������(��� ��� �

���������� ����� ����� ����������������:table=”project”;������� �T����

@hibernate.class. E����&��%����������H@�� ����������%� �����������!������� ���

������������ �����������*�

� �������������������������!���� ����H@�� ������ ������������id����� ��������������

@hibernate.id column="id"���getIteration(…)#�,��� ��������� ���������������?�

generator-class="native"� �,��� ���%� �����null��� �id�������!����� ���� ��������

�����������?�unsaved-value="null"*�

/**

* @param id

* @return

*

* @hibernate.id column="id" generator-class="native" unsaved-value="null"

*/

public Iteration getIteration(Long id) {

return (Iteration) iterations.get(id);

}

1����������������������)������ ������������������������ ����������-��������#������(��� ��

������� ������������������#������ ����������%� ��������������!����� ���������������������#�

����������)����%������� ����� ����?�

<id

name="id"

column="id"

type="java.lang.Long"

unsaved-value="null"

Page 27: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ��������������������

� � �����

>

<generator class="native">

</generator>

</id>

+���������� �T����@hibernate.property�� ����������� ������������name#����� ����� ��,���

)������������description#�startDate� �endDate?�

/**

* @return Returns the name.

*

* @hibernate.property not-null="true"

*/

public String getName() {

return name;

}

����������� ������������������� �����������?�

<property

name="name"

type="java.lang.String"

update="true"

insert="true"

access="property"

column="name"

not-null="true"

/>

,���)����,��� ������������name������������� �������������������� ���� �����name���� ��

��� �#��� ��%���,�������������������%� ��������null*�

1���� �������������������� ���� ����#�����������,������ ��������@hibernate.many-to-

one#� ��,���������������������������%��������� ������� �������������?�

/**

* @return Returns the company.

*

* @hibernate.many-to-one column="company_id" not-null="true"

* class="com.mwm.model.Company"

* outer-join="true"

*/

public Company getCompany() {

return company;

}

������������?�

<many-to-one

name="company"

class="com.mwm.model.Company"

cascade="none"

outer-join="true"

update="true"

insert="true"

access="property"

column="company_id"

not-null="true"

/>

Page 28: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ��������������������

� � �����

I���������' ����� ��������� �����������,������� �%������������ �����������������

��������� ��������������� ������������iterations?�

/**

* @return Returns the iterations.

*

* @hibernate.map name="iterations" inverse="true" lazy="false"

* cascade="all-delete-orphan"

* @hibernate.collection-one-to-many class="com.mwm.model.Iteration"

* @hibernate.collection-key column="project_id"

* @hibernate.collection-index column="id" type="long"

*/

public Map getIterations() {

return iterations;

}

@����� ����������,���������������������� ����-���������������������@hibernate.map#�,���

�������� ���� ����-����������������������� ���� ����� ��������&��������������������� ��

��������������lazy="false"#��,��������� �������������������������������� ���� ���������

cascade="all-delete-orphan"#�,��������������%��������!�������%����#������� ��

�������-��� ���� ���������inverse="true"#�,��� ���� ���-��������������)�������

@hibernate.collection-one-to-many#�,��� ��� �%���� �Map������������������� ���

���������������� ������� ���������-������@hibernate.collection-index column="id"

type="long"�� �,��� ��� �%�������������,������������������������������ ����#����������� ��

��� ������������������� ������������������� �id������� ���������@hibernate.collection-

key column="project_id"*�

<map

name="iterations"

lazy="false"

sort="unsorted"

outer-join="auto"

inverse="true"

cascade="all-delete-orphan"

>

<key

column="project_id"

>

</key>

<index

column="id"

type="long"

/>

<one-to-many

class="com.mwm.model.Iteration"

/>

</map>

5� ������,�����)����� ������������ ���T�������� ���� ��������������?�Company���

Iteration*�

Page 29: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ��������������������

� � �����

4.3 Ant <todo>

4�� �����-�����+�������� ����������-����� ���������������*�

4.4 MySQL <todo>

@���������/ 5O<�*2����)���?>>"""*� �, *���#�������� ������"��7U����U� �, �

4.5 Test Cases <todo>

4.5.1 DBUnit <todo>

Page 30: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ����������� � � �� ������������

� � ����

5 Uniendo todo con Spring

5.1 Inyección de dependencia

F� ����-����������������#������&��������������������������������&�������%����-�����

������ #���������������:����-�;����������-�������(����#����������,�����������������,��������

� ��������������-��������(�������������*�

5��������������� �����,������������������� ������#�)� �%������!�������������� ���������

�������������������*�<��!������������� �����)�����,��� ��� ����+�,��������������C#�

����������C*�1������������ �����)�� ���� �������������*�

8� ���������������� ����� ���������!�����������������$���� ������.���%���� ������0#�

,�������������������� ����,��� ��� ����+������� ��� ����C����������%���� ���������������

��� �����-�*������!�%������������� ��������������!������,���� ��&�������������#������

������������2��������*�

F� ����-��������������������������#�)����,��� ��� ����,������������� ������ ������#����

�����,������� �'�� ����#������,��� ���������������� ����-�#���������������������������

�����������:������;*��

1�����������!������������������������ ���� ����-��������������������������&������

��� ���������������������� �������� �������������� ����� ��������-����������(���� � ��

����������-������������(������ ���� ��������*�

5����������!���� ��� ���� ������� �� ����������� ����������:�2�����S������S��V �

$���V �@�%� �������"��)�����$CV#�9���$�)����� �$�������8�� ��#����#�=� � �

1�� ��)���#�F��*;#������������ ������ �������������,������������������ �����!��������

�����������%����-����������� ?�

G������������W����X�

5.2 Spring <todo>

5.3 El Contexto <todo>

Inversión de Cont rolEl framework es responsable del ciclo

de vida del objeto

Búsque da de Depe nde nc ias

El objeto implementa una API específica del contenedor

Inyecció n de Depe nde nc ias

No hay dependencias a una API específica del contenedor

Inyecció n po r me dio de Const r uc to res

Los objetos son asignados mediante argumentos de constructores

Inyecció n po r me dio de Muta do res

Los objetos son asignados mediante propiedades JavaBean

Page 31: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� � ���� !���������

� � ����

6 Coverage

6.1.1 Emma <todo>

Page 32: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� �"���� ��# �����������

� � �����

7 Documentación

7.1.1 Javadoc <todo>

7.1.2 java2html <todo>

Page 33: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� �$���%��&�����

� � �����

8 ACEGI

1���,���)��������� �������������P�

Page 34: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� �'����(�����

� � �����

9 CVS

9.1 WinCVS <todo>

Page 35: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ��)���� ���� ���������� ��* � +����

� � �����

10 Continuous Integration <todo>

Page 36: Desde Cero - con Spring e Hibernate - draft 0.1 - Español

����� ����������� ��� ���������,- �./����

� � �����

11 El proyecto 2° parte (Web)

11.1 Tomcat <todo>

@���������E������D*2����http://jakarta.apache.org/tomcat#�������� ������work\apps\tomcat

11.2 El proyecto <todo>

• /MG�

• 5�����#�5�����#�=��"��7�

11.3 Spring MVC <todo>

11.4 Sitemesh <todo>

11.5 Test Cases <todo>

11.5.1 jWebUnit <todo>