Agile Java EE Beyond Rails
EJB3ãJSFãJPAã¨ããJava EEã®æ¨æºä»æ§ããã¼ã¹ã«ããã®éçºå¹çãRails以ä¸ã«é«ãããã¨ããæ°ããã¸ã§ã¯ãchuraãç«ã¡ä¸ãã¾ããchuraã®åºæ¬æ§æã¯ï½¤Seasar2.4 + Teeda + KuinaDao + S2Hibernate-JPA + S2Dxo + ãã¼ã«ç¾¤ã¨ããå½¢ã«ãªãã¾ããS2Hibernate-JPAã¯ãæçµçã«ã¯ï½¤Kuinaã§ç½®ãæããã®ã§ï½¤ãã¹ã¦ã®ãããã¯ããSeasarãã¡ã¦ã³ãã¼ã·ã§ã³ããæä¾ããããã¨ã«ãªãã¾ãã
churaã®ã³ã³ã»ããã¯ï½¤
Java EEã®æ¨æºä»æ§ããã¼ã¹ã«ãã¦ï½¤Railsãè¶
ããçç£æ§ãçºæ®ããããã«æ¡å¼µæ©è½ã追å ãã¾ãã
Java EEã®æ¨æºä»æ§ã¯éè¦ãã§ãç´ ã®Java EEã®éçºã¯ããã¡ã¤ã«ãããã±ã¼ã¸ã³ã°åãããã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã®ãªãã¼ãããã³ãã³å¿
è¦ã ã£ãããéãã¦ãããããããRailsãªã¿ã«ããããéçºãããã¨ãã人ã«æ¯é使ã£ã¦æ¬²ããããã¡ãããJava EEã®æ¨æºä»æ§ãªãã¦ã©ãã§ãããããã Agileã«éçºãããã ããªãã ã¨ãã人ãæ¯é使ã£ã¦ãã ããããããã¯ãã¨ã«ããå·¥æ°ãåæ¸ãããã¨ããæ¹ã«ãåãã¦ãã¾ãã
Seasar2ã®ãã¬ã¼ã ã¯ã¼ã¯ç¾¤ãAllInOneã«ãã¦æ¬²ããã¨ããè¦æã«ãããã§ãçãã§ãã¾ãã
EJB3ãJSFãJPAãã©ã®ããã«çµã¿åãããã®ãããã®ã ãããã¨ãæ©ã¾ãã¦ããæ¹ãå¤ãã®ã§ã¯ãªãã§ããããããã®è¾ºã®ã¢ã¼ããã¯ãã£ã¯ãæ¨æ¥ï½¤ä¸æ¨æ¥ã¨blogã«æ¸ãã¾ãããchuraã¯ãType5ã®ã¢ã¼ããã¯ãã£ã«ãã¨ã¥ãã¦ãã¾ããåãªãAllInOneããã±ã¼ã¸ã§ã¯ãªããçµ±ä¸ãããã¢ã¼ããã¯ãã£ã«ãã¨ã¥ãã¦ããã®ã§ãã
churaã®ãã¼ã«ç¾¤ã«ã¯ï½¤ã½ã¼ã¹ã³ã¼ãã®èªåçæãDBã®ç§»è¡(ãªãã¡ã¯ã¿ãªã³ã°)ãå«ã¾ãã¾ããè¦æãããã°ï½¤éæ追å ãããã¨æãã¾ãã
ãµã³ãã«ã¢ããª
churaã®ã¤ã¡ã¼ã¸ãã¤ããã§ãããããã«ï½¤åç´ãªãµã³ãã«ãå
ã«èª¬æãã¾ããã¾ã ãå®è£
ããã¦ããªãã¨ãããããã¾ãã、ã¤ã¡ã¼ã¸ã¨ãããã¨ã§ã
ãã®ãµã³ãã«ã¯ï½¤å¾æ¥å¡ç®¡çã®ã¢ããªã±ã¼ã·ã§ã³ã§ããS2JSF-exampleã«åºã¦ãããã¤ã®ç°¡æçã§ãããã¼ãã«ã¯æ¬¡ã®2ã¤ã§ãã
ãã®ã¹ãã¼ãæ å ±ãå ã«æ¬¡ã®EntityãDaoãEntityLogicã®éåãèªåçæãã¾ãã
CREATE TABLE department (
id NUMERIC(9) NOT NULL PRIMARY KEY,
name VARCHAR(20)
);
CREATE TABLE employee (
id NUMERIC(9) NOT NULL PRIMARY KEY,
name VARCHAR(20),
deparment_id NUMERIC(9),
CONSTRAINT fk_department_id FOREIGN KEY (department_id)
REFERENCES department(id)
);
@Entity
public class Department {
@Id(generate=GeneratorType.AUTO)
private int id;
private String name;
...
}
@Entity
public class Employee {
@Id(generate=GeneratorType.AUTO)
private int id;
private String name;
@ManyToOne
private Department department;
...
}
Daoã®å®è£ ã¯ï½¤AOPã§èªåçæãããã®ã§ï½¤å¿ è¦ããã¾ããã
public interface DepartmentDao {
ListfindAll();
Department find(int id);
void persist(Department department);
void remove(Department department);
boolean contains(Department department);
Employee merge(Department department);
void refresh(Department department);
void readLock(Department department);
void writeLock(Department department);
}
public interface EmployeeDao {
ListfindAll();
Employee find(int id);
void persist(Employee employee);
void remove(Employee employee);
boolean contains(Employee employee);
Employee merge(Employee employee);
void refresh(Employee employee);
void readLock(Employee employee);
void writeLock(Employee employee);
}
EntityLogicã«ã¤ãã¦ã¯ï½¤å®è£ ã¯ã©ã¹ã®éåãä½æããã¾ãã
public interface DepartmentLogic {
ListfindAll();
Department find(int id);
void persist(Department department);
void remove(Department department);
}
public interface EmployeeLogic {
ListfindAll();
Employee find(int id);
void persist(Employee employee);
void remove(Employee employee);
}
public class DepartmentLogicImpl implements DepartmentLogic {
private DepartmentDao dao;
...
ListfindAll() {
return dao.findAll();
}
...
}
public class EmployeeLogicImpl implements EmployeeLogic {
private EmployeeDao dao;
...
ListfindAll() {
return dao.findAll();
}
...
}
ãµã³ãã«ã¢ããª2
ãã®ã¢ããªã±ã¼ã·ã§ã³ã¯ï½¤4ã¤ã®ç»é¢ã§æ§æããã¾ãã
- æ¤ç´¢æ¡ä»¶å ¥åç»é¢
- æ¤ç´¢çµæä¸è¦§ç»é¢
- ç·¨éç»é¢
- 確èªç»é¢
æåã¯ï½¤æ¤ç´¢æ¡ä»¶å ¥åç»é¢ã§ããå¾æ¥å¡åãé¨ç½²ãæå®ãããã¨ãã§ãã¾ããé¨ç½²ã¯ãªã¹ãããé¸æãã¾ããHTML(employeeSearch.html)ã¯æ¬¡ã®ããã«ãªãã¾ããéè¦ã§ãªãã¨ããã¯çç¥ãã¦ã¾ãã
é¢é£ããã³ã¼ããèªåçæãã¾ããããemployeeSearch.htmlãå³ã¯ãªãã¯ãã¦ï½¤ã³ã¼ãèªåçæãé¸ã³ã¾ããåºã¦ãããã¤ã¢ãã°ã§å¹¾ã¤ããªãã·ã§ã³ãæå®ãã¾ãã
<form id="employeeSearchForm">
å¾æ¥å¡å:<input id="name" type="text"/>
é¨ç½² :<select id="departmentId" size="2">
<option>Please select
<option value="1">Accounting
<option value="2">Research
</select>
<input id="doFindEmployee" type="submit" value="find"/>
</form>
- é¢é£ããEntityã«ã¯ãEmployeeãæå®ãã¾ãã
- èªåçæã¿ã¤ãã«ã¯ãæ¤ç´¢æ¡ä»¶å
¥åç»é¢ãæå®ãã¾ãã
- ãã®èªåçæã¿ã¤ãã¯ï½¤ã¦ã¼ã¶ã追å ãããã¨ãã«ã¹ã¿ãã¤ãºãããã¨ãã§ãã¾ãã
èªåçæã®ãã¿ã³ãã¯ãªãã¯ãã¦ã½ã¼ã¹ãä½æãã¾ããã§ããã½ã¼ã¹ãè¦ã¦ã¿ã¾ãããã
EmployeeSearchPage.java
HTMLã®ãã¡ã¤ã«åã®æåã大æåã«ã、Pageãsuffixã«ã¤ããã¯ã©ã¹ãèªåçæããã¾ããã½ã¼ã¹ã¯æ¬¡ã®ããã«ãªãã¾ããJava5ã¨1.4ã®ç°å¢ã§ã¯ãåãåºãããã½ã¼ã¹ã³ã¼ããç°ãªãã¾ããä»åã¯ãJava5ã®ä¾ã§ããnameããããã£ã¯ï½¤HTMLã®idå±æ§ããæ¥ã¦ãã¾ãã
public class EmployeeSearchPage {
//@Navigation(to=)
public static final String SUCCESS = "success";private String name;
//@SelectOneMenu(label="*Name")
private int departmentId;
private ListdepartmentIdItems;
private DepartmentLogic departmentLogic;
private EmployeeSearchDxo dxo;...
public String initialize() {
//TODO
departmentIdItems = departmentLogic.findAll();
return null;
}public String doFindEmployee() {
//TODO
return SUCCESS;
}
}
departmentIdããããã£ã¯ï½¤HTMLã®idå±æ§ããæ¥ã¦ãã¾ãããªããåãintã ã¨åããã®ã§ãããããã½ã¼ã¹ã³ã¼ããèªåçæããã¨ãã«ï½¤ãã®ãã¼ã¸ã®ãã¼ã¹Entityã¯Employeeã ã¨æå®ãã¦ãã¾ããEmployeeã«ã¯ãdepartmentããããã£ããã、ãã®åã¯Departementã§ããããã«Departmentã«ã¯idã¨ããããããã£ããã、åã¯intã§ãããããã®æ å ±ã«ãããã¼ã«ãé©åãªåãæ¨æ¸¬ãã¾ããé©åãªåãæ¨æ¸¬ã§ããªãã£ãå ´åã«ã¯ï½¤Stringã¨ã¿ãªãã¾ãã
selectã¿ã°ã§é¸æãããå¤ã¯ï½¤èªåçã«idã«é¢é£ä»ããããããããã£ã«æ ¼ç´ããã¾ããããã§ã¯ãselectã¿ã°ã®optionã¯ã©ã®ããã«ãã¦ç¥ããã¨ãã§ããã®ã§ãããããTeedaã¯ãselectã¿ã°ã®id + "Items"ã®ããããã£ãããã°ï½¤ãã®æ å ±ãããoptionã®æ å ±ãçµã¿ç«ã¦ã¾ããoptionã«è¨å®ããvalueå±æ§ã¨ããã£(HTMLã®ä¾ã ã¨Accountingã ã¨ã)ã¯ã©ã®ããã«ãã¦çµã¿ç«ã¦ãã®ã§ãããããTeedaã¯*Itemsã§æå®ããã¦ããCollectionããè¦ç´ ãåãåºã、ãã®è¦ç´ ã«idå±æ§ãããã°ï½¤ãããvalueå±æ§ã«è¨å®ãã¾ããããã£ã®å¤ã¯ï½¤@SelectOneMenuã®labelè¦ç´ ã§æå®ãããããããã£ã使ãã¾ãã@SelectOneMenu(value="id",label="name")ã®ããã«æ示çã«æå®ãããã¨ãã§ãã¾ããnameã¨æå®ããã¨Department.nameããªã¹ãã«è¡¨ç¤ºããã¾ãã
churaã¯ã*Itemsã®è¦ç´ ãDepartmentã§ãããã¨ãç¥ã£ã¦ããã®ã§ï½¤DepartmentLogicã¸ã®åç §ãèªåçã«ããããã£ã«è¿½å ãã¾ããã¾ãããªã¹ãã«è¡¨ç¤ºããã®ã§ãDepartmentãå ¨ä»¶åå¾ãããã ããã¨äºæ³ã、initialize()ã§departmentLogic.findAll()ãå¼ã³åºãã¾ããchuraã¯ãfindAll()ãå ¨ä»¶åå¾ã®ã¡ã½ããã§ãããã¨ãç¥ã£ã¦ããã®ã§ãã
Teedaã¯ãinitialize()ã¨ããã¡ã½ãããããã°ï½¤ãã¼ã¸ã表示ããç´åã«èªåçã«å¼ã³åºãã¾ãã
èªåçæãããã½ã¼ã¹ã³ã¼ããæåãã®ã¾ã¾å®è¡ãã¦ã¿ã¾ããããHOT deployã«å¯¾å¿ãã¦ã¾ããããã½ã¼ã¹ã³ã¼ããçæããå¾ã«ï½¤ã¢ããªã±ã¼ã·ã§ã³ãµã¼ããåèµ·åããå¿ è¦ã¯ããã¾ãããé¨ç½²ã®ãªã¹ãã«ã¯ï½¤Accountingã¨Researchã¨è¡¨ç¤ºããã¦ãã¾ããDepartmentã®ãã¼ã¿ãåå¾ãã¦ããã¯ãã§ãã、HTMLã«è¨è¿°ããå 容ããã®ã¾ã¾è¡¨ç¤ºããã¦ãã¾ãã
次ã«@SelectOneMenuã®ã³ã¡ã³ããã¯ããã¦ï½¤@SelectOneMenu(label="name")ã«æ¸ãæãã¦employeeSearch.htmlãåå®è¡ãã¦ã¿ã¾ããããDepartmentã®å 容ãå ¨ä»¶è¡¨ç¤ºããã¾ããã@SelectOneMenuã®æå®ãæå¹ã«ãªã£ãã®ã§ãã
ãã¿ã³ãã¯ãªãã¯ãã¦ã¿ã¾ããããã¾ã ãä½ãè¨è¿°ãã¦ããªãã®ã§ï½¤ä½ãèµ·ãã¾ããã
ãã£ã¨æéã ç¶ãã¯ã¾ãä»åº¦ãdoFindEmployee()ã®è¨è¿°ããã§ãã
Railsã®æ¯ã¹ã¦ã
ã¼ãã¯ã²ãããã¯å°æ¬ãã¦ããããä½ãèãã¦ãããããã¨ããããããããæ°ããããã©ãã§ã決å®çã«ããã¦ããRailsãRailsããããã®ã¯Rubyã ããã§ãã»ãã®ãã®ãRailsã¨ããã¹ã¦ã©ããããã¨ãããã¨ã«ã»ã¨ãã©æå³ã¯ãªãã
Railsã®ä»æ§ã¯ã»ã¨ãã©æã¡è¾¼ã¿ã¾ããããRubyã¨Javaã¯éãããRailsãçç£æ§ã§è¶ããã¨ãããã¨ã§ããä»åæ¸ãã¦ããä»æ§ãè¦ã¦ãRailsã¨å ¨ç¶éããã¨ãåããã¯ãããã ãRailsã®è¯ãç¹ã¯å¦ã³ããã¨æãã¾ãã