1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.mobility.writer.dao;
17
18 import org.kuali.mobility.writer.entity.Topic;
19 import org.springframework.stereotype.Repository;
20 import org.springframework.transaction.annotation.Transactional;
21
22 import javax.persistence.EntityManager;
23 import javax.persistence.PersistenceContext;
24 import javax.persistence.Query;
25 import java.util.List;
26
27
28
29
30
31
32 @Repository
33 public class TopicDaoImpl implements TopicDao{
34
35 @PersistenceContext
36 private EntityManager entityManager;
37
38 public EntityManager getEntityManager() {
39 return entityManager;
40 }
41
42 public void setEntityManager(EntityManager entityManager) {
43 this.entityManager = entityManager;
44 }
45
46
47 @Override
48 public List<Topic> getTopics() {
49 Query query = getEntityManager().createNamedQuery("Topic.getTopics");
50 return query.getResultList();
51 }
52
53
54 @Override
55 public Topic getTopic(long topicId) {
56 return getEntityManager().find(Topic.class, topicId);
57 }
58
59 @Override
60 @Transactional
61 public Topic saveTopic(Topic topic) {
62
63 if(topic.getId() == null){
64 entityManager.persist(topic);
65 return topic;
66 }
67 else{
68 return entityManager.merge(topic);
69 }
70
71 }
72
73
74 }