| src/main/java/info/ahlberg/spring/Contact.java |
| 1 | | package info.ahlberg.spring; |
| 2 | | |
| 3 | | import java.io.Serializable; |
| 4 | | |
| 5 | | import javax.persistence.Column; |
| 6 | | import javax.persistence.Entity; |
| 7 | | import javax.persistence.GeneratedValue; |
| 8 | | import javax.persistence.GenerationType; |
| 9 | | import javax.persistence.Id; |
| 10 | | import javax.persistence.Version; |
| 11 | | |
| 12 | | @Entity |
| 13 | | public class Contact implements Serializable { |
| 14 | | private static final long serialVersionUID = 1L; |
| 15 | | |
| 16 | | @Id |
| 17 | | @GeneratedValue(strategy = GenerationType.AUTO) |
| 18 | | private Long id; |
| 19 | | |
| 20 | | @Version |
| 21 | | private Long version; |
| 22 | | |
| 23 | | @Column(nullable = false) |
| 24 | | private String name; |
| 25 | | |
| 26 | | public Long getId() { |
| 27 | | return id; |
| 28 | | } |
| 29 | | |
| 30 | | public void setId(Long id) { |
| 31 | | this.id = id; |
| 32 | | } |
| 33 | | |
| 34 | | public Long getVersion() { |
| 35 | | return version; |
| 36 | | } |
| 37 | | |
| 38 | | public void setVersion(Long version) { |
| 39 | | this.version = version; |
| 40 | | } |
| 41 | | |
| 42 | | public String getName() { |
| 43 | | return name; |
| 44 | | } |
| 45 | | |
| 46 | | public void setName(String name) { |
| 47 | | this.name = name; |
| 48 | | } |
| 49 | | |
| 50 | | } |
| src/main/java/info/ahlberg/spring/ContactController.java |
| 1 | | package info.ahlberg.spring; |
| 2 | | |
| 3 | | import org.springframework.beans.factory.annotation.Autowired; |
| 4 | | import org.springframework.stereotype.Controller; |
| 5 | | import org.springframework.ui.Model; |
| 6 | | import org.springframework.web.bind.annotation.RequestMapping; |
| 7 | | import org.springframework.web.bind.annotation.RequestParam; |
| 8 | | |
| 9 | | @Controller |
| 10 | | @RequestMapping("/") |
| 11 | | public class ContactController { |
| 12 | | @Autowired |
| 13 | | private ContactService contactService; |
| 14 | | |
| 15 | | @RequestMapping("/") |
| 16 | | String index(Model model) { |
| 17 | | model.addAttribute("msg", "A nice message"); |
| 18 | | |
| 19 | | model.addAttribute("contacts", contactService.findAll()); |
| 20 | | |
| 21 | | return "index"; |
| 22 | | } |
| 23 | | |
| 24 | | |
| 25 | | @RequestMapping("/add") |
| 26 | | String add(@RequestParam String name, Model model) { |
| 27 | | model.addAttribute("msg", "A nice message"); |
| 28 | | |
| 29 | | Contact contact = new Contact(); |
| 30 | | contact.setName(name); |
| 31 | | contactService.save(contact); |
| 32 | | |
| 33 | | model.addAttribute("contacts", contactService.findAll()); |
| 34 | | |
| 35 | | return "index"; |
| 36 | | } |
| 37 | | } |
| src/main/java/info/ahlberg/spring/ContactServiceImpl.java |
| 1 | | package info.ahlberg.spring; |
| 2 | | |
| 3 | | import org.springframework.beans.factory.annotation.Autowired; |
| 4 | | import org.springframework.stereotype.Service; |
| 5 | | import org.springframework.transaction.annotation.Transactional; |
| 6 | | |
| 7 | | @Service |
| 8 | | public class ContactServiceImpl implements ContactService { |
| 9 | | @Autowired |
| 10 | | private ContactDAO contactDAO; |
| 11 | | |
| 12 | | @Override |
| 13 | | public Object findAll() { |
| 14 | | return contactDAO.loadAll(); |
| 15 | | } |
| 16 | | |
| 17 | | @Override |
| 18 | | @Transactional |
| 19 | | public void save(Contact contact) { |
| 20 | | contactDAO.persist(contact); |
| 21 | | } |
| 22 | | |
| 23 | | } |
| src/main/java/info/ahlberg/spring/GenericDAOWithJPA.java |
| 1 | | package info.ahlberg.spring; |
| 2 | | |
| 3 | | import java.io.Serializable; |
| 4 | | import java.lang.reflect.ParameterizedType; |
| 5 | | import java.util.List; |
| 6 | | |
| 7 | | import javax.persistence.EntityManager; |
| 8 | | import javax.persistence.PersistenceContext; |
| 9 | | |
| 10 | | public abstract class GenericDAOWithJPA<T, ID extends Serializable> implements GenericDAO<T, ID> { |
| 11 | | |
| 12 | | private Class<T> persistentClass; |
| 13 | | |
| 14 | | protected EntityManager entityManager; |
| 15 | | |
| 16 | | @SuppressWarnings("unchecked") |
| 17 | | public GenericDAOWithJPA() { |
| 18 | | this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()) |
| 19 | | .getActualTypeArguments()[0]; |
| 20 | | } |
| 21 | | |
| 22 | | @PersistenceContext |
| 23 | | public void setEntityManager(EntityManager entityManager) { |
| 24 | | this.entityManager = entityManager; |
| 25 | | } |
| 26 | | |
| 27 | | public void flush() { |
| 28 | | entityManager.flush(); |
| 29 | | } |
| 30 | | |
| 31 | | public Class<T> getPersistentClass() { |
| 32 | | return persistentClass; |
| 33 | | } |
| 34 | | |
| 35 | | public T loadById(ID id) { |
| 36 | | if(id == null) { |
| 37 | | return null; |
| 38 | | } else { |
| 39 | | return entityManager.find(persistentClass, id); |
| 40 | | } |
| 41 | | } |
| 42 | | |
| 43 | | public void persist(T entity) { |
| 44 | | entityManager.persist(entity); |
| 45 | | } |
| 46 | | |
| 47 | | public void update(T entity) { |
| 48 | | entityManager.merge(entity); |
| 49 | | entityManager.flush(); |
| 50 | | } |
| 51 | | |
| 52 | | public void delete(T entity) { |
| 53 | | entityManager.remove(entity); |
| 54 | | } |
| 55 | | |
| 56 | | @SuppressWarnings("unchecked") |
| 57 | | public List<T> loadAll() { |
| 58 | | return entityManager.createQuery("Select t from " + persistentClass.getSimpleName() + " t") |
| 59 | | .setHint("org.hibernate.cacheable", true).getResultList(); |
| 60 | | } |
| 61 | | } |
| src/main/java/info/ahlberg/spring/controller/ContactController.java |
| 1 | package info.ahlberg.spring.controller; |
| 2 | |
| 3 | import info.ahlberg.spring.domain.Contact; |
| 4 | import info.ahlberg.spring.service.ContactService; |
| 5 | |
| 6 | import org.springframework.beans.factory.annotation.Autowired; |
| 7 | import org.springframework.stereotype.Controller; |
| 8 | import org.springframework.ui.Model; |
| 9 | import org.springframework.web.bind.annotation.RequestMapping; |
| 10 | import org.springframework.web.bind.annotation.RequestParam; |
| 11 | |
| 12 | @Controller |
| 13 | @RequestMapping("/") |
| 14 | public class ContactController { |
| 15 | @Autowired |
| 16 | private ContactService contactService; |
| 17 | |
| 18 | @RequestMapping("/") |
| 19 | String index(Model model) { |
| 20 | model.addAttribute("msg", "A nice message"); |
| 21 | |
| 22 | model.addAttribute("contacts", contactService.findAll()); |
| 23 | |
| 24 | return "index"; |
| 25 | } |
| 26 | |
| 27 | |
| 28 | @RequestMapping("/add") |
| 29 | String add(@RequestParam String name, Model model) { |
| 30 | model.addAttribute("msg", "A nice message"); |
| 31 | |
| 32 | Contact contact = new Contact(); |
| 33 | contact.setName(name); |
| 34 | contactService.save(contact); |
| 35 | |
| 36 | model.addAttribute("contacts", contactService.findAll()); |
| 37 | |
| 38 | return "index"; |
| 39 | } |
| 40 | } |
| src/main/java/info/ahlberg/spring/dao/GenericDAOWithJPA.java |
| 1 | package info.ahlberg.spring.dao; |
| 2 | |
| 3 | import java.io.Serializable; |
| 4 | import java.lang.reflect.ParameterizedType; |
| 5 | import java.util.List; |
| 6 | |
| 7 | import javax.persistence.EntityManager; |
| 8 | import javax.persistence.PersistenceContext; |
| 9 | |
| 10 | public abstract class GenericDAOWithJPA<T, ID extends Serializable> implements GenericDAO<T, ID> { |
| 11 | |
| 12 | private Class<T> persistentClass; |
| 13 | |
| 14 | protected EntityManager entityManager; |
| 15 | |
| 16 | @SuppressWarnings("unchecked") |
| 17 | public GenericDAOWithJPA() { |
| 18 | this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()) |
| 19 | .getActualTypeArguments()[0]; |
| 20 | } |
| 21 | |
| 22 | @PersistenceContext |
| 23 | public void setEntityManager(EntityManager entityManager) { |
| 24 | this.entityManager = entityManager; |
| 25 | } |
| 26 | |
| 27 | public void flush() { |
| 28 | entityManager.flush(); |
| 29 | } |
| 30 | |
| 31 | public Class<T> getPersistentClass() { |
| 32 | return persistentClass; |
| 33 | } |
| 34 | |
| 35 | public T loadById(ID id) { |
| 36 | if(id == null) { |
| 37 | return null; |
| 38 | } else { |
| 39 | return entityManager.find(persistentClass, id); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | public void persist(T entity) { |
| 44 | entityManager.persist(entity); |
| 45 | } |
| 46 | |
| 47 | public void update(T entity) { |
| 48 | entityManager.merge(entity); |
| 49 | entityManager.flush(); |
| 50 | } |
| 51 | |
| 52 | public void delete(T entity) { |
| 53 | entityManager.remove(entity); |
| 54 | } |
| 55 | |
| 56 | @SuppressWarnings("unchecked") |
| 57 | public List<T> loadAll() { |
| 58 | return entityManager.createQuery("Select t from " + persistentClass.getSimpleName() + " t") |
| 59 | .setHint("org.hibernate.cacheable", true).getResultList(); |
| 60 | } |
| 61 | } |
| src/main/java/info/ahlberg/spring/domain/Contact.java |
| 1 | package info.ahlberg.spring.domain; |
| 2 | |
| 3 | import java.io.Serializable; |
| 4 | |
| 5 | import javax.persistence.Column; |
| 6 | import javax.persistence.Entity; |
| 7 | import javax.persistence.GeneratedValue; |
| 8 | import javax.persistence.GenerationType; |
| 9 | import javax.persistence.Id; |
| 10 | import javax.persistence.Version; |
| 11 | |
| 12 | @Entity |
| 13 | public class Contact implements Serializable { |
| 14 | private static final long serialVersionUID = 1L; |
| 15 | |
| 16 | @Id |
| 17 | @GeneratedValue(strategy = GenerationType.AUTO) |
| 18 | private Long id; |
| 19 | |
| 20 | @Version |
| 21 | private Long version; |
| 22 | |
| 23 | @Column(nullable = false) |
| 24 | private String name; |
| 25 | |
| 26 | public Long getId() { |
| 27 | return id; |
| 28 | } |
| 29 | |
| 30 | public void setId(Long id) { |
| 31 | this.id = id; |
| 32 | } |
| 33 | |
| 34 | public Long getVersion() { |
| 35 | return version; |
| 36 | } |
| 37 | |
| 38 | public void setVersion(Long version) { |
| 39 | this.version = version; |
| 40 | } |
| 41 | |
| 42 | public String getName() { |
| 43 | return name; |
| 44 | } |
| 45 | |
| 46 | public void setName(String name) { |
| 47 | this.name = name; |
| 48 | } |
| 49 | |
| 50 | } |
| src/main/java/info/ahlberg/spring/service/ContactServiceImpl.java |
| 1 | package info.ahlberg.spring.service; |
| 2 | |
| 3 | import info.ahlberg.spring.dao.ContactDAO; |
| 4 | import info.ahlberg.spring.domain.Contact; |
| 5 | |
| 6 | import org.springframework.beans.factory.annotation.Autowired; |
| 7 | import org.springframework.stereotype.Service; |
| 8 | import org.springframework.transaction.annotation.Transactional; |
| 9 | |
| 10 | @Service |
| 11 | public class ContactServiceImpl implements ContactService { |
| 12 | @Autowired |
| 13 | private ContactDAO contactDAO; |
| 14 | |
| 15 | @Override |
| 16 | public Object findAll() { |
| 17 | return contactDAO.loadAll(); |
| 18 | } |
| 19 | |
| 20 | @Override |
| 21 | @Transactional |
| 22 | public void save(Contact contact) { |
| 23 | contactDAO.persist(contact); |
| 24 | } |
| 25 | |
| 26 | } |