@k2works/claude-code-booster 3.2.1 → 3.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/lib/assets/.claude/skills/analyzing-business/SKILL.md +2 -2
  2. package/lib/assets/.claude/skills/analyzing-inception-deck/SKILL.md +5 -5
  3. package/lib/assets/.claude/skills/analyzing-requirements/SKILL.md +2 -2
  4. package/lib/assets/.claude/skills/generating-slides/SKILL.md +7 -7
  5. package/lib/assets/docs/article/index.md +4 -1
  6. package/lib/assets/docs/article/practical-database-design/index.md +121 -0
  7. package/lib/assets/docs/article/practical-database-design/part1/chapter01.md +288 -0
  8. package/lib/assets/docs/article/practical-database-design/part1/chapter02.md +518 -0
  9. package/lib/assets/docs/article/practical-database-design/part1/chapter03.md +557 -0
  10. package/lib/assets/docs/article/practical-database-design/part2/chapter04.md +924 -0
  11. package/lib/assets/docs/article/practical-database-design/part2/chapter05.md +1627 -0
  12. package/lib/assets/docs/article/practical-database-design/part2/chapter06.md +2716 -0
  13. package/lib/assets/docs/article/practical-database-design/part2/chapter07.md +2082 -0
  14. package/lib/assets/docs/article/practical-database-design/part2/chapter08.md +2105 -0
  15. package/lib/assets/docs/article/practical-database-design/part2/chapter09.md +2031 -0
  16. package/lib/assets/docs/article/practical-database-design/part2/chapter10.md +1387 -0
  17. package/lib/assets/docs/article/practical-database-design/part2/chapter11.md +1677 -0
  18. package/lib/assets/docs/article/practical-database-design/part2/chapter12.md +1417 -0
  19. package/lib/assets/docs/article/practical-database-design/part2/chapter13.md +1434 -0
  20. package/lib/assets/docs/article/practical-database-design/part3/chapter14.md +667 -0
  21. package/lib/assets/docs/article/practical-database-design/part3/chapter15.md +1625 -0
  22. package/lib/assets/docs/article/practical-database-design/part3/chapter16.md +1915 -0
  23. package/lib/assets/docs/article/practical-database-design/part3/chapter17.md +1708 -0
  24. package/lib/assets/docs/article/practical-database-design/part3/chapter18.md +2095 -0
  25. package/lib/assets/docs/article/practical-database-design/part3/chapter19.md +1123 -0
  26. package/lib/assets/docs/article/practical-database-design/part3/chapter20.md +1031 -0
  27. package/lib/assets/docs/article/practical-database-design/part3/chapter21.md +1382 -0
  28. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter14-orm.md +991 -0
  29. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter15-orm.md +1300 -0
  30. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter16-orm.md +1166 -0
  31. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter17-orm.md +1584 -0
  32. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter18-orm.md +1183 -0
  33. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter19-orm.md +1016 -0
  34. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter20-orm.md +1753 -0
  35. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter21-orm.md +1447 -0
  36. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter22-orm.md +1878 -0
  37. package/lib/assets/docs/article/practical-database-design/part4/chapter22.md +965 -0
  38. package/lib/assets/docs/article/practical-database-design/part4/chapter23.md +2069 -0
  39. package/lib/assets/docs/article/practical-database-design/part4/chapter24.md +2439 -0
  40. package/lib/assets/docs/article/practical-database-design/part4/chapter25.md +3661 -0
  41. package/lib/assets/docs/article/practical-database-design/part4/chapter26.md +2916 -0
  42. package/lib/assets/docs/article/practical-database-design/part4/chapter27.md +3105 -0
  43. package/lib/assets/docs/article/practical-database-design/part4/chapter28.md +2697 -0
  44. package/lib/assets/docs/article/practical-database-design/part4/chapter29.md +2544 -0
  45. package/lib/assets/docs/article/practical-database-design/part4/chapter30.md +2180 -0
  46. package/lib/assets/docs/article/practical-database-design/part4/chapter31.md +1192 -0
  47. package/lib/assets/docs/article/practical-database-design/part4/chapter32.md +2101 -0
  48. package/lib/assets/docs/article/practical-database-design/part5/chapter33.md +1032 -0
  49. package/lib/assets/docs/article/practical-database-design/part5/chapter34.md +1609 -0
  50. package/lib/assets/docs/article/practical-database-design/part5/chapter35.md +1453 -0
  51. package/lib/assets/docs/article/practical-database-design/part5/chapter36.md +1292 -0
  52. package/lib/assets/docs/article/practical-database-design/part5/chapter37.md +1470 -0
  53. package/lib/assets/docs/article/practical-database-design/part5/chapter38.md +1698 -0
  54. package/lib/assets/docs/article/practical-database-design/part5/chapter39.md +2334 -0
  55. package/lib/assets/docs/article/practical-database-design/study/study2-1.md +1693 -0
  56. package/lib/assets/docs/article/practical-database-design/study/study2-2.md +1347 -0
  57. package/lib/assets/docs/article/practical-database-design/study/study2-3.md +2044 -0
  58. package/lib/assets/docs/article/practical-database-design/study/study2-4.md +2229 -0
  59. package/lib/assets/docs/article/practical-database-design/study/study2-5.md +2418 -0
  60. package/lib/assets/docs/article/practical-database-design/study/study3-1.md +2205 -0
  61. package/lib/assets/docs/article/practical-database-design/study/study3-2.md +2221 -0
  62. package/lib/assets/docs/article/practical-database-design/study/study3-3.md +2253 -0
  63. package/lib/assets/docs/article/practical-database-design/study/study3-4.md +2106 -0
  64. package/lib/assets/docs/article/practical-database-design/study/study3-5.md +2507 -0
  65. package/lib/assets/docs/article/practical-database-design/study/study4-1.md +2587 -0
  66. package/lib/assets/docs/article/practical-database-design/study/study4-2.md +2075 -0
  67. package/lib/assets/docs/article/practical-database-design/study/study4-3.md +1805 -0
  68. package/lib/assets/docs/article/practical-database-design/study/study4-4.md +1895 -0
  69. package/lib/assets/docs/article/practical-database-design/study/study4-5.md +2878 -0
  70. package/package.json +1 -1
