Created
September 19, 2021 13:43
-
-
Save hoangnt-2197/c824e2fee119e1bee6fa0f7e2b9c6124 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service | |
@Transactional(readOnly = true) | |
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 | |
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