Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. package zesp03.webapp.page;
  2.  
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.ui.ModelMap;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import zesp03.common.core.Database;
  8. import zesp03.common.exception.NotFoundException;
  9. import zesp03.webapp.dto.BuildingDto;
  10.  
  11. import javax.persistence.EntityManager;
  12. import javax.persistence.EntityTransaction;
  13.  
  14. @Controller
  15. public class BuildingPage {
  16.     @GetMapping("/building")
  17.     public String get(
  18.             @RequestParam("id") long id,
  19.             ModelMap model) {
  20.         EntityManager em = null;
  21.         EntityTransaction tran = null;
  22.         try {
  23.             em = Database.createEntityManager();
  24.             tran = em.getTransaction();
  25.             tran.begin();
  26.  
  27.             zesp03.common.entity.Building b = em.find(zesp03.common.entity.Building.class, id);
  28.             if(b == null)
  29.                 throw new NotFoundException("building");
  30.             BuildingDto dto = BuildingDto.make(b);
  31.             model.put("building", dto);
  32.             tran.commit();
  33.             return "building";
  34.         } catch (RuntimeException exc) {
  35.             if (tran != null && tran.isActive()) tran.rollback();
  36.             throw exc;
  37.         } finally {
  38.             if (em != null) em.close();
  39.         }
  40.     }
  41. }
  42.  
  43. --------------------------------------------
  44. package zesp03.webapp.page;
  45.  
  46. import org.springframework.stereotype.Controller;
  47. import org.springframework.ui.ModelMap;
  48. import org.springframework.web.bind.annotation.GetMapping;
  49. import zesp03.common.core.Database;
  50. import zesp03.common.entity.Building;
  51.  
  52. import javax.persistence.EntityManager;
  53. import javax.persistence.EntityTransaction;
  54. import java.util.List;
  55.  
  56. @Controller
  57. public class AllBuildingsPage {
  58.     @GetMapping("/all-buildings")
  59.     public String get(ModelMap model) {
  60.         EntityManager em = null;
  61.         EntityTransaction tran = null;
  62.  
  63.  
  64.         List<Building> BuildingList;
  65.  
  66.         try {
  67.  
  68.             em = Database.createEntityManager();
  69.             tran = em.getTransaction();
  70.  
  71.             tran.begin();
  72.  
  73.             BuildingList = em.createQuery("SELECT b FROM Building b", Building.class).getResultList();
  74.             tran.commit();
  75.  
  76.         } catch (RuntimeException exc) {
  77.             if( tran != null && tran.isActive() )
  78.                 tran.rollback();
  79.             throw exc;
  80.         } finally {
  81.             if (em != null)
  82.                 em.close();
  83.         }
  84.  
  85.         model.put("list", BuildingList);
  86.         return "all-buildings";
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement