본문 바로가기

카테고리 없음

2.5. 스프링루 시작하기 - Step 2: Creating Entities and Fields

2. 스프링 루 시작하기 : 튜토리얼
2.1. 뭘 배울수있나? 
2.2. 또다른 튜토리얼: 웨딩 초대장 어플리케이션
2.3. 튜토리얼 어플리케이션 상세설명
2.4. 1단계: 일반적인 프로젝 시작하기
2.5. 2단계: 엔티티와 필드 생성하기
2.6. 3단계: 통합테스트
2.7. 4단계: IDE 사용하기
2.8. 5단계: 웹티어 만들기
2.9. 6단계: 웹서버 로딩하기
2.10. 어플리케이션에 보안적용하기
2.11. 웹UI의 Look & Feel 커스터마이징하기
2.12. 셀레니움 테스트 
2.13. 백업과 디플로이하기
2.14. 이제 뭐하지


2.5. 2 단계 : 엔티티와 필드 생성하기

이제 우리의 클래스 다이어그램에 그려본 도메인 객체와 필드들을 만들어야할때야. 첫째로 우리는 entity jpa 명령어를 써서 실제 도메인 객체를 만들어야. entity jpa 명령어는 여러 optional attributes 를 가질수있고 최소 하나의 --class 속성을 가져야만해.

필수인 --class 속성 뿐만 아니라 우리는  --testAutomatically속성으로 도메인 객체의 편리한 통합테스트를 만들었어.

이제 Topping 도메인 객체를 시작해보자:

com.springsource.roo.pizzashop roo> hint
You can create entities either via Roo or your IDE.
Using the Roo shell is fast and easy, especially thanks to the TAB completion.

Start by typing 'ent' and then hitting TAB twice.
Enter the --name in the form '~.domain.MyEntityClassName'
In Roo, '~' means the --topLevelPackage you specified via 'create project'.

After specify a --name argument, press SPACE then TAB. Note nothing appears.
Because nothing appears, it means you've entered all mandatory arguments.
However, optional arguments do exist for this command (and most others in Roo).
To see the optional arguments, type '--' and then hit TAB. Mostly you won't
need any optional arguments, but let's select the --testAutomatically option
and hit ENTER. You can always use this approach to view optional arguments.

After creating an entity, use 'hint' for the next suggestion.
com.springsource.roo.pizzashop roo>
com.springsource.roo.pizzashop roo> 
entity jpa --class ~.domain.Topping --testAutomatically Created SRC_MAIN_JAVA/com/springsource/roo/pizzashop/domain Created SRC_MAIN_JAVA/com/springsource/roo/pizzashop/domain/Topping.java Created SRC_TEST_JAVA/com/springsource/roo/pizzashop/domain Created SRC_TEST_JAVA/com/springsource/roo/pizzashop/domain/
ToppingDataOnDemand.java Created SRC_TEST_JAVA/com/springsource/roo/pizzashop/domain/
ToppingIntegrationTest.java Created SRC_MAIN_JAVA/com/springsource/roo/pizzashop/domain/
Topping_Roo_Configurable.aj Created SRC_MAIN_JAVA/com/springsource/roo/pizzashop/domain/
Topping_Roo_JpaEntity.aj Created SRC_MAIN_JAVA/com/springsource/roo/pizzashop/domain/
Topping_Roo_JpaActiveRecord.aj Created SRC_MAIN_JAVA/com/springsource/roo/pizzashop/domain/
Topping_Roo_ToString.aj Created SRC_TEST_JAVA/com/springsource/roo/pizzashop/domain/
ToppingIntegrationTest_Roo_Configurable.aj Created SRC_TEST_JAVA/com/springsource/roo/pizzashop/domain/
ToppingDataOnDemand_Roo_DataOnDemand.aj Created SRC_TEST_JAVA/com/springsource/roo/pizzashop/domain/
ToppingIntegrationTest_Roo_IntegrationTest.aj Created SRC_TEST_JAVA/com/springsource/roo/pizzashop/domain/
ToppingDataOnDemand_Roo_Configurable.aj

자바와 AspectJ 소스들 생성외에도 루쉘의 entity jpa 명령어가 너가 일찌기 정의한 최상위 패키지의 프로젝내에 적절한 폴더구조도 만들어준다는걸 알아차렸을거야. 우리가 '~' 문자를 써서 프로젝의 최상위 패키지를 대신하는것도 알수있겠지. 이렇게 긴 명령어를 줄이기 위한 편리함들 제공해주지만, 너는 루쉘로 최상위 패키지 전체를 탭 자동완성으로 할수있어.

