diff --git a/springboot-starter-script/pom.xml b/springboot-starter-script/pom.xml index 12f37acf..07940b93 100644 --- a/springboot-starter-script/pom.xml +++ b/springboot-starter-script/pom.xml @@ -44,6 +44,13 @@ groovy-xml + + + jakarta.persistence + jakarta.persistence-api + true + + org.springframework.boot diff --git a/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptField.java b/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptField.java new file mode 100644 index 00000000..16faf593 --- /dev/null +++ b/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptField.java @@ -0,0 +1,87 @@ +package com.codingapi.springboot.script.persistence; + +import lombok.Getter; + +/** + * 脚本持久化对象:实体字段上是脚本对象,数据库列上是 key + * + *

实体字段声明为 {@code GroovyScriptField} 即可被 {@link GroovyScriptFieldConverter} 自动转换, + * 业务侧不需要在转换器里处理 key,也不会因为字段是普通 String 而漏掉处理。

+ * + *

key 生命周期:

+ * + */ +@Getter +public class GroovyScriptField { + + /** + * 脚本唯一编码,null 表示尚未落库 + */ + private String key; + + /** + * 脚本正文 + */ + private String content; + + private GroovyScriptField(String key, String content) { + this.key = key; + this.content = content; + } + + /** + * 新建脚本(key 由框架按策略生成) + * + * @param content 脚本正文 + * @return 脚本持久化对象 + */ + public static GroovyScriptField of(String content) { + return new GroovyScriptField(null, content); + } + + /** + * 业务指定 key 的脚本(或从库中还原) + * + * @param key 脚本唯一编码 + * @param content 脚本正文 + * @return 脚本持久化对象 + */ + public static GroovyScriptField of(String key, String content) { + return new GroovyScriptField(key, content); + } + + /** + * 沿用上一版本已持久化的 key,替换脚本正文 + * + * @param previous 上一版本,null 表示新建 + * @param content 新的脚本正文 + * @return 脚本持久化对象 + */ + public static GroovyScriptField carry(GroovyScriptField previous, String content) { + if (previous == null) { + return of(content); + } + return new GroovyScriptField(previous.key, content); + } + + /** + * 是否尚未落库 + */ + public boolean isNew() { + return this.key == null; + } + + /** + * 落库:key 为空时由 {@link GroovyScriptKeyGeneratorContext} 生成,非空时复用同一 key 覆盖内容 + * + * @return 脚本 key + */ + public String persist() { + this.key = GroovyScriptPersistenceContext.getInstance().save(this.key, this.content); + return this.key; + } +} diff --git a/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptFieldConverter.java b/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptFieldConverter.java new file mode 100644 index 00000000..d3cad086 --- /dev/null +++ b/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptFieldConverter.java @@ -0,0 +1,46 @@ +package com.codingapi.springboot.script.persistence; + +import jakarta.persistence.AttributeConverter; +import jakarta.persistence.Converter; + +/** + * 脚本字段转换器:数据库列存 key,实体属性是脚本持久化对象 + * + *

标注 {@code autoApply = true},实体字段只要是 {@link GroovyScriptField} 类型即自动生效, + * 业务侧无需为每个字段写转换代码。

+ * + *

key 的生成、复用与内容存取都委托给 {@link GroovyScriptPersistenceContext}, + * 因此非 JPA 的持久化实现可以复用同一套抽象,而不是绑死在 JPA 上。

+ */ +@Converter(autoApply = true) +public class GroovyScriptFieldConverter implements AttributeConverter { + + /** + * 落库:脚本持久化对象转换为 key + * + * @param attribute 脚本持久化对象 + * @return 脚本 key + */ + @Override + public String convertToDatabaseColumn(GroovyScriptField attribute) { + if (attribute == null) { + return null; + } + return attribute.persist(); + } + + /** + * 读取:key 还原为脚本持久化对象 + * + * @param dbData 脚本 key + * @return 脚本持久化对象 + */ + @Override + public GroovyScriptField convertToEntityAttribute(String dbData) { + if (dbData == null) { + return null; + } + String content = GroovyScriptPersistenceContext.getInstance().getScript(dbData); + return GroovyScriptField.of(dbData, content); + } +} diff --git a/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptKeyGenerator.java b/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptKeyGenerator.java new file mode 100644 index 00000000..d2baef60 --- /dev/null +++ b/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptKeyGenerator.java @@ -0,0 +1,27 @@ +package com.codingapi.springboot.script.persistence; + +import java.util.UUID; + +/** + * 脚本 key 生成策略 + * + *

业务可以先自行决定 key({@link GroovyScriptField#of(String, String)}), + * 只有业务未指定 key 时才由该策略兜底。默认 UUID。

+ */ +@FunctionalInterface +public interface GroovyScriptKeyGenerator { + + /** + * 生成脚本 key + * + * @return 脚本唯一编码 + */ + String generate(); + + /** + * 默认策略:UUID(去掉分隔符) + */ + static GroovyScriptKeyGenerator uuid() { + return () -> UUID.randomUUID().toString().replace("-", ""); + } +} diff --git a/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptKeyGeneratorContext.java b/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptKeyGeneratorContext.java new file mode 100644 index 00000000..d10c0282 --- /dev/null +++ b/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptKeyGeneratorContext.java @@ -0,0 +1,38 @@ +package com.codingapi.springboot.script.persistence; + +import lombok.Getter; + +/** + * 脚本 key 生成策略上下文 + * + *

业务在启动时通过 {@link #setGenerator(GroovyScriptKeyGenerator)} 注入自己的策略, + * 未注入时使用默认 UUID 策略。

+ */ +public class GroovyScriptKeyGeneratorContext { + + @Getter + private final static GroovyScriptKeyGeneratorContext instance = new GroovyScriptKeyGeneratorContext(); + + private GroovyScriptKeyGenerator generator = GroovyScriptKeyGenerator.uuid(); + + private GroovyScriptKeyGeneratorContext() { + } + + /** + * 替换 key 生成策略,入参为 null 时回退为默认 UUID 策略 + * + * @param generator key 生成策略 + */ + public void setGenerator(GroovyScriptKeyGenerator generator) { + this.generator = (generator == null) ? GroovyScriptKeyGenerator.uuid() : generator; + } + + /** + * 生成脚本 key + * + * @return 脚本唯一编码 + */ + public String generate() { + return this.generator.generate(); + } +} diff --git a/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptPersistenceContext.java b/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptPersistenceContext.java new file mode 100644 index 00000000..40c9e15c --- /dev/null +++ b/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptPersistenceContext.java @@ -0,0 +1,50 @@ +package com.codingapi.springboot.script.persistence; + +import lombok.Getter; + +/** + * 脚本持久化仓库上下文 + * + *

默认使用 {@link GroovyScriptRepositoryImpl};非 JPA 的持久化实现(文档库、缓存库等) + * 可以通过 {@link #setRepository(GroovyScriptRepository)} 替换为自己的实现。

+ */ +public class GroovyScriptPersistenceContext { + + @Getter + private final static GroovyScriptPersistenceContext instance = new GroovyScriptPersistenceContext(); + + private GroovyScriptRepository repository = new GroovyScriptRepositoryImpl(); + + private GroovyScriptPersistenceContext() { + } + + /** + * 替换脚本持久化仓库实现,入参为 null 时回退为默认实现 + * + * @param repository 脚本持久化仓库 + */ + public void setRepository(GroovyScriptRepository repository) { + this.repository = (repository == null) ? new GroovyScriptRepositoryImpl() : repository; + } + + /** + * 保存脚本正文 + * + * @param key 已有 key,null 表示新建 + * @param content 脚本正文 + * @return 最终使用的 key + */ + public String save(String key, String content) { + return this.repository.save(key, content); + } + + /** + * 按 key 读取脚本正文 + * + * @param key 脚本 key + * @return 脚本正文,不存在时返回空串 + */ + public String getScript(String key) { + return this.repository.getScript(key); + } +} diff --git a/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptRepository.java b/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptRepository.java new file mode 100644 index 00000000..3b4f1200 --- /dev/null +++ b/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptRepository.java @@ -0,0 +1,28 @@ +package com.codingapi.springboot.script.persistence; + +/** + * 脚本持久化仓库抽象:对外只暴露「脚本正文」,内部负责 key 的生成、复用与存储 + * + *

JPA 场景由 {@link GroovyScriptFieldConverter} 委托本仓库实现; + * 非 JPA 场景(文档库、缓存库、自定义表结构等)可直接通过 + * {@link GroovyScriptPersistenceContext} 使用同一套能力,无需自己处理 key。

+ */ +public interface GroovyScriptRepository { + + /** + * 保存脚本正文 + * + * @param key 已有 key,null 表示新建 + * @param content 脚本正文 + * @return 最终使用的 key + */ + String save(String key, String content); + + /** + * 按 key 读取脚本正文 + * + * @param key 脚本 key + * @return 脚本正文,不存在时返回空串 + */ + String getScript(String key); +} diff --git a/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptRepositoryImpl.java b/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptRepositoryImpl.java new file mode 100644 index 00000000..07c55942 --- /dev/null +++ b/springboot-starter-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptRepositoryImpl.java @@ -0,0 +1,43 @@ +package com.codingapi.springboot.script.persistence; + +import com.codingapi.springboot.script.GroovyScript; +import com.codingapi.springboot.script.repository.GroovyScriptRepositoryContext; + +/** + * 默认脚本持久化仓库实现:基于脚本仓储完成正文与 key 的映射 + * + *

key 生命周期:key 为空时由 {@link GroovyScriptKeyGeneratorContext} 生成,非空时复用并覆盖内容。

+ */ +public class GroovyScriptRepositoryImpl implements GroovyScriptRepository { + + /** + * 保存脚本正文 + * + * @param key 已有 key,null 表示新建 + * @param content 脚本正文 + * @return 最终使用的 key + */ + @Override + public String save(String key, String content) { + String targetKey = (key == null || key.isBlank()) + ? GroovyScriptKeyGeneratorContext.getInstance().generate() + : key; + GroovyScript.builder(targetKey).script(content).build().save(); + return targetKey; + } + + /** + * 按 key 读取脚本正文 + * + * @param key 脚本 key + * @return 脚本正文,不存在时返回空串 + */ + @Override + public String getScript(String key) { + if (key == null || key.isBlank()) { + return ""; + } + GroovyScript groovyScript = GroovyScriptRepositoryContext.getInstance().get(key); + return groovyScript == null ? "" : groovyScript.getScript(); + } +} diff --git a/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/GroovyScriptPersistPocTest.java b/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/GroovyScriptPersistPocTest.java new file mode 100644 index 00000000..38b4a56a --- /dev/null +++ b/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/GroovyScriptPersistPocTest.java @@ -0,0 +1,192 @@ +package com.codingapi.springboot.script.poc; + +import com.codingapi.springboot.script.persistence.GroovyScriptField; +import com.codingapi.springboot.script.persistence.GroovyScriptKeyGeneratorContext; +import com.codingapi.springboot.script.persistence.GroovyScriptPersistenceContext; +import com.codingapi.springboot.script.persistence.GroovyScriptRepository; +import com.codingapi.springboot.script.poc.domain.Order; +import com.codingapi.springboot.script.poc.domain.OrderRepository; +import com.codingapi.springboot.script.poc.infra.OrderEntity; +import com.codingapi.springboot.script.poc.infra.OrderJpaRepository; +import com.codingapi.springboot.script.poc.infra.OrderRepositoryImpl; +import com.codingapi.springboot.script.repository.GroovyScriptRepositoryContext; +import jakarta.persistence.EntityManager; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +/** + * POC:验证「数据库列存 key、实体字段是脚本对象、领域对象是正文」的自动转换 + * + *

实体字段类型为 {@link GroovyScriptField},转换由框架的 autoApply 转换器完成, + * 业务侧没有针对 key 的代码。

+ */ +@DataJpaTest +class GroovyScriptPersistPocTest { + + private static final String TABLE = "poc_order"; + + private static final String COLUMN = "rule_key"; + + @Autowired + private OrderJpaRepository orderJpaRepository; + + @Autowired + private EntityManager entityManager; + + private final List keys = new ArrayList<>(); + + @AfterEach + void tearDown() { + for (String key : keys) { + GroovyScriptRepositoryContext.getInstance().delete(key); + } + keys.clear(); + } + + @Test + void saveShouldStoreKeyInsteadOfScriptContent() { + String script = "def run(){ return 1 }"; + Order order = Order.create("订单A", script); + + repository().save(order); + entityManager.flush(); + + String storedKey = rawColumn(order.getId()); + keys.add(storedKey); + + assertNotNull(storedKey); + assertNotEquals(script, storedKey); + assertEquals(script, GroovyScriptRepositoryContext.getInstance().get(storedKey).getScript()); + } + + @Test + void loadShouldAssembleScriptContentIntoDomainObject() { + String script = "def run(){ return 2 }"; + Order order = Order.create("订单B", script); + repository().save(order); + entityManager.flush(); + keys.add(rawColumn(order.getId())); + + entityManager.clear(); + Order loaded = repository().getOrder(order.getId()); + + assertNotNull(loaded); + assertEquals(script, loaded.getRule()); + } + + @Test + void updateShouldReusePreviousKeyAndOnlyChangeContent() { + Order order = Order.create("订单C", "return 1;"); + repository().save(order); + entityManager.flush(); + String key = rawColumn(order.getId()); + keys.add(key); + + entityManager.clear(); + Order loaded = repository().getOrder(order.getId()); + loaded.changeRule("return 2;"); + repository().save(loaded); + entityManager.flush(); + + // 列里的 key 不变,脚本内容被覆盖 + assertEquals(key, rawColumn(order.getId())); + assertEquals("return 2;", GroovyScriptRepositoryContext.getInstance().get(key).getScript()); + } + + @Test + void businessSuppliedKeyShouldBeUsedDirectly() { + OrderEntity entity = new OrderEntity(); + entity.setName("订单D"); + entity.setRule(GroovyScriptField.of("business-rule-key", "return 3;")); + orderJpaRepository.saveAndFlush(entity); + keys.add("business-rule-key"); + + // 业务给的 key 优先级高于框架生成策略 + assertEquals("business-rule-key", rawColumn(entity.getId())); + assertEquals("return 3;", GroovyScriptRepositoryContext.getInstance().get("business-rule-key").getScript()); + } + + @Test + void nullScriptFieldShouldStayNull() { + OrderEntity entity = new OrderEntity(); + entity.setName("订单E"); + orderJpaRepository.saveAndFlush(entity); + + assertNull(rawColumn(entity.getId())); + + entityManager.clear(); + OrderEntity loaded = orderJpaRepository.findById(entity.getId()).orElseThrow(); + assertNull(loaded.getRule()); + } + + @Test + void keyGeneratorShouldBeReplaceableThroughContext() { + GroovyScriptKeyGeneratorContext.getInstance().setGenerator(() -> "context-generated-key"); + try { + Order order = Order.create("订单F", "return 4;"); + repository().save(order); + entityManager.flush(); + keys.add(rawColumn(order.getId())); + + // 业务注入的策略优先于默认 UUID + assertEquals("context-generated-key", rawColumn(order.getId())); + } finally { + GroovyScriptKeyGeneratorContext.getInstance().setGenerator(null); + } + } + + @Test + void nonJpaPersistenceShouldReuseRepositoryAbstraction() { + Map documents = new HashMap<>(); + GroovyScriptPersistenceContext.getInstance().setRepository(new GroovyScriptRepository() { + @Override + public String save(String key, String content) { + String targetKey = (key == null) ? "document-" + documents.size() : key; + documents.put(targetKey, content); + return targetKey; + } + + @Override + public String getScript(String key) { + return documents.getOrDefault(key, ""); + } + }); + try { + String key = GroovyScriptPersistenceContext.getInstance().save(null, "return 5;"); + assertEquals("return 5;", GroovyScriptPersistenceContext.getInstance().getScript(key)); + + String sameKey = GroovyScriptPersistenceContext.getInstance().save(key, "return 6;"); + assertEquals(key, sameKey); + assertEquals("return 6;", documents.get(key)); + } finally { + GroovyScriptPersistenceContext.getInstance().setRepository(null); + } + } + + private OrderRepository repository() { + return new OrderRepositoryImpl(orderJpaRepository); + } + + /** + * 直接读取数据库列,确认列里存的是 key 而不是脚本正文 + */ + private String rawColumn(long id) { + Object value = entityManager + .createNativeQuery("select " + COLUMN + " from " + TABLE + " where id = ?1") + .setParameter(1, id) + .getSingleResult(); + return value == null ? null : value.toString(); + } +} diff --git a/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/domain/Order.java b/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/domain/Order.java new file mode 100644 index 00000000..f4d9b02a --- /dev/null +++ b/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/domain/Order.java @@ -0,0 +1,42 @@ +package com.codingapi.springboot.script.poc.domain; + +import lombok.Getter; + +/** + * POC 演示领域对象:带脚本的业务对象 + * + *

领域层只保存脚本正文(String),不依赖脚本框架的任何类型,也不感知 key 与存储位置。

+ */ +@Getter +public class Order { + + private long id; + + private final String name; + + private String rule; + + public Order(long id, String name, String rule) { + this.id = id; + this.name = name; + this.rule = rule; + } + + /** + * 新建订单(脚本正文由领域自己决定怎么存) + */ + public static Order create(String name, String rule) { + return new Order(0, name, rule); + } + + /** + * 修改脚本正文 + */ + public void changeRule(String rule) { + this.rule = rule; + } + + public void setId(long id) { + this.id = id; + } +} diff --git a/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/domain/OrderRepository.java b/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/domain/OrderRepository.java new file mode 100644 index 00000000..4ae33695 --- /dev/null +++ b/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/domain/OrderRepository.java @@ -0,0 +1,11 @@ +package com.codingapi.springboot.script.poc.domain; + +/** + * POC 演示领域仓储接口,由基础设施层实现 + */ +public interface OrderRepository { + + void save(Order order); + + Order getOrder(long id); +} diff --git a/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/infra/OrderEntity.java b/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/infra/OrderEntity.java new file mode 100644 index 00000000..cd4a4df6 --- /dev/null +++ b/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/infra/OrderEntity.java @@ -0,0 +1,50 @@ +package com.codingapi.springboot.script.poc.infra; + +import com.codingapi.springboot.script.persistence.GroovyScriptField; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import lombok.Getter; +import lombok.Setter; + +/** + * POC 演示 JPA 实体:脚本字段声明为 {@link GroovyScriptField},数据库列只存 key + * + *

字段类型本身就是「需要按脚本处理」的显式标识,转换由框架的 + * {@code GroovyScriptFieldConverter} 自动完成,业务不需要写转换代码。

+ */ +@Getter +@Setter +@Entity +@Table(name = "poc_order") +public class OrderEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + private String name; + + /** + * 脚本字段:数据库列 rule_key 存 key,属性上是脚本持久化对象 + */ + @Column(name = "rule_key", length = 128) + private GroovyScriptField rule; + + /** + * 更新脚本正文:沿用已落库的 key,未落库时由框架生成 + */ + public void changeRule(String content) { + this.rule = GroovyScriptField.carry(this.rule, content); + } + + /** + * 脚本正文 + */ + public String getRuleContent() { + return this.rule == null ? "" : this.rule.getContent(); + } +} diff --git a/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/infra/OrderJpaRepository.java b/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/infra/OrderJpaRepository.java new file mode 100644 index 00000000..1c200034 --- /dev/null +++ b/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/infra/OrderJpaRepository.java @@ -0,0 +1,9 @@ +package com.codingapi.springboot.script.poc.infra; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface OrderJpaRepository extends JpaRepository { + +} diff --git a/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/infra/OrderRepositoryImpl.java b/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/infra/OrderRepositoryImpl.java new file mode 100644 index 00000000..5d578b29 --- /dev/null +++ b/springboot-starter-script/src/test/java/com/codingapi/springboot/script/poc/infra/OrderRepositoryImpl.java @@ -0,0 +1,38 @@ +package com.codingapi.springboot.script.poc.infra; + +import com.codingapi.springboot.script.poc.domain.Order; +import com.codingapi.springboot.script.poc.domain.OrderRepository; + +/** + * POC 演示:持久化层只做「脚本正文 <-> 脚本持久化对象」的搬运,key 的生成与复用完全由框架处理 + */ +public class OrderRepositoryImpl implements OrderRepository { + + private final OrderJpaRepository orderJpaRepository; + + public OrderRepositoryImpl(OrderJpaRepository orderJpaRepository) { + this.orderJpaRepository = orderJpaRepository; + } + + @Override + public void save(Order order) { + OrderEntity entity = order.getId() > 0 + ? orderJpaRepository.findById(order.getId()).orElseGet(OrderEntity::new) + : new OrderEntity(); + entity.setName(order.getName()); + // 已有 key 沿用时不需要额外查询;新建时由框架在落库时生成 + entity.changeRule(order.getRule()); + + OrderEntity saved = orderJpaRepository.save(entity); + order.setId(saved.getId()); + } + + @Override + public Order getOrder(long id) { + OrderEntity entity = orderJpaRepository.findById(id).orElse(null); + if (entity == null) { + return null; + } + return new Order(entity.getId(), entity.getName(), entity.getRuleContent()); + } +}