Skip to content

Instantly share code, notes, and snippets.

@hoangnt-2197
Created September 19, 2021 13:31
Show Gist options
  • Save hoangnt-2197/eaca1b8d174641db68b31e9ac5ed3c5d to your computer and use it in GitHub Desktop.
Save hoangnt-2197/eaca1b8d174641db68b31e9ac5ed3c5d to your computer and use it in GitHub Desktop.
@Service
public class ForumServiceImpl
implements ForumService {
@PersistenceContext
private EntityManager entityManager;
@Override
@Transactional
public Post newPost(String title, String... tags) {
Post post = new Post();
post.setTitle(title);
post.getTags().addAll(
entityManager.createQuery("""
select t
from Tag t
where t.name in :tags
""", Tag.class)
.setParameter("tags", Arrays.asList(tags))
.getResultList()
);
entityManager.persist(post);
return post;
}
@Override
@Transactional(readOnly = true)
public List<Post> findAllPostsByTitle(String title) {
return entityManager.createQuery("""
select p
from Post p
where p.title = :title
""", Post.class)
.setParameter("title", title)
.getResultList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment