@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,1300 @@
1
+ # 第15章:勘定科目の設計(ORM版)
2
+
3
+ 財務会計システムの根幹となる勘定科目マスタを Spring Data JPA で設計します。本章では、TDD(テスト駆動開発)を用いて勘定科目マスタ、勘定科目構成マスタ、課税取引マスタを JPA エンティティとして実装していきます。
4
+
5
+ ---
6
+
7
+ ## 15.1 勘定科目マスタの設計
8
+
9
+ ### 勘定科目の区分体系
10
+
11
+ 勘定科目は複数の区分で分類されます。これらの区分を正しく理解することが、財務会計システム設計の基礎となります。
12
+
13
+ ```plantuml
14
+ @startuml
15
+
16
+ title 勘定科目の区分体系
17
+
18
+ package "BSPL区分" {
19
+ rectangle "BS(貸借対照表)" as BS #LightBlue
20
+ rectangle "PL(損益計算書)" as PL #LightGreen
21
+ }
22
+
23
+ package "貸借区分" {
24
+ rectangle "借方" as Debit #LightYellow
25
+ rectangle "貸方" as Credit #LightPink
26
+ }
27
+
28
+ package "取引要素区分" {
29
+ rectangle "資産" as Asset
30
+ rectangle "負債" as Liability
31
+ rectangle "資本" as Equity
32
+ rectangle "収益" as Revenue
33
+ rectangle "費用" as Expense
34
+ }
35
+
36
+ package "集計区分" {
37
+ rectangle "見出科目" as Header
38
+ rectangle "集計科目" as Summary
39
+ rectangle "計上科目" as Posting
40
+ }
41
+
42
+ BS --> Asset : 借方
43
+ BS --> Liability : 貸方
44
+ BS --> Equity : 貸方
45
+ PL --> Revenue : 貸方
46
+ PL --> Expense : 借方
47
+
48
+ @enduml
49
+ ```
50
+
51
+ #### 区分の説明
52
+
53
+ | 区分 | 説明 | 値の例 |
54
+ |-----|------|-------|
55
+ | **BSPL区分** | 貸借対照表(BS)か損益計算書(PL)か | `BS`, `PL` |
56
+ | **貸借区分** | 借方科目か貸方科目か | `借方`, `貸方` |
57
+ | **取引要素区分** | 5つの取引要素のどれに該当するか | `資産`, `負債`, `資本`, `収益`, `費用` |
58
+ | **集計区分** | 科目の性質(見出し/集計/計上) | `見出科目`, `集計科目`, `計上科目` |
59
+
60
+ #### 取引要素と貸借区分の関係
61
+
62
+ 複式簿記では、取引要素によって借方・貸方が決まります。
63
+
64
+ | 取引要素 | BSPL区分 | 増加時の貸借 | 減少時の貸借 |
65
+ |---------|---------|------------|------------|
66
+ | 資産 | BS | 借方 | 貸方 |
67
+ | 負債 | BS | 貸方 | 借方 |
68
+ | 資本 | BS | 貸方 | 借方 |
69
+ | 収益 | PL | 貸方 | 借方 |
70
+ | 費用 | PL | 借方 | 貸方 |
71
+
72
+ ### 勘定科目マスタのER図
73
+
74
+ ```plantuml
75
+ @startuml
76
+
77
+ title 勘定科目マスタ
78
+
79
+ entity 勘定科目マスタ {
80
+ 勘定科目コード <<PK>>
81
+ --
82
+ 勘定科目名
83
+ 勘定科目略名
84
+ 勘定科目カナ
85
+ BSPL区分
86
+ 貸借区分
87
+ 取引要素区分
88
+ 集計区分
89
+ 管理会計区分
90
+ 費用区分
91
+ 元帳出力区分
92
+ 補助科目種別
93
+ 消費税計算区分
94
+ 課税取引コード <<FK>>
95
+ 期日管理区分
96
+ 作成日時
97
+ 更新日時
98
+ 更新者名
99
+ }
100
+
101
+ entity 課税取引マスタ {
102
+ 課税取引コード <<PK>>
103
+ --
104
+ 課税取引名
105
+ 税率
106
+ 更新日時
107
+ 更新者名
108
+ }
109
+
110
+ 勘定科目マスタ }o..|| 課税取引マスタ
111
+
112
+ @enduml
113
+ ```
114
+
115
+ ### マイグレーション:ENUM型とテーブルの作成
116
+
117
+ <details>
118
+ <summary>V001__create_enum_types.sql</summary>
119
+
120
+ ```sql
121
+ -- BSPL区分(貸借対照表・損益計算書)
122
+ CREATE TYPE BSPL区分 AS ENUM ('BS', 'PL');
123
+
124
+ -- 貸借区分
125
+ CREATE TYPE 貸借区分 AS ENUM ('借方', '貸方');
126
+
127
+ -- 取引要素区分
128
+ CREATE TYPE 取引要素区分 AS ENUM ('資産', '負債', '資本', '収益', '費用');
129
+
130
+ -- 集計区分
131
+ CREATE TYPE 集計区分 AS ENUM ('見出科目', '集計科目', '計上科目');
132
+
133
+ -- 消費税計算区分
134
+ CREATE TYPE 消費税計算区分 AS ENUM ('課税', '非課税', '不課税', '免税');
135
+ ```
136
+
137
+ </details>
138
+
139
+ <details>
140
+ <summary>V002__create_account_master.sql</summary>
141
+
142
+ ```sql
143
+ -- 課税取引マスタ
144
+ CREATE TABLE "課税取引マスタ" (
145
+ "課税取引コード" VARCHAR(2) PRIMARY KEY,
146
+ "課税取引名" VARCHAR(20) NOT NULL,
147
+ "税率" DECIMAL(5,3) NOT NULL DEFAULT 0.10,
148
+ "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
149
+ "更新者名" VARCHAR(12)
150
+ );
151
+
152
+ -- 勘定科目マスタ
153
+ CREATE TABLE "勘定科目マスタ" (
154
+ "勘定科目コード" VARCHAR(5) PRIMARY KEY,
155
+ "勘定科目名" VARCHAR(40) NOT NULL,
156
+ "勘定科目略名" VARCHAR(10),
157
+ "勘定科目カナ" VARCHAR(40),
158
+ "BSPL区分" BSPL区分 NOT NULL,
159
+ "貸借区分" 貸借区分 NOT NULL,
160
+ "取引要素区分" 取引要素区分 NOT NULL,
161
+ "集計区分" 集計区分 NOT NULL,
162
+ "管理会計区分" VARCHAR(1),
163
+ "費用区分" VARCHAR(1),
164
+ "元帳出力区分" VARCHAR(1),
165
+ "補助科目種別" VARCHAR(1),
166
+ "消費税計算区分" 消費税計算区分,
167
+ "課税取引コード" VARCHAR(2) REFERENCES "課税取引マスタ"("課税取引コード"),
168
+ "期日管理区分" VARCHAR(1),
169
+ "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
170
+ "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
171
+ "更新者名" VARCHAR(12)
172
+ );
173
+
174
+ -- インデックス
175
+ CREATE INDEX idx_勘定科目マスタ_BSPL区分 ON "勘定科目マスタ"("BSPL区分");
176
+ CREATE INDEX idx_勘定科目マスタ_取引要素区分 ON "勘定科目マスタ"("取引要素区分");
177
+ CREATE INDEX idx_勘定科目マスタ_集計区分 ON "勘定科目マスタ"("集計区分");
178
+ ```
179
+
180
+ </details>
181
+
182
+ ---
183
+
184
+ ## 15.2 JPA エンティティの実装
185
+
186
+ ### TDD による勘定科目マスタの実装
187
+
188
+ #### Red: 失敗するテストを書く
189
+
190
+ <details>
191
+ <summary>勘定科目リポジトリテスト</summary>
192
+
193
+ ```java
194
+ package com.example.accounting.infrastructure.persistence.repository;
195
+
196
+ import com.example.accounting.application.port.out.AccountRepository;
197
+ import com.example.accounting.domain.model.account.*;
198
+ import com.example.accounting.testsetup.BaseIntegrationTest;
199
+ import org.junit.jupiter.api.*;
200
+ import org.springframework.beans.factory.annotation.Autowired;
201
+
202
+ import static org.assertj.core.api.Assertions.*;
203
+
204
+ @DisplayName("勘定科目リポジトリ")
205
+ class AccountRepositoryTest extends BaseIntegrationTest {
206
+
207
+ @Autowired
208
+ private AccountRepository accountRepository;
209
+
210
+ @BeforeEach
211
+ void setUp() {
212
+ accountRepository.deleteAll();
213
+ }
214
+
215
+ @Nested
216
+ @DisplayName("登録")
217
+ class Registration {
218
+
219
+ @Test
220
+ @DisplayName("勘定科目を登録できる")
221
+ void canRegisterAccount() {
222
+ // Arrange
223
+ var account = Account.builder()
224
+ .accountCode("11110")
225
+ .accountName("現金")
226
+ .accountShortName("現金")
227
+ .bsPlType(BsPlType.BS)
228
+ .debitCreditType(DebitCreditType.DEBIT)
229
+ .transactionElementType(TransactionElementType.ASSET)
230
+ .aggregationType(AggregationType.POSTING)
231
+ .build();
232
+
233
+ // Act
234
+ accountRepository.save(account);
235
+
236
+ // Assert
237
+ var result = accountRepository.findByCode("11110");
238
+ assertThat(result).isPresent();
239
+ assertThat(result.get().getAccountName()).isEqualTo("現金");
240
+ assertThat(result.get().getBsPlType()).isEqualTo(BsPlType.BS);
241
+ assertThat(result.get().getDebitCreditType()).isEqualTo(DebitCreditType.DEBIT);
242
+ }
243
+
244
+ @Test
245
+ @DisplayName("貸借対照表の資産科目を登録できる")
246
+ void canRegisterBSAssetAccount() {
247
+ var account = createAccount("11000", "流動資産", BsPlType.BS,
248
+ DebitCreditType.DEBIT, TransactionElementType.ASSET, AggregationType.SUMMARY);
249
+ accountRepository.save(account);
250
+
251
+ var result = accountRepository.findByCode("11000");
252
+ assertThat(result).isPresent();
253
+ assertThat(result.get().getBsPlType()).isEqualTo(BsPlType.BS);
254
+ assertThat(result.get().getTransactionElementType()).isEqualTo(TransactionElementType.ASSET);
255
+ }
256
+
257
+ @Test
258
+ @DisplayName("損益計算書の収益科目を登録できる")
259
+ void canRegisterPLRevenueAccount() {
260
+ var account = createAccount("41000", "売上高", BsPlType.PL,
261
+ DebitCreditType.CREDIT, TransactionElementType.REVENUE, AggregationType.SUMMARY);
262
+ accountRepository.save(account);
263
+
264
+ var result = accountRepository.findByCode("41000");
265
+ assertThat(result).isPresent();
266
+ assertThat(result.get().getBsPlType()).isEqualTo(BsPlType.PL);
267
+ assertThat(result.get().getTransactionElementType()).isEqualTo(TransactionElementType.REVENUE);
268
+ }
269
+ }
270
+
271
+ @Nested
272
+ @DisplayName("集計区分")
273
+ class AggregationTypes {
274
+
275
+ @Test
276
+ @DisplayName("見出科目を登録できる")
277
+ void canRegisterHeaderAccount() {
278
+ var account = createAccount("11", "(資産の部)", BsPlType.BS,
279
+ DebitCreditType.DEBIT, TransactionElementType.ASSET, AggregationType.HEADING);
280
+ accountRepository.save(account);
281
+
282
+ var result = accountRepository.findByCode("11");
283
+ assertThat(result.get().getAggregationType()).isEqualTo(AggregationType.HEADING);
284
+ }
285
+
286
+ @Test
287
+ @DisplayName("集計科目を登録できる")
288
+ void canRegisterSummaryAccount() {
289
+ var account = createAccount("11190", "現金及び預金", BsPlType.BS,
290
+ DebitCreditType.DEBIT, TransactionElementType.ASSET, AggregationType.SUMMARY);
291
+ accountRepository.save(account);
292
+
293
+ var result = accountRepository.findByCode("11190");
294
+ assertThat(result.get().getAggregationType()).isEqualTo(AggregationType.SUMMARY);
295
+ }
296
+
297
+ @Test
298
+ @DisplayName("計上科目を登録できる")
299
+ void canRegisterPostingAccount() {
300
+ var account = createAccount("11110", "現金", BsPlType.BS,
301
+ DebitCreditType.DEBIT, TransactionElementType.ASSET, AggregationType.POSTING);
302
+ accountRepository.save(account);
303
+
304
+ var result = accountRepository.findByCode("11110");
305
+ assertThat(result.get().getAggregationType()).isEqualTo(AggregationType.POSTING);
306
+ }
307
+ }
308
+
309
+ private Account createAccount(String code, String name, BsPlType bsPlType,
310
+ DebitCreditType debitCreditType, TransactionElementType transactionElementType,
311
+ AggregationType aggregationType) {
312
+ return Account.builder()
313
+ .accountCode(code)
314
+ .accountName(name)
315
+ .bsPlType(bsPlType)
316
+ .debitCreditType(debitCreditType)
317
+ .transactionElementType(transactionElementType)
318
+ .aggregationType(aggregationType)
319
+ .build();
320
+ }
321
+ }
322
+ ```
323
+
324
+ </details>
325
+
326
+ #### Green: テストを通す実装
327
+
328
+ ##### ドメイン層(JPA エンティティ)
329
+
330
+ <details>
331
+ <summary>Account.java(勘定科目エンティティ)</summary>
332
+
333
+ ```java
334
+ package com.example.accounting.domain.model.account;
335
+
336
+ import com.example.accounting.infrastructure.persistence.converter.*;
337
+ import jakarta.persistence.*;
338
+ import lombok.*;
339
+ import java.time.LocalDateTime;
340
+
341
+ @Entity
342
+ @Table(name = "勘定科目マスタ")
343
+ @Data
344
+ @Builder
345
+ @NoArgsConstructor
346
+ @AllArgsConstructor
347
+ public class Account {
348
+
349
+ @Id
350
+ @Column(name = "勘定科目コード", length = 5)
351
+ private String accountCode;
352
+
353
+ @Column(name = "勘定科目名", length = 40, nullable = false)
354
+ private String accountName;
355
+
356
+ @Column(name = "勘定科目略名", length = 10)
357
+ private String accountShortName;
358
+
359
+ @Column(name = "勘定科目カナ", length = 40)
360
+ private String accountNameKana;
361
+
362
+ @Convert(converter = BsPlTypeConverter.class)
363
+ @Column(name = "BSPL区分", nullable = false, columnDefinition = "BSPL区分")
364
+ private BsPlType bsPlType;
365
+
366
+ @Convert(converter = DebitCreditTypeConverter.class)
367
+ @Column(name = "貸借区分", nullable = false, columnDefinition = "貸借区分")
368
+ private DebitCreditType debitCreditType;
369
+
370
+ @Convert(converter = TransactionElementTypeConverter.class)
371
+ @Column(name = "取引要素区分", nullable = false, columnDefinition = "取引要素区分")
372
+ private TransactionElementType transactionElementType;
373
+
374
+ @Convert(converter = AggregationTypeConverter.class)
375
+ @Column(name = "集計区分", nullable = false, columnDefinition = "集計区分")
376
+ private AggregationType aggregationType;
377
+
378
+ @Column(name = "管理会計区分", length = 1)
379
+ private String managementAccountingType;
380
+
381
+ @Column(name = "費用区分", length = 1)
382
+ private String expenseType;
383
+
384
+ @Column(name = "元帳出力区分", length = 1)
385
+ private String ledgerOutputType;
386
+
387
+ @Column(name = "補助科目種別", length = 1)
388
+ private String subAccountType;
389
+
390
+ @Convert(converter = TaxCalculationTypeConverter.class)
391
+ @Column(name = "消費税計算区分", columnDefinition = "消費税計算区分")
392
+ private TaxCalculationType taxCalculationType;
393
+
394
+ @Column(name = "課税取引コード", length = 2)
395
+ private String taxTransactionCode;
396
+
397
+ @Column(name = "期日管理区分", length = 1)
398
+ private String dueDateManagementType;
399
+
400
+ @Column(name = "作成日時")
401
+ private LocalDateTime createdAt;
402
+
403
+ @Column(name = "更新日時")
404
+ private LocalDateTime updatedAt;
405
+
406
+ @Column(name = "更新者名", length = 12)
407
+ private String updatedBy;
408
+
409
+ @PrePersist
410
+ protected void onCreate() {
411
+ createdAt = LocalDateTime.now();
412
+ updatedAt = LocalDateTime.now();
413
+ }
414
+
415
+ @PreUpdate
416
+ protected void onUpdate() {
417
+ updatedAt = LocalDateTime.now();
418
+ }
419
+ }
420
+ ```
421
+
422
+ </details>
423
+
424
+ ##### ENUM 定義(ドメイン層)
425
+
426
+ <details>
427
+ <summary>BsPlType.java(BSPL区分)</summary>
428
+
429
+ ```java
430
+ package com.example.accounting.domain.model.account;
431
+
432
+ import lombok.Getter;
433
+ import lombok.RequiredArgsConstructor;
434
+
435
+ @Getter
436
+ @RequiredArgsConstructor
437
+ public enum BsPlType {
438
+ BS("BS"),
439
+ PL("PL");
440
+
441
+ private final String displayName;
442
+
443
+ public static BsPlType fromDisplayName(String displayName) {
444
+ for (BsPlType type : values()) {
445
+ if (type.displayName.equals(displayName)) {
446
+ return type;
447
+ }
448
+ }
449
+ throw new IllegalArgumentException("不正なBSPL区分: " + displayName);
450
+ }
451
+ }
452
+ ```
453
+
454
+ </details>
455
+
456
+ <details>
457
+ <summary>DebitCreditType.java(貸借区分)</summary>
458
+
459
+ ```java
460
+ package com.example.accounting.domain.model.account;
461
+
462
+ import lombok.Getter;
463
+ import lombok.RequiredArgsConstructor;
464
+
465
+ @Getter
466
+ @RequiredArgsConstructor
467
+ public enum DebitCreditType {
468
+ DEBIT("借方"),
469
+ CREDIT("貸方");
470
+
471
+ private final String displayName;
472
+
473
+ public static DebitCreditType fromDisplayName(String displayName) {
474
+ for (DebitCreditType type : values()) {
475
+ if (type.displayName.equals(displayName)) {
476
+ return type;
477
+ }
478
+ }
479
+ throw new IllegalArgumentException("不正な貸借区分: " + displayName);
480
+ }
481
+ }
482
+ ```
483
+
484
+ </details>
485
+
486
+ <details>
487
+ <summary>TransactionElementType.java(取引要素区分)</summary>
488
+
489
+ ```java
490
+ package com.example.accounting.domain.model.account;
491
+
492
+ import lombok.Getter;
493
+ import lombok.RequiredArgsConstructor;
494
+
495
+ @Getter
496
+ @RequiredArgsConstructor
497
+ public enum TransactionElementType {
498
+ ASSET("資産"),
499
+ LIABILITY("負債"),
500
+ EQUITY("資本"),
501
+ REVENUE("収益"),
502
+ EXPENSE("費用");
503
+
504
+ private final String displayName;
505
+
506
+ public static TransactionElementType fromDisplayName(String displayName) {
507
+ for (TransactionElementType type : values()) {
508
+ if (type.displayName.equals(displayName)) {
509
+ return type;
510
+ }
511
+ }
512
+ throw new IllegalArgumentException("不正な取引要素区分: " + displayName);
513
+ }
514
+ }
515
+ ```
516
+
517
+ </details>
518
+
519
+ <details>
520
+ <summary>AggregationType.java(集計区分)</summary>
521
+
522
+ ```java
523
+ package com.example.accounting.domain.model.account;
524
+
525
+ import lombok.Getter;
526
+ import lombok.RequiredArgsConstructor;
527
+
528
+ @Getter
529
+ @RequiredArgsConstructor
530
+ public enum AggregationType {
531
+ HEADING("見出科目"),
532
+ SUMMARY("集計科目"),
533
+ POSTING("計上科目");
534
+
535
+ private final String displayName;
536
+
537
+ public static AggregationType fromDisplayName(String displayName) {
538
+ for (AggregationType type : values()) {
539
+ if (type.displayName.equals(displayName)) {
540
+ return type;
541
+ }
542
+ }
543
+ throw new IllegalArgumentException("不正な集計区分: " + displayName);
544
+ }
545
+ }
546
+ ```
547
+
548
+ </details>
549
+
550
+ ##### AttributeConverter(ENUM 変換)
551
+
552
+ JPA では `AttributeConverter` を使って PostgreSQL ENUM と Java Enum を変換します。MyBatis の TypeHandler に相当します。
553
+
554
+ <details>
555
+ <summary>BsPlTypeConverter.java</summary>
556
+
557
+ ```java
558
+ package com.example.accounting.infrastructure.persistence.converter;
559
+
560
+ import com.example.accounting.domain.model.account.BsPlType;
561
+ import jakarta.persistence.AttributeConverter;
562
+ import jakarta.persistence.Converter;
563
+
564
+ @Converter(autoApply = true)
565
+ public class BsPlTypeConverter implements AttributeConverter<BsPlType, String> {
566
+
567
+ @Override
568
+ public String convertToDatabaseColumn(BsPlType attribute) {
569
+ return attribute != null ? attribute.getDisplayName() : null;
570
+ }
571
+
572
+ @Override
573
+ public BsPlType convertToEntityAttribute(String dbData) {
574
+ return dbData != null ? BsPlType.fromDisplayName(dbData) : null;
575
+ }
576
+ }
577
+ ```
578
+
579
+ </details>
580
+
581
+ <details>
582
+ <summary>DebitCreditTypeConverter.java</summary>
583
+
584
+ ```java
585
+ package com.example.accounting.infrastructure.persistence.converter;
586
+
587
+ import com.example.accounting.domain.model.account.DebitCreditType;
588
+ import jakarta.persistence.AttributeConverter;
589
+ import jakarta.persistence.Converter;
590
+
591
+ @Converter(autoApply = true)
592
+ public class DebitCreditTypeConverter implements AttributeConverter<DebitCreditType, String> {
593
+
594
+ @Override
595
+ public String convertToDatabaseColumn(DebitCreditType attribute) {
596
+ return attribute != null ? attribute.getDisplayName() : null;
597
+ }
598
+
599
+ @Override
600
+ public DebitCreditType convertToEntityAttribute(String dbData) {
601
+ return dbData != null ? DebitCreditType.fromDisplayName(dbData) : null;
602
+ }
603
+ }
604
+ ```
605
+
606
+ </details>
607
+
608
+ <details>
609
+ <summary>TransactionElementTypeConverter.java</summary>
610
+
611
+ ```java
612
+ package com.example.accounting.infrastructure.persistence.converter;
613
+
614
+ import com.example.accounting.domain.model.account.TransactionElementType;
615
+ import jakarta.persistence.AttributeConverter;
616
+ import jakarta.persistence.Converter;
617
+
618
+ @Converter(autoApply = true)
619
+ public class TransactionElementTypeConverter implements AttributeConverter<TransactionElementType, String> {
620
+
621
+ @Override
622
+ public String convertToDatabaseColumn(TransactionElementType attribute) {
623
+ return attribute != null ? attribute.getDisplayName() : null;
624
+ }
625
+
626
+ @Override
627
+ public TransactionElementType convertToEntityAttribute(String dbData) {
628
+ return dbData != null ? TransactionElementType.fromDisplayName(dbData) : null;
629
+ }
630
+ }
631
+ ```
632
+
633
+ </details>
634
+
635
+ <details>
636
+ <summary>AggregationTypeConverter.java</summary>
637
+
638
+ ```java
639
+ package com.example.accounting.infrastructure.persistence.converter;
640
+
641
+ import com.example.accounting.domain.model.account.AggregationType;
642
+ import jakarta.persistence.AttributeConverter;
643
+ import jakarta.persistence.Converter;
644
+
645
+ @Converter(autoApply = true)
646
+ public class AggregationTypeConverter implements AttributeConverter<AggregationType, String> {
647
+
648
+ @Override
649
+ public String convertToDatabaseColumn(AggregationType attribute) {
650
+ return attribute != null ? attribute.getDisplayName() : null;
651
+ }
652
+
653
+ @Override
654
+ public AggregationType convertToEntityAttribute(String dbData) {
655
+ return dbData != null ? AggregationType.fromDisplayName(dbData) : null;
656
+ }
657
+ }
658
+ ```
659
+
660
+ </details>
661
+
662
+ ##### Repository インターフェース(Output Port)
663
+
664
+ <details>
665
+ <summary>AccountRepository.java</summary>
666
+
667
+ ```java
668
+ package com.example.accounting.application.port.out;
669
+
670
+ import com.example.accounting.domain.model.account.Account;
671
+ import com.example.accounting.domain.model.account.AggregationType;
672
+ import com.example.accounting.domain.model.account.BsPlType;
673
+ import com.example.accounting.domain.model.account.TransactionElementType;
674
+
675
+ import java.util.List;
676
+ import java.util.Optional;
677
+
678
+ /**
679
+ * 勘定科目リポジトリ(Output Port)
680
+ */
681
+ public interface AccountRepository {
682
+
683
+ void save(Account account);
684
+
685
+ Optional<Account> findByCode(String accountCode);
686
+
687
+ List<Account> findAll();
688
+
689
+ List<Account> findByBsPlType(BsPlType bsPlType);
690
+
691
+ List<Account> findByAggregationType(AggregationType aggregationType);
692
+
693
+ List<Account> findByTransactionElementType(TransactionElementType type);
694
+
695
+ void update(Account account);
696
+
697
+ void deleteAll();
698
+ }
699
+ ```
700
+
701
+ </details>
702
+
703
+ ##### Spring Data JPA Repository
704
+
705
+ <details>
706
+ <summary>AccountJpaRepository.java</summary>
707
+
708
+ ```java
709
+ package com.example.accounting.infrastructure.persistence.repository;
710
+
711
+ import com.example.accounting.domain.model.account.Account;
712
+ import com.example.accounting.domain.model.account.AggregationType;
713
+ import com.example.accounting.domain.model.account.BsPlType;
714
+ import com.example.accounting.domain.model.account.TransactionElementType;
715
+ import org.springframework.data.jpa.repository.JpaRepository;
716
+ import org.springframework.data.jpa.repository.Modifying;
717
+ import org.springframework.data.jpa.repository.Query;
718
+ import org.springframework.stereotype.Repository;
719
+
720
+ import java.util.List;
721
+
722
+ @Repository
723
+ public interface AccountJpaRepository extends JpaRepository<Account, String> {
724
+
725
+ List<Account> findByBsPlType(BsPlType bsPlType);
726
+
727
+ List<Account> findByAggregationType(AggregationType aggregationType);
728
+
729
+ List<Account> findByTransactionElementType(TransactionElementType transactionElementType);
730
+
731
+ List<Account> findAllByOrderByAccountCodeAsc();
732
+
733
+ @Modifying
734
+ @Query(value = "TRUNCATE TABLE \"勘定科目マスタ\" CASCADE", nativeQuery = true)
735
+ void truncateTable();
736
+ }
737
+ ```
738
+
739
+ </details>
740
+
741
+ ##### Repository 実装クラス(インフラストラクチャ層)
742
+
743
+ <details>
744
+ <summary>AccountRepositoryImpl.java</summary>
745
+
746
+ ```java
747
+ package com.example.accounting.infrastructure.persistence.repository;
748
+
749
+ import com.example.accounting.application.port.out.AccountRepository;
750
+ import com.example.accounting.domain.model.account.Account;
751
+ import com.example.accounting.domain.model.account.AggregationType;
752
+ import com.example.accounting.domain.model.account.BsPlType;
753
+ import com.example.accounting.domain.model.account.TransactionElementType;
754
+ import lombok.RequiredArgsConstructor;
755
+ import org.springframework.stereotype.Repository;
756
+ import org.springframework.transaction.annotation.Transactional;
757
+
758
+ import java.util.List;
759
+ import java.util.Optional;
760
+
761
+ @Repository
762
+ @RequiredArgsConstructor
763
+ public class AccountRepositoryImpl implements AccountRepository {
764
+
765
+ private final AccountJpaRepository jpaRepository;
766
+
767
+ @Override
768
+ public void save(Account account) {
769
+ jpaRepository.save(account);
770
+ }
771
+
772
+ @Override
773
+ public Optional<Account> findByCode(String accountCode) {
774
+ return jpaRepository.findById(accountCode);
775
+ }
776
+
777
+ @Override
778
+ public List<Account> findAll() {
779
+ return jpaRepository.findAllByOrderByAccountCodeAsc();
780
+ }
781
+
782
+ @Override
783
+ public List<Account> findByBsPlType(BsPlType bsPlType) {
784
+ return jpaRepository.findByBsPlType(bsPlType);
785
+ }
786
+
787
+ @Override
788
+ public List<Account> findByAggregationType(AggregationType aggregationType) {
789
+ return jpaRepository.findByAggregationType(aggregationType);
790
+ }
791
+
792
+ @Override
793
+ public List<Account> findByTransactionElementType(TransactionElementType type) {
794
+ return jpaRepository.findByTransactionElementType(type);
795
+ }
796
+
797
+ @Override
798
+ public void update(Account account) {
799
+ jpaRepository.save(account);
800
+ }
801
+
802
+ @Override
803
+ @Transactional
804
+ public void deleteAll() {
805
+ jpaRepository.truncateTable();
806
+ }
807
+ }
808
+ ```
809
+
810
+ </details>
811
+
812
+ ---
813
+
814
+ ## 15.3 勘定科目構成マスタの設計
815
+
816
+ ### チルダ連結方式による階層構造
817
+
818
+ 勘定科目の階層構造を「チルダ(^)連結」で表現します。これにより、SQL の LIKE 検索で効率的に階層検索ができます。
819
+
820
+ ```plantuml
821
+ @startwbs
822
+ title 貸借対照表の科目ツリー構造(部分)
823
+
824
+ * 貸借対照表
825
+ ** 資産の部
826
+ *** 流動資産
827
+ **** 現金及び預金
828
+ ***** 現金
829
+ ***** 当座預金
830
+ ***** 普通預金
831
+ *** 固定資産
832
+ **** 有形固定資産
833
+ ***** 建物
834
+ ***** 機械及び装置
835
+ ** 負債の部
836
+ *** 流動負債
837
+ **** 支払手形
838
+ **** 買掛金
839
+ ** 資本の部
840
+ *** 資本金
841
+ *** 利益剰余金
842
+ @endwbs
843
+ ```
844
+
845
+ ### 勘定科目構成マスタのデータ例
846
+
847
+ | 勘定科目コード | 勘定科目パス |
848
+ |-------------|-------------|
849
+ | 11 | 11 |
850
+ | 11000 | 11^11000 |
851
+ | 11190 | 11^11000^11190 |
852
+ | 11110 | 11^11000^11190^11110 |
853
+ | 11120 | 11^11000^11190^11120 |
854
+ | 11130 | 11^11000^11190^11130 |
855
+
856
+ ### マイグレーション
857
+
858
+ <details>
859
+ <summary>V003__create_account_structure.sql</summary>
860
+
861
+ ```sql
862
+ -- 勘定科目構成マスタ
863
+ CREATE TABLE "勘定科目構成マスタ" (
864
+ "勘定科目コード" VARCHAR(5) PRIMARY KEY REFERENCES "勘定科目マスタ"("勘定科目コード"),
865
+ "勘定科目パス" VARCHAR(100) NOT NULL,
866
+ "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
867
+ "更新者名" VARCHAR(12)
868
+ );
869
+
870
+ -- インデックス(パス検索用)
871
+ CREATE INDEX idx_勘定科目構成マスタ_パス ON "勘定科目構成マスタ"("勘定科目パス");
872
+ ```
873
+
874
+ </details>
875
+
876
+ ### TDD による勘定科目構成マスタの実装
877
+
878
+ #### JPA エンティティ
879
+
880
+ <details>
881
+ <summary>AccountStructure.java</summary>
882
+
883
+ ```java
884
+ package com.example.accounting.domain.model.account;
885
+
886
+ import jakarta.persistence.*;
887
+ import lombok.*;
888
+ import java.time.LocalDateTime;
889
+
890
+ @Entity
891
+ @Table(name = "勘定科目構成マスタ")
892
+ @Data
893
+ @Builder
894
+ @NoArgsConstructor
895
+ @AllArgsConstructor
896
+ public class AccountStructure {
897
+
898
+ @Id
899
+ @Column(name = "勘定科目コード", length = 5)
900
+ private String accountCode;
901
+
902
+ @Column(name = "勘定科目パス", length = 100, nullable = false)
903
+ private String accountPath;
904
+
905
+ @Column(name = "更新日時")
906
+ private LocalDateTime updatedAt;
907
+
908
+ @Column(name = "更新者名", length = 12)
909
+ private String updatedBy;
910
+
911
+ /**
912
+ * パスの深さ(階層レベル)を取得
913
+ */
914
+ @Transient
915
+ public int getDepth() {
916
+ if (accountPath == null || accountPath.isEmpty()) {
917
+ return 0;
918
+ }
919
+ return accountPath.split("\\^").length - 1;
920
+ }
921
+
922
+ /**
923
+ * 親科目コードを取得
924
+ */
925
+ @Transient
926
+ public String getParentCode() {
927
+ if (accountPath == null || !accountPath.contains("^")) {
928
+ return null;
929
+ }
930
+ String[] parts = accountPath.split("\\^");
931
+ if (parts.length < 2) {
932
+ return null;
933
+ }
934
+ return parts[parts.length - 2];
935
+ }
936
+
937
+ @PrePersist
938
+ @PreUpdate
939
+ protected void onUpdate() {
940
+ updatedAt = LocalDateTime.now();
941
+ }
942
+ }
943
+ ```
944
+
945
+ </details>
946
+
947
+ #### Repository インターフェース(Output Port)
948
+
949
+ <details>
950
+ <summary>AccountStructureRepository.java</summary>
951
+
952
+ ```java
953
+ package com.example.accounting.application.port.out;
954
+
955
+ import com.example.accounting.domain.model.account.AccountStructure;
956
+
957
+ import java.util.List;
958
+ import java.util.Optional;
959
+
960
+ /**
961
+ * 勘定科目構成リポジトリ(Output Port)
962
+ */
963
+ public interface AccountStructureRepository {
964
+
965
+ void save(AccountStructure structure);
966
+
967
+ Optional<AccountStructure> findByCode(String accountCode);
968
+
969
+ List<AccountStructure> findAll();
970
+
971
+ List<AccountStructure> findByPathContaining(String pathSegment);
972
+
973
+ List<AccountStructure> findChildren(String parentCode);
974
+
975
+ void update(AccountStructure structure);
976
+
977
+ void deleteAll();
978
+ }
979
+ ```
980
+
981
+ </details>
982
+
983
+ #### Spring Data JPA Repository
984
+
985
+ <details>
986
+ <summary>AccountStructureJpaRepository.java</summary>
987
+
988
+ ```java
989
+ package com.example.accounting.infrastructure.persistence.repository;
990
+
991
+ import com.example.accounting.domain.model.account.AccountStructure;
992
+ import org.springframework.data.jpa.repository.JpaRepository;
993
+ import org.springframework.data.jpa.repository.Modifying;
994
+ import org.springframework.data.jpa.repository.Query;
995
+ import org.springframework.data.repository.query.Param;
996
+ import org.springframework.stereotype.Repository;
997
+
998
+ import java.util.List;
999
+
1000
+ @Repository
1001
+ public interface AccountStructureJpaRepository extends JpaRepository<AccountStructure, String> {
1002
+
1003
+ List<AccountStructure> findAllByOrderByAccountCodeAsc();
1004
+
1005
+ @Query("SELECT s FROM AccountStructure s WHERE s.accountPath LIKE %:pathSegment% ORDER BY s.accountCode")
1006
+ List<AccountStructure> findByAccountPathContaining(@Param("pathSegment") String pathSegment);
1007
+
1008
+ @Query("SELECT s FROM AccountStructure s WHERE s.accountPath LIKE :parentCode% ORDER BY s.accountCode")
1009
+ List<AccountStructure> findChildrenByParentCode(@Param("parentCode") String parentCode);
1010
+
1011
+ @Modifying
1012
+ @Query(value = "TRUNCATE TABLE \"勘定科目構成マスタ\" CASCADE", nativeQuery = true)
1013
+ void truncateTable();
1014
+ }
1015
+ ```
1016
+
1017
+ </details>
1018
+
1019
+ #### Repository 実装クラス
1020
+
1021
+ <details>
1022
+ <summary>AccountStructureRepositoryImpl.java</summary>
1023
+
1024
+ ```java
1025
+ package com.example.accounting.infrastructure.persistence.repository;
1026
+
1027
+ import com.example.accounting.application.port.out.AccountStructureRepository;
1028
+ import com.example.accounting.domain.model.account.AccountStructure;
1029
+ import lombok.RequiredArgsConstructor;
1030
+ import org.springframework.stereotype.Repository;
1031
+ import org.springframework.transaction.annotation.Transactional;
1032
+
1033
+ import java.util.List;
1034
+ import java.util.Optional;
1035
+
1036
+ @Repository
1037
+ @RequiredArgsConstructor
1038
+ public class AccountStructureRepositoryImpl implements AccountStructureRepository {
1039
+
1040
+ private final AccountStructureJpaRepository jpaRepository;
1041
+
1042
+ @Override
1043
+ public void save(AccountStructure structure) {
1044
+ jpaRepository.save(structure);
1045
+ }
1046
+
1047
+ @Override
1048
+ public Optional<AccountStructure> findByCode(String accountCode) {
1049
+ return jpaRepository.findById(accountCode);
1050
+ }
1051
+
1052
+ @Override
1053
+ public List<AccountStructure> findAll() {
1054
+ return jpaRepository.findAllByOrderByAccountCodeAsc();
1055
+ }
1056
+
1057
+ @Override
1058
+ public List<AccountStructure> findByPathContaining(String pathSegment) {
1059
+ return jpaRepository.findByAccountPathContaining(pathSegment);
1060
+ }
1061
+
1062
+ @Override
1063
+ public List<AccountStructure> findChildren(String parentCode) {
1064
+ return jpaRepository.findChildrenByParentCode(parentCode + "^");
1065
+ }
1066
+
1067
+ @Override
1068
+ public void update(AccountStructure structure) {
1069
+ jpaRepository.save(structure);
1070
+ }
1071
+
1072
+ @Override
1073
+ @Transactional
1074
+ public void deleteAll() {
1075
+ jpaRepository.truncateTable();
1076
+ }
1077
+ }
1078
+ ```
1079
+
1080
+ </details>
1081
+
1082
+ ---
1083
+
1084
+ ## 15.4 課税取引マスタの設計
1085
+
1086
+ ### 課税取引コード・税率の管理
1087
+
1088
+ 課税取引マスタは、消費税の税率情報を管理するマスタテーブルです。
1089
+
1090
+ | 課税取引コード | 課税取引名 | 税率 |
1091
+ |-------------|-----------|------|
1092
+ | 00 | 非課税 | 0.000 |
1093
+ | 08 | 軽減税率 | 0.080 |
1094
+ | 10 | 標準税率 | 0.100 |
1095
+
1096
+ ### TDD による課税取引マスタの実装
1097
+
1098
+ #### JPA エンティティ
1099
+
1100
+ <details>
1101
+ <summary>TaxTransaction.java</summary>
1102
+
1103
+ ```java
1104
+ package com.example.accounting.domain.model.tax;
1105
+
1106
+ import jakarta.persistence.*;
1107
+ import lombok.*;
1108
+ import java.math.BigDecimal;
1109
+ import java.time.LocalDateTime;
1110
+
1111
+ @Entity
1112
+ @Table(name = "課税取引マスタ")
1113
+ @Data
1114
+ @Builder
1115
+ @NoArgsConstructor
1116
+ @AllArgsConstructor
1117
+ public class TaxTransaction {
1118
+
1119
+ @Id
1120
+ @Column(name = "課税取引コード", length = 2)
1121
+ private String taxCode;
1122
+
1123
+ @Column(name = "課税取引名", length = 20, nullable = false)
1124
+ private String taxName;
1125
+
1126
+ @Builder.Default
1127
+ @Column(name = "税率", precision = 5, scale = 3, nullable = false)
1128
+ private BigDecimal taxRate = new BigDecimal("0.10");
1129
+
1130
+ @Column(name = "更新日時")
1131
+ private LocalDateTime updatedAt;
1132
+
1133
+ @Column(name = "更新者名", length = 12)
1134
+ private String updatedBy;
1135
+
1136
+ /**
1137
+ * 税込金額を計算
1138
+ */
1139
+ public BigDecimal calculateTaxIncludedAmount(BigDecimal amount) {
1140
+ return amount.add(amount.multiply(taxRate));
1141
+ }
1142
+
1143
+ /**
1144
+ * 消費税額を計算
1145
+ */
1146
+ public BigDecimal calculateTaxAmount(BigDecimal amount) {
1147
+ return amount.multiply(taxRate);
1148
+ }
1149
+
1150
+ @PrePersist
1151
+ @PreUpdate
1152
+ protected void onUpdate() {
1153
+ updatedAt = LocalDateTime.now();
1154
+ }
1155
+ }
1156
+ ```
1157
+
1158
+ </details>
1159
+
1160
+ #### Spring Data JPA Repository
1161
+
1162
+ <details>
1163
+ <summary>TaxTransactionJpaRepository.java</summary>
1164
+
1165
+ ```java
1166
+ package com.example.accounting.infrastructure.persistence.repository;
1167
+
1168
+ import com.example.accounting.domain.model.tax.TaxTransaction;
1169
+ import org.springframework.data.jpa.repository.JpaRepository;
1170
+ import org.springframework.data.jpa.repository.Modifying;
1171
+ import org.springframework.data.jpa.repository.Query;
1172
+ import org.springframework.stereotype.Repository;
1173
+
1174
+ import java.util.List;
1175
+
1176
+ @Repository
1177
+ public interface TaxTransactionJpaRepository extends JpaRepository<TaxTransaction, String> {
1178
+
1179
+ List<TaxTransaction> findAllByOrderByTaxCodeAsc();
1180
+
1181
+ @Modifying
1182
+ @Query(value = "TRUNCATE TABLE \"課税取引マスタ\" CASCADE", nativeQuery = true)
1183
+ void truncateTable();
1184
+ }
1185
+ ```
1186
+
1187
+ </details>
1188
+
1189
+ ---
1190
+
1191
+ ## 15.5 MyBatis 版との比較
1192
+
1193
+ ### 実装の違い
1194
+
1195
+ | 観点 | MyBatis 版 | JPA 版 |
1196
+ |------|-----------|--------|
1197
+ | マッピング | XML Mapper + resultMap | @Entity + @Column |
1198
+ | ENUM 変換 | TypeHandler | AttributeConverter |
1199
+ | Repository | Mapper インターフェース + XML | JpaRepository インターフェース |
1200
+ | SQL | XML で明示的に記述 | メソッド名規約で自動生成 |
1201
+ | トランザクション管理 | 明示的に記述 | @Transactional |
1202
+ | TRUNCATE | XML で定義 | @Query(nativeQuery) |
1203
+
1204
+ ### コード量の比較
1205
+
1206
+ | 項目 | MyBatis 版 | JPA 版 |
1207
+ |------|-----------|--------|
1208
+ | エンティティ | POJO(アノテーションなし) | @Entity + @Column |
1209
+ | ENUM 変換 | TypeHandler(各4クラス) | AttributeConverter(各4クラス) |
1210
+ | Mapper/Repository | Mapper + XML | JpaRepository インターフェースのみ |
1211
+ | SQL | XML で全て記述 | メソッド名規約 + @Query |
1212
+
1213
+ ---
1214
+
1215
+ ## 第15章のまとめ
1216
+
1217
+ ### 作成したテーブル
1218
+
1219
+ | テーブル名 | 説明 |
1220
+ |----------|------|
1221
+ | `課税取引マスタ` | 消費税の税率情報を管理 |
1222
+ | `勘定科目マスタ` | 勘定科目の基本情報を管理 |
1223
+ | `勘定科目構成マスタ` | 勘定科目の階層構造をチルダ連結で管理 |
1224
+
1225
+ ### ER図
1226
+
1227
+ ```plantuml
1228
+ @startuml
1229
+
1230
+ title 勘定科目関連 ER図
1231
+
1232
+ entity 課税取引マスタ {
1233
+ 課税取引コード <<PK>>
1234
+ --
1235
+ 課税取引名
1236
+ 税率
1237
+ 更新日時
1238
+ 更新者名
1239
+ }
1240
+
1241
+ entity 勘定科目マスタ {
1242
+ 勘定科目コード <<PK>>
1243
+ --
1244
+ 勘定科目名
1245
+ 勘定科目略名
1246
+ 勘定科目カナ
1247
+ BSPL区分
1248
+ 貸借区分
1249
+ 取引要素区分
1250
+ 集計区分
1251
+ 管理会計区分
1252
+ 費用区分
1253
+ 元帳出力区分
1254
+ 補助科目種別
1255
+ 消費税計算区分
1256
+ 課税取引コード <<FK>>
1257
+ 期日管理区分
1258
+ ...
1259
+ }
1260
+
1261
+ entity 勘定科目構成マスタ {
1262
+ 勘定科目コード <<PK,FK>>
1263
+ --
1264
+ 勘定科目パス
1265
+ 更新日時
1266
+ 更新者名
1267
+ }
1268
+
1269
+ 勘定科目マスタ }o..|| 課税取引マスタ
1270
+ 勘定科目マスタ ||--|| 勘定科目構成マスタ
1271
+
1272
+ @enduml
1273
+ ```
1274
+
1275
+ ### 設計のポイント
1276
+
1277
+ 1. **ENUM 型の活用**: BSPL区分、貸借区分、取引要素区分、集計区分で型安全性を確保
1278
+ 2. **日本語 DB / 英語 Java**: テーブル・カラムは日本語、Java コードは英語で統一
1279
+ 3. **AttributeConverter**: Java Enum と日本語 ENUM 値の変換を JPA AttributeConverter で実装
1280
+ 4. **チルダ連結方式**: 勘定科目構成マスタで階層構造を表現し、JPQL の LIKE 検索で効率的に階層検索
1281
+ 5. **ヘキサゴナルアーキテクチャ**: ドメイン層がインフラに依存しない設計
1282
+ 6. **JpaRepository**: メソッド名規約による自動 SQL 生成
1283
+
1284
+ ### 命名規則
1285
+
1286
+ | 要素 | 言語 | 例 |
1287
+ |------|------|-----|
1288
+ | **テーブル名** | 日本語 | `"勘定科目マスタ"`, `"課税取引マスタ"` |
1289
+ | **カラム名** | 日本語 | `"勘定科目コード"`, `"勘定科目名"` |
1290
+ | **ENUM 型** | 日本語 | `BSPL区分`, `貸借区分` |
1291
+ | **ENUM 値** | 日本語 | `'BS'`, `'借方'` |
1292
+ | **Java クラス名** | 英語 | `Account`, `AccountStructure` |
1293
+ | **Java フィールド名** | 英語 | `accountCode`, `accountName` |
1294
+ | **JPA @Column** | 日本語カラム指定 | `@Column(name = "勘定科目コード")` |
1295
+
1296
+ ---
1297
+
1298
+ ## 次章の予告
1299
+
1300
+ 第16章では、仕訳テーブルの設計に進みます。複式簿記の基本構造である「借方・貸方」の仕訳データを JPA で実装していきます。@EmbeddedId を使った複合主キーや @ManyToOne によるリレーションも解説します。