다음단계로 우리는 Topping 도메인 클래스를 위해 '이름' 필드를 추가해야겠지. 이거는 다음의 field 명령어를 통해 얻을수있어:

~.domain.Topping roo> hint
You can add fields to your entities using either Roo or your IDE.

To add a new field, type 'field' and then hit TAB. Be sure to select
your entity and provide a legal Java field name. Use TAB to find an entity
name, and '~' to refer to the top level package. Also remember to use TAB
to access each mandatory argument for the command.

After completing the mandatory arguments, press SPACE, type '--' and then 
TAB. The optional arguments shown reflect official JSR 303 Validation constraints. Feel free to use an optional argument, or delete '--' and hit ENTER. If creating multiple fields, use the UP arrow to access command history. After adding your fields, type 'hint' for the next suggestion. To learn about setting up many-to-one fields, type 'hint relationships'. ~.domain.Topping roo> ~.domain.Topping roo> field string --fieldName name --notNull --sizeMin 2 Managed SRC_MAIN_JAVA/com/springsource/roo/pizzashop/domain/Topping.java Created SRC_MAIN_JAVA/com/springsource/roo/pizzashop/domain/
Topping_Roo_JavaBean.aj Managed SRC_TEST_JAVA/com/springsource/roo/pizzashop/domain/
ToppingDataOnDemand_Roo_DataOnDemand.aj Managed SRC_MAIN_JAVA/com/springsource/roo/pizzashop/domain/
Topping_Roo_ToString.aj

As explained in the documentation by typing the hint 명령어를 친 문서에서 설명한대로, --notNull이나 --sizeMin 2와 같은 optional attributes를 써서 쉽게 fields에 constraints을 추가했어. 이 속성들은 루가 너의 자바 소스에 field 정의를 추가할수있는 표준 JSR-303 애노테이션의 결과물이야.

또한 너는 루쉘이 너가 field 명령어를 사용하고 있는 동안 현재 context를 알고있다는걸 알수있을거야. 루쉘은 너가 Topping 엔티티를 막 생성했고 그러므로 field 명령어가 Topping 자바 소스에 적용되어야한다는걸 알고있어. 루의 현재 Context는 쉘안에서 실행하는해 (설명은 빨간 사각형안에 있어):

만일 너가 다른 타켓의 타입에 필드를 추가 하고싶다면, --class 속성을 프로젝의 어떤 타입으로 완성하려고 탭을 허용한 field 명령어의 일부로서 명시할수있어

다음 단계로 너는 Base 와 Pizza 도메인 객체를 만들거야 다음의 명령어와 비슷한 방식이 될거야 (쉘 출력물은 생각했어):

entity jpa --class ~.domain.Base --testAutomatically 
field string --fieldName name --notNull --sizeMin 2
entity jpa --class ~.domain.Pizza --testAutomatically 
field string --fieldName name --notNull --sizeMin 2
field number --fieldName price --type java.lang.Float

피자 도메인 클래스에 이름과 가격 필드를 추가한 후 우리는 Base와 Topping에 이들의 관계를 엮을 필요가 있어. (하나의 피자는 여려개의 Topping을질수있고, 하나의 Topping은 여러개의 피자에 쓰일수있으니) 피자와 토핑사이에 m:m relationship을 가지고 시작해보자.

이거한 다수:다수관계 (many-to-many relationship)을 만들수있게 루는 field set 명령어를 제공해줘:

~.domain.Pizza roo> field set --fieldName toppings --type ~.domain.Topping

만일 우리의 피자 도메인 엔티티의 이 맵핑을 만들기위해 필요한 정확한 JPA애노테이션에 대한 지식이 없다 하더라도 이 관계를 정의하기 쉽지.

비슷한 방법으로 Pizza와 Base 도메인 엔티티간의 m:1 관계를 정의할수있어 field reference 명령어를 써서 말이지:

~.domain.Pizza roo> field reference --fieldName base --type ~.domain.Base

비슷하게 우리는 계속 피자 주문 PizzaOrder 도메인 객체를 만들고 field date 와 field number 명령어를 조율해서 필드를 추가할수있어:

entity jpa --class ~.domain.PizzaOrder --testAutomatically 
field string --fieldName name --notNull --sizeMin 2
field string --fieldName address --sizeMax 30
field number --fieldName total --type java.lang.Float 
field date --fieldName deliveryDate --type java.util.Date
field set --fieldName pizzas --type ~.domain.Pizza

이걸로 초기 버전의 도메인 모델의 완성했으니 이 단계를 마칠께.