package com.vladmihalcea.book.hpjp.spring.data.crud.service;

import com.vladmihalcea.book.hpjp.hibernate.logging.validator.sql.SQLStatementCountValidator;
import com.vladmihalcea.book.hpjp.spring.data.crud.domain.PostComment;
import com.vladmihalcea.book.hpjp.spring.data.crud.repository.PostCommentRepository;
import com.vladmihalcea.book.hpjp.spring.data.crud.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Vlad Mihalcea
 */
@Service
@Transactional(readOnly = true)
public class PostServiceImpl implements PostService {

    @Autowired
    private PostRepository postRepository;

    @Autowired
    private PostCommentRepository postCommentRepository;

    @Override
    @Transactional
    public PostComment addNewPostComment(String review, Long postId) {
        PostComment comment = new PostComment()
            .setReview(review)
            .setPost(postRepository.findById(postId).orElse(null));

        postCommentRepository.save(comment);

        return comment;
    }
}

@@ -559,16 +559,67 @@
 ional;%0A%0A
+import javax.persistence.EntityNotFoundException;%0A%0A
 /**%0A * @
@@ -1117,13 +1117,173 @@
 Else
-(null
+Throw(%0A                ()-%3E new EntityNotFoundException(%0A                    String.format(%22Post with id %5B%25d%5D was not found!%22, postId)%0A                )%0A            
 ));%0A

Replace orElseNull to orElseThrow