작성
·
3.3K
0
안녕하세요. Jpa린이 입니다.
좋은 강의 감사합니다.
MSA형태로 Application을 구성하려고 합니다.
아래와 같은 경우에 2번DB에 save할때 Jpa가 내부적으로 pk를 가지고 먼저 select하는것 같은데, 2번DB에는 당연히 해당데이터가 없으니 에러가 발생하는 군요.
PK 값을 가지고 있지만 select없이 바로 저장하게 할 수 있을까요?
--아래--
1번DB와 2번DB의 위치는 다르지만, 테이블구조와 데이터는 동일해야합니다.
Eqp1Tr과 Eqp1TrDet는 1:N 구조입니다.
1번 App
- 1번 DB에 저장 -> Kafka Publish (ID포함)
2번 App
- Kafka Consume (ID포함) -> 2번 DB에 저장
<2번 App Consumer 와 Service 코드>
@KafkaListener(topics = "${app.topic.name}", groupId = "${spring.kafka.consumer.group-id}")
public void receiveMessage(@Payload Eqp1Tr eqp1Tr,
@Header(KafkaHeaders.RECEIVED_TOPIC) String topic,
@Header(KafkaHeaders.RECEIVED_PARTITION_ID) Integer partition,
@Header(KafkaHeaders.OFFSET) Long offset) {
log.info("Received message: data = {}, topic = {}, partition = {}, offset = {}", eqp1Tr, topic, partition, offset);
eqp1TrService.createTr(eqp1Tr);
}
public Eqp1Tr createTr(Eqp1Tr eqp1Tr) {
log.info("eqp1Tr : {} {}", eqp1Tr.getEqp1TrDets(), eqp1Tr.getName());
List<Eqp1TrDet> eqp1TrDets = eqp1Tr.getEqp1TrDets().stream()
.collect(Collectors.toList());
// Save at Eqp1Tr, Eqp1TrDet
trRepository.save(eqp1Tr);
trDetRepository.saveAll(eqp1TrDets);
return eqp1Tr;
}
<Error>
...
Hibernate:
select
eqp1trdet0_.id as id1_1_0_,
eqp1trdet0_.createdBy as createdb2_1_0_,
eqp1trdet0_.createdDate as createdd3_1_0_,
eqp1trdet0_.col1 as col4_1_0_,
eqp1trdet0_.col2 as col5_1_0_,
eqp1trdet0_.tr_id as tr_id6_1_0_
from
Eqp1TrDet eqp1trdet0_
where
eqp1trdet0_.id=?
2021-04-24 00:05:45.319 TRACE 19496 --- [ntainer#0-0-C-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [2]
2021-04-24 00:06:25.719 INFO 19496 --- [ntainer#0-0-C-1] o.a.k.clients.consumer.KafkaConsumer : [Consumer clientId=consumer-tr-consumer-service-1, groupId=tr-consumer-service] Seeking to offset 3 for partition eqp.tr.ic.eqp1-0
2021-04-24 00:06:27.013 ERROR 19496 --- [ntainer#0-0-C-1] essageListenerContainer$ListenerConsumer : Error handler threw an exception
org.springframework.kafka.KafkaException: Seek to current after exception; nested exception is org.springframework.kafka.listener.ListenerExecutionFailedException: Listener method 'public void me.kalpha.trconsumerservice.trmart.service.Eqp1TrConsumerService.receiveMessage(me.kalpha.trconsumerservice.trmart.entity.Eqp1Tr,java.lang.String,java.lang.Integer,java.lang.Long)' threw exception; nested exception is org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find me.kalpha.trconsumerservice.trmart.entity.Eqp1TrDet with id 2; nested exception is javax.persistence.EntityNotFoundException: Unable to find me.kalpha.trconsumerservice.trmart.entity.Eqp1TrDet with id 2; nested exception is org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find me.kalpha.trconsumerservice.trmart.entity.Eqp1TrDet with id 2; nested exception is javax.persistence.EntityNotFoundException: Unable to find me.kalpha.trconsumerservice.trmart.entity.Eqp1TrDet with id 2
...
답변 1
0
안녕하세요. 허당더기님
다음 질문을 참고해주세요.
https://www.inflearn.com/questions/197446
혹시 해당 내용이 이해가 어려우면 JPA 기본편 강의를 꼭 들으시길 추천합니다.
감사합니다.