[Chapter 08] EntityManager #119
Answered
by
corock
BvrPark
asked this question in
Chapter 08. Data Persistence
-
1.
|
Beta Was this translation helpful? Give feedback.
Answered by
corock
Nov 11, 2023
Replies: 1 comment
-
네 맞습니다. 책에 나오는 예제로 간단하게 테스트해 볼 수 있습니다. XxxApplication.java @Slf4j
@SpringBootApplication
public class XxxApplication {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(CorockApplication.class, args);
HotelService hotelService = ctx.getBean(HotelService.class);
HotelCreateRequest request = new HotelCreateRequest();
request.setName("The LINE LA");
request.setAddress("3515 Wilshire Blvd, Los Angeles, CA 90010");
request.setPhoneNumber("+12133817411");
request.setRoomCount(8);
hotelService.createHotel(request);
}
} HotelService.java @Service
@RequiredArgsConstructor
public class HotelService {
private final HotelRepository hotelRepository;
@Transactional(readOnly = false, isolation = Isolation.SERIALIZABLE)
public HotelCreateResponse createHotel(HotelCreateRequest createRequest) {
HotelEntity hotelEntity = HotelEntity.of(createRequest.getName(), createRequest.getAddress(),
createRequest.getPhoneNumber());
int roomCount = createRequest.getRoomCount();
List<HotelRoomEntity> hotelRoomEntities = IntStream.range(0, roomCount)
.mapToObj(i -> HotelRoomEntity.of("ROOM-" + i, HotelRoomType.DOUBLE, BigDecimal.valueOf(100)))
.collect(Collectors.toList());
hotelEntity.addHotelRooms(hotelRoomEntities);
HotelEntity savedHotelEntity = hotelRepository.save(hotelEntity);
return HotelCreateResponse.of(savedHotelEntity.getHotelId());
}
} HotelAuditListener.java @Slf4j
public class HotelAuditListener {
@PrePersist
public void testBeforeCreated(HotelEntity hotelEntity) {
log.info("before HotelEntity created. HotelEntity: {}", hotelEntity);
}
@PostPersist
public void logWhenCreated(HotelEntity hotelEntity) {
log.info("after HotelEntity created. HotelEntity: {}", hotelEntity);
}
} hotel-pre-persist.json {
"address": "3515 Wilshire Blvd, Los Angeles, CA 90010",
"hotelId": null,
"hotelRoomEntities": [
"HotelRoomEntity(hotelRoomId=null, hotelId=null, roomNumber=ROOM-0, roomType=DOUBLE, originalPrice=100)",
"HotelRoomEntity(hotelRoomId=null, hotelId=null, roomNumber=ROOM-1, roomType=DOUBLE, originalPrice=100)",
"HotelRoomEntity(hotelRoomId=null, hotelId=null, roomNumber=ROOM-2, roomType=DOUBLE, originalPrice=100)",
"HotelRoomEntity(hotelRoomId=null, hotelId=null, roomNumber=ROOM-3, roomType=DOUBLE, originalPrice=100)",
"HotelRoomEntity(hotelRoomId=null, hotelId=null, roomNumber=ROOM-4, roomType=DOUBLE, originalPrice=100)",
"HotelRoomEntity(hotelRoomId=null, hotelId=null, roomNumber=ROOM-5, roomType=DOUBLE, originalPrice=100)",
"HotelRoomEntity(hotelRoomId=null, hotelId=null, roomNumber=ROOM-6, roomType=DOUBLE, originalPrice=100)",
"HotelRoomEntity(hotelRoomId=null, hotelId=null, roomNumber=ROOM-7, roomType=DOUBLE, originalPrice=100)"
],
"name": "The LINE LA",
"phoneNumber": "+12133817411",
"roomCount": 8,
"status": "READY",
"createdBy": null,
"createdDate": "2023-11-11T19:11:07.053588+09:00[Asia/Seoul]",
"modifiedBy": null,
"modifiedDate": null
} hotel-post-persist.json {
"address": "3515 Wilshire Blvd, Los Angeles, CA 90010",
"hotelId": 6,
"hotelRoomEntities": [
"HotelRoomEntity(hotelRoomId=null, hotelId=null, roomNumber=ROOM-0, roomType=DOUBLE, originalPrice=100)",
"HotelRoomEntity(hotelRoomId=null, hotelId=null, roomNumber=ROOM-1, roomType=DOUBLE, originalPrice=100)",
"HotelRoomEntity(hotelRoomId=null, hotelId=null, roomNumber=ROOM-2, roomType=DOUBLE, originalPrice=100)",
"HotelRoomEntity(hotelRoomId=null, hotelId=null, roomNumber=ROOM-3, roomType=DOUBLE, originalPrice=100)",
"HotelRoomEntity(hotelRoomId=null, hotelId=null, roomNumber=ROOM-4, roomType=DOUBLE, originalPrice=100)",
"HotelRoomEntity(hotelRoomId=null, hotelId=null, roomNumber=ROOM-5, roomType=DOUBLE, originalPrice=100)",
"HotelRoomEntity(hotelRoomId=null, hotelId=null, roomNumber=ROOM-6, roomType=DOUBLE, originalPrice=100)",
"HotelRoomEntity(hotelRoomId=null, hotelId=null, roomNumber=ROOM-7, roomType=DOUBLE, originalPrice=100)"
],
"name": "The LINE LA",
"phoneNumber": "+12133817411",
"roomCount": 8,
"status": "READY",
"createdBy": null,
"createdDate": "2023-11-11T19:11:07.053588+09:00[Asia/Seoul]",
"modifiedBy": null,
"modifiedDate": null
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
BvrPark
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
네 맞습니다. 책에 나오는 예제로 간단하게 테스트해 볼 수 있습니다.
XxxApplication.java
HotelService.java