@@ -0,0 +1,991 @@
1
+ # 第14章:財務会計システムの全体像(ORM版)
2
+
3
+ 本章では、財務会計システムの全体像を把握し、Spring Data JPA を使用した ORM 実装のアーキテクチャについて解説します。MyBatis 版との違いを比較しながら、JPA の特徴を活かした設計手法を学びます。
4
+
5
+ ---
6
+
7
+ ## 14.1 財務会計システムのスコープ
8
+
9
+ ### 基幹業務システムにおける位置づけ
10
+
11
+ 財務会計システムは、基幹業務システムの中で他のシステムから仕訳データを受け取り、企業の財務状態を管理する重要なシステムです。
12
+
13
+ ```plantuml
14
+ @startuml
15
+
16
+ title 基幹業務システムの全体像
17
+
18
+ state "得意先\n(Customer)" as Customer
19
+ state "販売管理\n(Sales Management)" as SalesManagement
20
+ SalesManagement : ・受注 \n・出荷 \n・売上
21
+
22
+ state "購買管理\n(Procurement Management)" as ProcurementManagement
23
+ ProcurementManagement : ・発注\n・入荷/受入\n・仕入(検収)
24
+
25
+ state "在庫\n(Inventory)" as Inventory
26
+
27
+ state "仕入先\n(Supplier)" as Supplier
28
+
29
+ state "生産管理\n(Production Management)" as ProductionManagement
30
+ ProductionManagement : ・生産計画\n・品質管理\n・工程管理\n・製造原価管理
31
+
32
+ state "債権管理\n(Accounts Receivable)" as AccountsReceivable
33
+ AccountsReceivable : ・請求 \n・入金
34
+
35
+ state "債務管理\n(Accounts Payable)" as AccountsPayable
36
+ AccountsPayable : ・支払\n・出金
37
+
38
+ state "財務会計\n(Financial Accounting)" as Accounting #LightBlue
39
+ Accounting : ・勘定科目管理\n・仕訳処理\n・残高管理\n・財務諸表\n・決算処理
40
+
41
+ [*] -> Customer
42
+
43
+ Customer --> SalesManagement
44
+ SalesManagement --> AccountsReceivable
45
+ SalesManagement --> ProcurementManagement : 調達依頼
46
+ SalesManagement --> ProductionManagement : 需要予測 \n製造指示
47
+ SalesManagement --> Accounting : 仕訳
48
+ SalesManagement --> Customer
49
+ Inventory --> SalesManagement
50
+
51
+ ProcurementManagement --> Inventory
52
+ ProcurementManagement --> AccountsPayable
53
+ ProcurementManagement --> Supplier
54
+ Supplier --> ProcurementManagement
55
+ ProductionManagement --> ProcurementManagement : 調達依頼
56
+
57
+ ProductionManagement --> Inventory : 入荷
58
+ Inventory --> ProductionManagement : 払出
59
+
60
+ AccountsReceivable --> Accounting
61
+ AccountsPayable --> Accounting
62
+ ProductionManagement --> Accounting : 仕訳
63
+ ProcurementManagement --> Accounting : 仕訳
64
+
65
+ Customer -> [*]
66
+
67
+ @enduml
68
+ ```
69
+
70
+ ### 財務会計システムの業務領域
71
+
72
+ 財務会計システムは、以下の業務領域をカバーします。
73
+
74
+ ```plantuml
75
+ @startuml
76
+
77
+ [*] -> 販売管理
78
+ 販売管理 --> 財務会計 : 売上仕訳
79
+ 販売管理 : 売掛金 / 売上
80
+
81
+ [*] -> 購買管理
82
+ 購買管理 --> 財務会計 : 仕入仕訳
83
+ 購買管理 : 仕入 / 買掛金
84
+
85
+ [*] -> 債権管理
86
+ 債権管理 --> 財務会計 : 入金仕訳
87
+ 債権管理 : 現金預金 / 売掛金
88
+
89
+ [*] -> 債務管理
90
+ 債務管理 --> 財務会計 : 支払仕訳
91
+ 債務管理 : 買掛金 / 現金預金
92
+
93
+ 財務会計 --> 帳票出力
94
+ 財務会計 : 勘定科目管理 \n仕訳処理 \n残高管理
95
+ 帳票出力 : 日計表 \n合計残高試算表 \n財務諸表
96
+
97
+ 帳票出力 -> [*]
98
+
99
+ @enduml
100
+ ```
101
+
102
+ ### 業務機能の概要
103
+
104
+ | 業務 | 説明 |
105
+ |------|------|
106
+ | **勘定科目管理** | 勘定科目マスタ・勘定科目構成マスタの管理 |
107
+ | **仕訳処理** | 仕訳入力・自動仕訳・仕訳照会 |
108
+ | **残高管理** | 日次残高・月次残高の管理 |
109
+ | **帳票出力** | 日計表・合計残高試算表・財務諸表の出力 |
110
+ | **決算処理** | 月次決算・年次決算 |
111
+
112
+ ### ユースケース図
113
+
114
+ 財務会計システム全体のユースケースは以下の通りです。
115
+
116
+ ```plantuml
117
+ @startuml
118
+ left to right direction
119
+ skinparam packageStyle rectangle
120
+ skinparam linetype ortho
121
+ title 財務会計システム ユースケース図
122
+
123
+ actor 経理担当者 as accounting
124
+ actor 財務担当者 as finance
125
+ actor 営業担当者 as sales
126
+ actor 購買担当者 as buyer
127
+ actor 管理者 as admin
128
+
129
+ rectangle 財務会計システム {
130
+ usecase "勘定科目管理" as UC_ACCOUNT
131
+ usecase "仕訳入力" as UC_JOURNAL
132
+ usecase "自動仕訳" as UC_AUTO_JOURNAL
133
+ usecase "残高照会" as UC_BALANCE
134
+ usecase "日計表出力" as UC_DAILY
135
+ usecase "試算表出力" as UC_TRIAL
136
+ usecase "財務諸表出力" as UC_FINANCIAL
137
+ usecase "決算処理" as UC_CLOSING
138
+ }
139
+
140
+ admin -- UC_ACCOUNT
141
+ accounting -- UC_JOURNAL
142
+ accounting -- UC_BALANCE
143
+ accounting -- UC_DAILY
144
+ accounting -- UC_TRIAL
145
+ accounting -- UC_CLOSING
146
+ finance -- UC_FINANCIAL
147
+ sales -- UC_AUTO_JOURNAL
148
+ buyer -- UC_AUTO_JOURNAL
149
+ @enduml
150
+ ```
151
+
152
+ ---
153
+
154
+ ## 14.2 ORM アーキテクチャの概要
155
+
156
+ ### MyBatis 版と JPA 版の比較
157
+
158
+ 本章では、MyBatis 版と同じビジネスロジックを Spring Data JPA で実装します。ORM の違いによるアプローチの差異を理解することが目的です。
159
+
160
+ | 観点 | MyBatis 版 | JPA 版 |
161
+ |------|-----------|--------|
162
+ | マッピング方法 | XML Mapper + resultMap | @Entity + @Column |
163
+ | ENUM 変換 | TypeHandler | AttributeConverter |
164
+ | Repository | Mapper インターフェース + XML | JpaRepository インターフェース |
165
+ | SQL | XML で明示的に記述 | メソッド名規約で自動生成 |
166
+ | テスト基底クラス | @MybatisTest | @DataJpaTest |
167
+ | N+1 問題対策 | 明示的な JOIN | @EntityGraph, JPQL JOIN FETCH |
168
+
169
+ ### JPA の特徴
170
+
171
+ ```plantuml
172
+ @startuml
173
+ title JPA / Hibernate の特徴
174
+
175
+ package "JPA (Java Persistence API)" {
176
+ rectangle entity [
177
+ <b>Entity</b>
178
+ --
179
+ - @Entity
180
+ - @Table
181
+ - @Column
182
+ - @Id
183
+ ]
184
+
185
+ rectangle relations [
186
+ <b>Relationships</b>
187
+ --
188
+ - @OneToMany
189
+ - @ManyToOne
190
+ - @ManyToMany
191
+ - @Embedded
192
+ ]
193
+
194
+ rectangle query [
195
+ <b>Query</b>
196
+ --
197
+ - JPQL
198
+ - Criteria API
199
+ - Native Query
200
+ - メソッド名規約
201
+ ]
202
+ }
203
+
204
+ package "Spring Data JPA" {
205
+ rectangle repo [
206
+ <b>Repository</b>
207
+ --
208
+ - JpaRepository
209
+ - @Query
210
+ - Specification
211
+ - @EntityGraph
212
+ ]
213
+ }
214
+
215
+ ' 関係線の定義
216
+ entity --> relations
217
+ entity --> query
218
+ query --> repo
219
+
220
+ note right of entity
221
+ オブジェクト指向の
222
+ マッピング定義
223
+ end note
224
+
225
+ note right of repo
226
+ Repository パターンの
227
+ 自動実装
228
+ end note
229
+ @enduml
230
+ ```
231
+
232
+ ---
233
+
234
+ ## 14.3 プロジェクト構成
235
+
236
+ ### ヘキサゴナルアーキテクチャ
237
+
238
+ 財務会計システムは、ヘキサゴナルアーキテクチャ(ポート&アダプターパターン)を採用します。
239
+
240
+ ```
241
+ src/main/java/com/example/accounting/
242
+ ├── application/ # アプリケーション層
243
+ │ └── port/
244
+ │ └── out/ # Output Port(リポジトリインターフェース)
245
+ │ └── AccountRepository.java
246
+ ├── domain/ # ドメイン層(純粋なビジネスロジック)
247
+ │ └── model/ # ドメインモデル
248
+ │ └── account/
249
+ │ ├── Account.java # @Entity アノテーション付き
250
+ │ ├── BsPlType.java
251
+ │ ├── DebitCreditType.java
252
+ │ ├── TransactionElementType.java
253
+ │ ├── AggregationType.java
254
+ │ └── TaxCalculationType.java
255
+ ├── infrastructure/ # インフラストラクチャ層
256
+ │ └── persistence/
257
+ │ ├── converter/ # JPA AttributeConverter
258
+ │ │ ├── BsPlTypeConverter.java
259
+ │ │ ├── DebitCreditTypeConverter.java
260
+ │ │ ├── TransactionElementTypeConverter.java
261
+ │ │ ├── AggregationTypeConverter.java
262
+ │ │ └── TaxCalculationTypeConverter.java
263
+ │ └── repository/ # Repository実装
264
+ │ ├── AccountJpaRepository.java # Spring Data JPA
265
+ │ └── AccountRepositoryImpl.java
266
+ └── config/
267
+ └── JpaConfig.java
268
+
269
+ src/main/resources/
270
+ ├── db/migration/
271
+ │ └── V001__create_enum_types.sql
272
+ └── application.yml
273
+ ```
274
+
275
+ ### アーキテクチャ図
276
+
277
+ ```plantuml
278
+ @startuml hexagonal_architecture_jpa
279
+ !define RECTANGLE class
280
+
281
+ package "Hexagonal Architecture (財務会計API - JPA版)" {
282
+
283
+ RECTANGLE "Application Core\n(Domain + Use Cases)" as core {
284
+ - Account (勘定科目)
285
+ - Journal (仕訳)
286
+ - JournalEntry (仕訳明細)
287
+ - DailyBalance (日次残高)
288
+ - MonthlyBalance (月次残高)
289
+ - AccountUseCase
290
+ - JournalUseCase
291
+ - BalanceUseCase
292
+ }
293
+
294
+ RECTANGLE "Input Adapters\n(Driving Side)" as input {
295
+ - Spring Controllers
296
+ - REST API Endpoints
297
+ - Request Validation
298
+ - Error Handling
299
+ }
300
+
301
+ RECTANGLE "Output Adapters\n(Driven Side)" as output {
302
+ - Spring Data JPA Repository
303
+ - AttributeConverter
304
+ - Entity Mapping
305
+ }
306
+ }
307
+
308
+ input --> core : "Input Ports\n(Use Cases)"
309
+ core --> output : "Output Ports\n(Repository Interfaces)"
310
+
311
+ note top of core
312
+ 純粋なビジネスロジック
313
+ JPA アノテーションは
314
+ ドメイン層に配置
315
+ end note
316
+
317
+ note left of input
318
+ 外部からアプリケーションを
319
+ 駆動するアダプター
320
+ HTTP, REST等
321
+ end note
322
+
323
+ note right of output
324
+ アプリケーションが外部の
325
+ 技術を使うためのアダプター
326
+ PostgreSQL, Hibernate等
327
+ end note
328
+ @enduml
329
+ ```
330
+
331
+ ---
332
+
333
+ ## 14.4 環境構築
334
+
335
+ ### 前提条件
336
+
337
+ 以下のツールがインストールされていることを確認してください。
338
+
339
+ - Java 17以上(推奨:Java 21 LTS)
340
+ - Gradle 8.x以上(推奨:Gradle Wrapper使用)
341
+ - Docker & Docker Compose(推奨)
342
+ - Git
343
+
344
+ ### build.gradle の設定
345
+
346
+ <details>
347
+ <summary>build.gradle</summary>
348
+
349
+ ```groovy
350
+ plugins {
351
+ id 'java'
352
+ id 'application'
353
+ id 'jacoco'
354
+ id 'org.springframework.boot' version '3.2.0'
355
+ id 'io.spring.dependency-management' version '1.1.4'
356
+ }
357
+
358
+ group = 'com.example.accounting'
359
+ version = '0.0.1-SNAPSHOT'
360
+
361
+ java {
362
+ sourceCompatibility = '21'
363
+ }
364
+
365
+ repositories {
366
+ mavenCentral()
367
+ }
368
+
369
+ dependencies {
370
+ // Spring Boot
371
+ implementation 'org.springframework.boot:spring-boot-starter-web'
372
+ implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
373
+ implementation 'org.springframework.boot:spring-boot-starter-validation'
374
+ testImplementation 'org.springframework.boot:spring-boot-starter-test'
375
+
376
+ // Flyway
377
+ implementation 'org.flywaydb:flyway-core'
378
+ implementation 'org.flywaydb:flyway-database-postgresql'
379
+
380
+ // Database
381
+ runtimeOnly 'org.postgresql:postgresql'
382
+
383
+ // Lombok (オプション)
384
+ compileOnly 'org.projectlombok:lombok'
385
+ annotationProcessor 'org.projectlombok:lombok'
386
+
387
+ // Test
388
+ testImplementation 'org.junit.jupiter:junit-jupiter-api'
389
+ testImplementation 'org.junit.jupiter:junit-jupiter-params'
390
+ testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
391
+ testImplementation 'org.assertj:assertj-core:3.24.2'
392
+ testImplementation 'org.testcontainers:testcontainers:1.19.3'
393
+ testImplementation 'org.testcontainers:postgresql:1.19.3'
394
+ testImplementation 'org.testcontainers:junit-jupiter:1.19.3'
395
+ }
396
+
397
+ test {
398
+ useJUnitPlatform()
399
+ }
400
+
401
+ jacoco {
402
+ toolVersion = "0.8.11"
403
+ }
404
+
405
+ jacocoTestReport {
406
+ dependsOn test
407
+ reports {
408
+ xml.required = true
409
+ html.required = true
410
+ }
411
+ }
412
+ ```
413
+
414
+ </details>
415
+
416
+ ### Docker Compose のセットアップ
417
+
418
+ <details>
419
+ <summary>docker-compose.yml</summary>
420
+
421
+ ```yaml
422
+ services:
423
+ postgres:
424
+ image: postgres:16-alpine
425
+ container_name: financial-accounting-postgres-jpa
426
+ restart: unless-stopped
427
+ environment:
428
+ POSTGRES_USER: ${POSTGRES_USER:-postgres}
429
+ POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
430
+ POSTGRES_DB: ${POSTGRES_DB:-financial_accounting}
431
+ TZ: 'Asia/Tokyo'
432
+ ports:
433
+ - "${POSTGRES_PORT:-5432}:5432"
434
+ volumes:
435
+ - postgres_data:/var/lib/postgresql/data
436
+ healthcheck:
437
+ test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-postgres}"]
438
+ interval: 10s
439
+ timeout: 5s
440
+ retries: 5
441
+
442
+ volumes:
443
+ postgres_data:
444
+ ```
445
+
446
+ </details>
447
+
448
+ ### application.yml の設定
449
+
450
+ <details>
451
+ <summary>application.yml</summary>
452
+
453
+ ```yaml
454
+ spring:
455
+ application:
456
+ name: financial-accounting-jpa
457
+
458
+ datasource:
459
+ url: jdbc:postgresql://localhost:5432/financial_accounting
460
+ username: postgres
461
+ password: postgres
462
+ driver-class-name: org.postgresql.Driver
463
+
464
+ jpa:
465
+ hibernate:
466
+ ddl-auto: validate # Flyway でスキーマ管理するため validate
467
+ show-sql: true
468
+ properties:
469
+ hibernate:
470
+ format_sql: true
471
+ dialect: org.hibernate.dialect.PostgreSQLDialect
472
+
473
+ flyway:
474
+ enabled: true
475
+ locations: classpath:db/migration
476
+
477
+ logging:
478
+ level:
479
+ org.hibernate.SQL: DEBUG
480
+ org.hibernate.type.descriptor.sql.BasicBinder: TRACE
481
+ ```
482
+
483
+ </details>
484
+
485
+ ### PostgreSQL ENUM の定義
486
+
487
+ <details>
488
+ <summary>V001__create_enum_types.sql</summary>
489
+
490
+ ```sql
491
+ -- BSPL区分(貸借対照表・損益計算書)
492
+ CREATE TYPE BSPL区分 AS ENUM ('BS', 'PL');
493
+
494
+ -- 貸借区分
495
+ CREATE TYPE 貸借区分 AS ENUM ('借方', '貸方');
496
+
497
+ -- 取引要素区分
498
+ CREATE TYPE 取引要素区分 AS ENUM ('資産', '負債', '資本', '収益', '費用');
499
+
500
+ -- 集計区分
501
+ CREATE TYPE 集計区分 AS ENUM ('見出科目', '集計科目', '計上科目');
502
+
503
+ -- 消費税計算区分
504
+ CREATE TYPE 消費税計算区分 AS ENUM ('課税', '非課税', '不課税', '免税');
505
+ ```
506
+
507
+ </details>
508
+
509
+ ---
510
+
511
+ ## 14.5 JPA エンティティの基本
512
+
513
+ ### 日本語テーブル名・カラム名のマッピング
514
+
515
+ 本シリーズでは、**日本語テーブル名・日本語カラム名**を使用します。JPA では、`@Table` と `@Column` アノテーションを使ってマッピングします。
516
+
517
+ | 項目 | 例 |
518
+ |-----|-----|
519
+ | テーブル名 | `勘定科目マスタ`, `仕訳データ`, `日次勘定科目残高` |
520
+ | カラム名 | `勘定科目コード`, `勘定科目名`, `BSPL区分` |
521
+ | ENUM名 | `BSPL区分`, `貸借区分`, `集計区分` |
522
+ | ENUM値 | `BS`, `PL`, `借方`, `貸方` |
523
+
524
+ ### エンティティクラスの例
525
+
526
+ <details>
527
+ <summary>Account.java(勘定科目エンティティ)</summary>
528
+
529
+ ```java
530
+ package com.example.accounting.domain.model.account;
531
+
532
+ import jakarta.persistence.*;
533
+ import lombok.*;
534
+ import java.time.LocalDateTime;
535
+
536
+ @Entity
537
+ @Table(name = "勘定科目マスタ")
538
+ @Data
539
+ @Builder
540
+ @NoArgsConstructor
541
+ @AllArgsConstructor
542
+ public class Account {
543
+
544
+ @Id
545
+ @Column(name = "勘定科目コード", length = 5)
546
+ private String accountCode;
547
+
548
+ @Column(name = "勘定科目名", length = 40, nullable = false)
549
+ private String accountName;
550
+
551
+ @Column(name = "勘定科目略名", length = 10)
552
+ private String accountShortName;
553
+
554
+ @Column(name = "勘定科目カナ", length = 40)
555
+ private String accountNameKana;
556
+
557
+ @Convert(converter = BsPlTypeConverter.class)
558
+ @Column(name = "BSPL区分", nullable = false, columnDefinition = "BSPL区分")
559
+ private BsPlType bsPlType;
560
+
561
+ @Convert(converter = DebitCreditTypeConverter.class)
562
+ @Column(name = "貸借区分", nullable = false, columnDefinition = "貸借区分")
563
+ private DebitCreditType debitCreditType;
564
+
565
+ @Convert(converter = TransactionElementTypeConverter.class)
566
+ @Column(name = "取引要素区分", nullable = false, columnDefinition = "取引要素区分")
567
+ private TransactionElementType transactionElementType;
568
+
569
+ @Convert(converter = AggregationTypeConverter.class)
570
+ @Column(name = "集計区分", nullable = false, columnDefinition = "集計区分")
571
+ private AggregationType aggregationType;
572
+
573
+ @Column(name = "管理会計区分", length = 1)
574
+ private String managementAccountingType;
575
+
576
+ @Column(name = "費用区分", length = 1)
577
+ private String expenseType;
578
+
579
+ @Column(name = "元帳出力区分", length = 1)
580
+ private String ledgerOutputType;
581
+
582
+ @Column(name = "補助科目種別", length = 1)
583
+ private String subAccountType;
584
+
585
+ @Convert(converter = TaxCalculationTypeConverter.class)
586
+ @Column(name = "消費税計算区分", columnDefinition = "消費税計算区分")
587
+ private TaxCalculationType taxCalculationType;
588
+
589
+ @Column(name = "課税取引コード", length = 2)
590
+ private String taxTransactionCode;
591
+
592
+ @Column(name = "期日管理区分", length = 1)
593
+ private String dueDateManagementType;
594
+
595
+ @Column(name = "作成日時")
596
+ private LocalDateTime createdAt;
597
+
598
+ @Column(name = "更新日時")
599
+ private LocalDateTime updatedAt;
600
+
601
+ @Column(name = "更新者名", length = 12)
602
+ private String updatedBy;
603
+
604
+ @PrePersist
605
+ protected void onCreate() {
606
+ createdAt = LocalDateTime.now();
607
+ updatedAt = LocalDateTime.now();
608
+ }
609
+
610
+ @PreUpdate
611
+ protected void onUpdate() {
612
+ updatedAt = LocalDateTime.now();
613
+ }
614
+ }
615
+ ```
616
+
617
+ </details>
618
+
619
+ ### ENUM 定義
620
+
621
+ <details>
622
+ <summary>BsPlType.java(BSPL区分)</summary>
623
+
624
+ ```java
625
+ package com.example.accounting.domain.model.account;
626
+
627
+ import lombok.Getter;
628
+ import lombok.RequiredArgsConstructor;
629
+
630
+ @Getter
631
+ @RequiredArgsConstructor
632
+ public enum BsPlType {
633
+ BS("BS"), // 貸借対照表
634
+ PL("PL"); // 損益計算書
635
+
636
+ private final String displayName;
637
+
638
+ public static BsPlType fromDisplayName(String displayName) {
639
+ for (BsPlType type : values()) {
640
+ if (type.displayName.equals(displayName)) {
641
+ return type;
642
+ }
643
+ }
644
+ throw new IllegalArgumentException("不正なBSPL区分: " + displayName);
645
+ }
646
+ }
647
+ ```
648
+
649
+ </details>
650
+
651
+ <details>
652
+ <summary>DebitCreditType.java(貸借区分)</summary>
653
+
654
+ ```java
655
+ package com.example.accounting.domain.model.account;
656
+
657
+ import lombok.Getter;
658
+ import lombok.RequiredArgsConstructor;
659
+
660
+ @Getter
661
+ @RequiredArgsConstructor
662
+ public enum DebitCreditType {
663
+ DEBIT("借方"),
664
+ CREDIT("貸方");
665
+
666
+ private final String displayName;
667
+
668
+ public static DebitCreditType fromDisplayName(String displayName) {
669
+ for (DebitCreditType type : values()) {
670
+ if (type.displayName.equals(displayName)) {
671
+ return type;
672
+ }
673
+ }
674
+ throw new IllegalArgumentException("不正な貸借区分: " + displayName);
675
+ }
676
+ }
677
+ ```
678
+
679
+ </details>
680
+
681
+ ### AttributeConverter
682
+
683
+ JPA では `AttributeConverter` を使って PostgreSQL ENUM と Java Enum を変換します。
684
+
685
+ <details>
686
+ <summary>BsPlTypeConverter.java</summary>
687
+
688
+ ```java
689
+ package com.example.accounting.infrastructure.persistence.converter;
690
+
691
+ import com.example.accounting.domain.model.account.BsPlType;
692
+ import jakarta.persistence.AttributeConverter;
693
+ import jakarta.persistence.Converter;
694
+
695
+ @Converter(autoApply = true)
696
+ public class BsPlTypeConverter implements AttributeConverter<BsPlType, String> {
697
+
698
+ @Override
699
+ public String convertToDatabaseColumn(BsPlType attribute) {
700
+ return attribute != null ? attribute.getDisplayName() : null;
701
+ }
702
+
703
+ @Override
704
+ public BsPlType convertToEntityAttribute(String dbData) {
705
+ return dbData != null ? BsPlType.fromDisplayName(dbData) : null;
706
+ }
707
+ }
708
+ ```
709
+
710
+ </details>
711
+
712
+ ---
713
+
714
+ ## 14.6 Spring Data JPA Repository
715
+
716
+ ### JpaRepository インターフェース
717
+
718
+ Spring Data JPA では、インターフェースを定義するだけで基本的な CRUD 操作が自動実装されます。
719
+
720
+ <details>
721
+ <summary>AccountJpaRepository.java</summary>
722
+
723
+ ```java
724
+ package com.example.accounting.infrastructure.persistence.repository;
725
+
726
+ import com.example.accounting.domain.model.account.Account;
727
+ import com.example.accounting.domain.model.account.AggregationType;
728
+ import com.example.accounting.domain.model.account.BsPlType;
729
+ import org.springframework.data.jpa.repository.JpaRepository;
730
+ import org.springframework.stereotype.Repository;
731
+
732
+ import java.util.List;
733
+
734
+ @Repository
735
+ public interface AccountJpaRepository extends JpaRepository<Account, String> {
736
+
737
+ List<Account> findByBsPlType(BsPlType bsPlType);
738
+
739
+ List<Account> findByAggregationType(AggregationType aggregationType);
740
+
741
+ List<Account> findAllByOrderByAccountCodeAsc();
742
+ }
743
+ ```
744
+
745
+ </details>
746
+
747
+ ### Repository インターフェース(Output Port)
748
+
749
+ ヘキサゴナルアーキテクチャでは、アプリケーション層に Output Port を定義します。
750
+
751
+ <details>
752
+ <summary>AccountRepository.java</summary>
753
+
754
+ ```java
755
+ package com.example.accounting.application.port.out;
756
+
757
+ import com.example.accounting.domain.model.account.Account;
758
+ import com.example.accounting.domain.model.account.AggregationType;
759
+ import com.example.accounting.domain.model.account.BsPlType;
760
+
761
+ import java.util.List;
762
+ import java.util.Optional;
763
+
764
+ /**
765
+ * 勘定科目リポジトリ(Output Port)
766
+ */
767
+ public interface AccountRepository {
768
+
769
+ void save(Account account);
770
+
771
+ Optional<Account> findByCode(String accountCode);
772
+
773
+ List<Account> findAll();
774
+
775
+ List<Account> findByBsPlType(BsPlType bsPlType);
776
+
777
+ List<Account> findByAggregationType(AggregationType aggregationType);
778
+
779
+ void delete(String accountCode);
780
+
781
+ void deleteAll();
782
+ }
783
+ ```
784
+
785
+ </details>
786
+
787
+ ### Repository 実装(Infrastructure 層)
788
+
789
+ <details>
790
+ <summary>AccountRepositoryImpl.java</summary>
791
+
792
+ ```java
793
+ package com.example.accounting.infrastructure.persistence.repository;
794
+
795
+ import com.example.accounting.application.port.out.AccountRepository;
796
+ import com.example.accounting.domain.model.account.Account;
797
+ import com.example.accounting.domain.model.account.AggregationType;
798
+ import com.example.accounting.domain.model.account.BsPlType;
799
+ import lombok.RequiredArgsConstructor;
800
+ import org.springframework.stereotype.Repository;
801
+
802
+ import java.util.List;
803
+ import java.util.Optional;
804
+
805
+ @Repository
806
+ @RequiredArgsConstructor
807
+ public class AccountRepositoryImpl implements AccountRepository {
808
+
809
+ private final AccountJpaRepository jpaRepository;
810
+
811
+ @Override
812
+ public void save(Account account) {
813
+ jpaRepository.save(account);
814
+ }
815
+
816
+ @Override
817
+ public Optional<Account> findByCode(String accountCode) {
818
+ return jpaRepository.findById(accountCode);
819
+ }
820
+
821
+ @Override
822
+ public List<Account> findAll() {
823
+ return jpaRepository.findAllByOrderByAccountCodeAsc();
824
+ }
825
+
826
+ @Override
827
+ public List<Account> findByBsPlType(BsPlType bsPlType) {
828
+ return jpaRepository.findByBsPlType(bsPlType);
829
+ }
830
+
831
+ @Override
832
+ public List<Account> findByAggregationType(AggregationType aggregationType) {
833
+ return jpaRepository.findByAggregationType(aggregationType);
834
+ }
835
+
836
+ @Override
837
+ public void delete(String accountCode) {
838
+ jpaRepository.deleteById(accountCode);
839
+ }
840
+
841
+ @Override
842
+ public void deleteAll() {
843
+ jpaRepository.deleteAll();
844
+ }
845
+ }
846
+ ```
847
+
848
+ </details>
849
+
850
+ ---
851
+
852
+ ## 14.7 TestContainers によるテスト
853
+
854
+ ### テスト基盤クラス
855
+
856
+ <details>
857
+ <summary>BaseIntegrationTest.java</summary>
858
+
859
+ ```java
860
+ package com.example.accounting.testsetup;
861
+
862
+ import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
863
+ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
864
+ import org.springframework.test.context.DynamicPropertyRegistry;
865
+ import org.springframework.test.context.DynamicPropertySource;
866
+ import org.testcontainers.containers.PostgreSQLContainer;
867
+ import org.testcontainers.junit.jupiter.Container;
868
+ import org.testcontainers.junit.jupiter.Testcontainers;
869
+
870
+ @Testcontainers
871
+ @DataJpaTest
872
+ @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
873
+ public abstract class BaseIntegrationTest {
874
+
875
+ @Container
876
+ static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16-alpine")
877
+ .withDatabaseName("testdb")
878
+ .withUsername("testuser")
879
+ .withPassword("testpass");
880
+
881
+ @DynamicPropertySource
882
+ static void configureProperties(DynamicPropertyRegistry registry) {
883
+ registry.add("spring.datasource.url", postgres::getJdbcUrl);
884
+ registry.add("spring.datasource.username", postgres::getUsername);
885
+ registry.add("spring.datasource.password", postgres::getPassword);
886
+ registry.add("spring.flyway.enabled", () -> "true");
887
+ registry.add("spring.flyway.locations", () -> "classpath:db/migration");
888
+ registry.add("spring.jpa.hibernate.ddl-auto", () -> "validate");
889
+ }
890
+ }
891
+ ```
892
+
893
+ </details>
894
+
895
+ ### データベース接続テスト
896
+
897
+ <details>
898
+ <summary>DatabaseConnectionTest.java</summary>
899
+
900
+ ```java
901
+ package com.example.accounting;
902
+
903
+ import com.example.accounting.testsetup.BaseIntegrationTest;
904
+ import org.junit.jupiter.api.DisplayName;
905
+ import org.junit.jupiter.api.Test;
906
+ import org.springframework.beans.factory.annotation.Autowired;
907
+
908
+ import javax.sql.DataSource;
909
+ import java.sql.Connection;
910
+ import java.sql.ResultSet;
911
+ import java.sql.Statement;
912
+
913
+ import static org.assertj.core.api.Assertions.assertThat;
914
+
915
+ @DisplayName("データベース接続")
916
+ class DatabaseConnectionTest extends BaseIntegrationTest {
917
+
918
+ @Autowired
919
+ private DataSource dataSource;
920
+
921
+ @Test
922
+ @DisplayName("PostgreSQLに接続できる")
923
+ void canConnectToPostgres() throws Exception {
924
+ try (Connection conn = dataSource.getConnection();
925
+ Statement stmt = conn.createStatement();
926
+ ResultSet rs = stmt.executeQuery("SELECT 1")) {
927
+ assertThat(rs.next()).isTrue();
928
+ assertThat(rs.getInt(1)).isEqualTo(1);
929
+ }
930
+ }
931
+
932
+ @Test
933
+ @DisplayName("BSPL区分ENUMが作成されている")
934
+ void bsplTypeEnumExists() throws Exception {
935
+ try (Connection conn = dataSource.getConnection();
936
+ Statement stmt = conn.createStatement();
937
+ ResultSet rs = stmt.executeQuery(
938
+ "SELECT unnest(enum_range(NULL::BSPL区分))::text")) {
939
+ assertThat(rs.next()).isTrue();
940
+ assertThat(rs.getString(1)).isEqualTo("BS");
941
+ }
942
+ }
943
+ }
944
+ ```
945
+
946
+ </details>
947
+
948
+ ---
949
+
950
+ ## 14.8 命名規則のまとめ
951
+
952
+ | 要素 | 言語 | 例 |
953
+ |------|------|-----|
954
+ | **テーブル名** | 日本語 | `"勘定科目マスタ"`, `"仕訳データ"` |
955
+ | **カラム名** | 日本語 | `"勘定科目コード"`, `"勘定科目名"` |
956
+ | **ENUM 型** | 日本語 | `BSPL区分`, `貸借区分` |
957
+ | **ENUM 値** | 日本語 | `'BS'`, `'借方'` |
958
+ | **Java クラス名** | 英語 | `Account`, `BsPlType` |
959
+ | **Java フィールド名** | 英語 | `accountCode`, `accountName` |
960
+ | **Java メソッド名** | 英語 | `findByCode()`, `save()` |
961
+ | **JPA アノテーション** | 日本語カラム指定 | `@Column(name = "勘定科目コード")` |
962
+
963
+ ---
964
+
965
+ ## 本章のまとめ
966
+
967
+ 本章では、財務会計システムの全体像と JPA による ORM 実装の基本を学びました。
968
+
969
+ ### 学んだこと
970
+
971
+ | カテゴリ | 内容 |
972
+ |----------|------|
973
+ | スコープ | 勘定科目管理・仕訳処理・残高管理・帳票出力・決算処理 |
974
+ | アーキテクチャ | ヘキサゴナルアーキテクチャ(ポート&アダプター) |
975
+ | ORM | Spring Data JPA / Hibernate |
976
+ | マッピング | @Entity, @Table, @Column, @Convert |
977
+ | ENUM 変換 | AttributeConverter |
978
+ | テスト | TestContainers + @DataJpaTest |
979
+
980
+ ### MyBatis 版との主な違い
981
+
982
+ | 項目 | MyBatis 版 | JPA 版 |
983
+ |------|-----------|--------|
984
+ | SQL 記述 | XML で明示的に記述 | 自動生成(メソッド名規約) |
985
+ | マッピング | resultMap | @Entity + @Column |
986
+ | ENUM 変換 | TypeHandler | AttributeConverter |
987
+ | 関連取得 | 明示的な JOIN | @EntityGraph, FETCH JOIN |
988
+
989
+ ### 次章の予告
990
+
991
+ 第15章では、財務会計システムの基盤となる勘定科目マスタの設計を JPA で実装します。@EmbeddedId を使った複合主キーや @ManyToOne によるリレーションも解説します。