Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Desde Cero - Con Spring e Hibernate - Draft 0.1 - Español
Desde Cero - Con Spring e Hibernate - Draft 0.1 - Español
Nicols Cornaglia
http://nikofactory.blogspot.com
2004
Contenidos
1
Introduccin
El proyecto 1 parte
3.1
Anlisis
3.2
Creacin del proyecto en eclipse
3.3
Un aproximacin a la arquitectura
3.4
Test driven development
3.4.1
Anlisis
3.4.2
JUnit
3.4.3
El primer caso de prueba
3.5
Cosmtica
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
Persistencia
4.1
Introduccin a Hibernate
4.2
Xdoclet <doing>
4.3
Ant <todo>
4.4
MySQL <todo>
4.5
Test Cases <todo>
4.5.1
DBUnit <todo>
Coverage
6.1.1
Emma <todo>
Documentacin
7.1.1
Javadoc <todo>
7.1.2
java2html <todo>
ACEGI
CVS
9.1
WinCVS <todo>
1 Introduccin
!"
$ % #%
&
#%
#
!
+
./ "/ 0#
1
&
#
#
,
#
*
)
!"
#,
5
#
,
!
'
#
!
*
2
,
) !
6
" 7
-
#
.
&
"
0#
"
+
#
/ 9
!
%
:http://raibledesigns.com/page/rd;#
(
% + 6
:http://raibledesigns.com/wiki/Wiki.jsp?page=AppFuse; 5
<%
:http://www.springlive.com;*
!" *
# 8
*
-#
,
)#
"
#
<
,
2>4 2*
2.1 Directorios
<
!
%
!
, )
:" 7
,
*
work\
-#
work\
(
#
: 2
C:\work\*
:
;
;#
:;
:;
;*
) ) )
"
5
&
# ,
& ,
(
- . !!
!
*
(
* 2# (
% -
,
4
(
5@A
work\jdks#
5@A* 5 )
!
%
%
%JAVA_HOME%\bin
*8
% #
work\jdks\j2sdk1.4.2_05*
!
&
5@A
.$ 5
work\downloads* B
%
% 0# ,
#
,
%
E
;
:http://java.sun.com/;#
!
C
# , )
- * *
/
$%
!
D
,
*
JAVA_HOME=work\jdks\j2sdk1.4.2_05
PATH*
:http://www.eclipse.org;# , F
:http://www.netbeans.org/;
$F@ +
<
" #
work\apps\eclipse#
.-data
eclipse.exe#
work\workspace0#
*
5 (
#%
, %
? Resources*
3 El proyecto 1 parte
4
F
"
: 2
!
!
5
<
<
%
,
*
-
!
H1
!"
1
;*
:http://www.extremeprogramming.org;#
!
4 5
:F
1
;
,
! :9
1
;*
,
%
*
!
#
,
4 5
,
%
!
*
'
4 5
:)
,
.
0;#
#
!
*
.%
0#&
%
*
4 5
#
#
*I
'
#
# %
*
(
#
)
) ) D
D
#
%
DJ K JDL D* 1
,
-#
D*
3.1 Anlisis
E %
H1#
!
?
5
5
5
5
% #
H1
G
! )
G
!*
G
G
*G
*
!
- :
-*5
*5
* G
;* 5 ,
-
! " *
# ! )
!
)
#
#
#
! ) *
,
#
-#
*
5 D+
5
5
#
-
+
5
-*4
- )
*
*4
#
,
*
#
*
% # !
)
!
!
# ,
*
1
)
H1#
!
*@
-*1
#
)
#
,
,
*I
,
,
%
Java#
#,
!
'
*
!
Resources
? File/New/Project/Java
!
mwm?
src
Project*
bin?
, !
-#
Java?
" :5 ;#,
,
# * :5 #5 #5 # *;# ( ,
?
:5 ;
:5 ;
) #
:5 ;
- :5 ;
:5 ;*
,
G
1
F
4
(%
,
*
! " *
*
%
!"
? E@@ E
2
%
!
!
:http://www.testdriven.com;*
!
#E@@
:
;#
:
;
:
*
!
-
!
?
3.4.1 Anlisis
NO
O
:5 ;P
( ,
(
*I,
0 .! )
!
- "
!
)
5
3 0#.! )
)
!
#,
!
#)
- 0*
#
Project#
*
1
#)
,
)
, !
*1
#
$4 *
3.4.2 JUnit
$4
:) ?>>"""*(
- #,
%
(
(
/ )
;
( ,
$%
!
! 2- # 2
*/
&
3
F@
#
test*
'
#2
*
%
$4 #
$% *
TestCase#
,
)
&
#
.+
0
:assertNull()#AssertEquals()* E
&
.5
0#
(
!
(
'*
#
*
%
,
#
!
,
'File/New/Source Folder?
test
&
test
src*
'File/New/JUnit
,
build path
<
Yes#
botn
Java Build
(
?
Test Case
*(
&
Source Folder
ProjectTest*
com.mwm.model
&
setUp()
test#
& )
tearDown()#
<
Finish#
7
,
*
%
com\mwm\model\ProjectTest.java
/*
* Created on Oct 14, 2004
*
Java
Project#
< ,
(
.
<
)
Project#
&
0
&
#
!
&
-
*
&
*E
&
setUp()#
&
)
&
% )
) )
(
&
&
!
testNewProject()#
!
Project*
tearDown()
project = null;
;?
}
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
#
1
%
(
! ) #
#
$4 #
&
#
assertEquals(String arg0,
(
&
*
0! #
&
,
* +)
-*
*
!
5
1
setUp()#
#
!
.
%
,
0 )
#
*1
&
Project#
5 *
-
: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;
)
com.mwm.model
!
5
# , )
7
%
,
src;
*
,
%
$
:
!
)#
#
%
#
% ,
;?
ProjectTest
'Run/Junit
) !
Test*
5
:
,
,
;
>
# )
>
%
&
,
!
&
.
E@@
0!
*5
$4
3.5 Cosmtica
+
% )
#
!
,
3.5.1 Fonts
1
)
#
%
# !
3.5.2 Tabs
<
%
!
-
# ,
!
% #
#
*
3.5.3 JavaDocs
< $% @
%
!
,
#
&
*< , )
1
2
:Types;#
-
(% #
.html#
(%
@author#:
*E
? <a href="mailto:nombre@dominio.com">Nombre</a>;#
GM5 :
;*
&
-
!
?
-#
#
4
'
&
/**
* ${see_to_overridden}
*/
#
(%
2 #
*
:Overriding
*
&
:
methods;
)%
?
:Code/New
Java Files;
${package_declaration}
${typecomment}
${type_declaration}
'
?
log.error("Error: " + ${exception_var}.getMessage());
Log4J
1 '
!
ProjectTest :Alt+Shift+J
Project
&
;#
:Alt+Shift+F
;*
&
-
!)
;*
.A
F5
:AF55;,
%
'
;
0:/
)
1 (
);
#
,
!
!
*1
#
,
)
&
,
Project*
&
#
,
)
?
* F
,
I+QBF
%
F0:B
* +
* 9%
)
H1* I+QBF
;
, ,
#
%
&
I+QBF
! ,
Project#
&
*
)
%
%
- #
#
.I
RQ
&
! -
B
%
-#
&
company
-*
3
*I
,
)
Project?
description
#
)
1
%
,
*
'
Company#,
package com.mwm.model;
#
)
%
%
/**
* <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;
}
}
5
*
!*
#
#
&
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));
}
# ,
Project
removeIteration()*
,
3 #
&
& )
%
Iteration#
addIteration()#getIteration()
!
*
-#
, )
Iteration?
&
Project
;#
&
, )
% %
getIteration(id)
, $4
null#)
D ? junit.framework.AssertionFailedError:
Iteration expected:
) !
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());
}
&
,
% # )
, !
4 Persistencia
4.1 Introduccin 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;
}
}
?
, )
!
#
%
%
#
Long#
)
*
,
>
!
$% C
!
, 8
Constructor.newInstance()*
#)
!
,
#
$C
*1
,
,
#Company#
package com.mwm.model;
public class Company {
private Long id;
!
#
ProjectTest*
<
Company
NG
, 8
;
!
#
! ) H/ <:H/ <
)
P@ !
!)
H/ <
!
; !
Project
Project
:
!
Session
Transaction
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>
Company
,
project
Project.hbm.xml
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"
table="company">
<id
name="id"
column="id">
<generator class="native"/>
</id>
<property
name="name"
column="name"/>
</class>
</hibernate-mapping>
2
2?
import
import
import
import
!)
classpath
hibernate.properties
;#
net.sf.hibernate.Session;
net.sf.hibernate.SessionFactory;
net.sf.hibernate.Transaction;
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();
5O<?
% #
Project
& #
Company*
#
8
*1
H@
:) ?>>2
G
!
-
>2
>;#
)
*
!
#
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
#
:
H@
Project.java#(
,
;#
Project.hbm.xml?
<class
name="com.mwm.model.Project"
table="project"
dynamic-update="false"
dynamic-insert="false"
>
< T
@hibernate.class.
,
!
#
:table=project;
T
H@
%
& %
(
!
*
!
H@
,
generator-class="native"
id
getIteration()#,
@hibernate.id column="id"
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);
}
<id
name="id"
column="id"
type="java.lang.Long"
unsaved-value="null"
%
?
(
#
>
<generator class="native">
</generator>
</id>
+
)
name#
@hibernate.property
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"
/>
, ) ,
# %
1
one# ,
name
name
null*
#
%
@hibernate.many-to-
/**
* @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"
/>
'
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#,
type="long"
!
-
#
)
Map
@hibernate.collection-index column="id"
#
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>
Iteration*
? Company
/ 5O< *2
) ?>>"""*
,*
" 7U
#
&
-;
(
:
-
)
)
8
,
!
,
-*
!%
2
#
#) %
,
*
$
C
!
,
!
+,
C#
. %
0#
%
&
#)
'
,
-#
;*
!
&
(
5
$
1
:
1
,
*< !
C* 1
&
*
!
V @%
) #F *;#
% -
*
: 2
" )
$CV #9
$)
S
8
S V
#=
!
?
Inve rsi n de Co n t ro l
El framework es responsable del ciclo
de vida del objeto
Bs que da de
De pe n de nc ias
Inyecci n de
De pe n de nc ias
Inyecci n po r me di o
de M u t a do res
Inyecci n po r me di o
de Co ns t r uc t o res
6 Coverage
6.1.1 Emma <todo>
"
7 Documentacin
7.1.1 Javadoc <todo>
7.1.2 java2html <todo>
$ %
&
8 ACEGI
1 , )
'
9 CVS
9.1 WinCVS <todo>
,- ./
/ MG
5
#5
#=
" 7
jWebUnit <todo>