Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 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.entity.Building;
  9. import zesp03.common.entity.Unit;
  10. import zesp03.webapp.dto.BuildingDto;
  11. import zesp03.webapp.dto.UnitBuildingsDto;
  12. import zesp03.webapp.dto.UnitDto;
  13.  
  14. import javax.persistence.EntityManager;
  15. import javax.persistence.EntityTransaction;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18.  
  19. /**
  20.  * Created by Media on 2017-03-28.
  21.  */
  22.  
  23. @Controller
  24. public class LinkUnitAllBuildingsPage {
  25.         @GetMapping("/link-unit-all-buildings")
  26.         public String get(@RequestParam("id") long id,
  27.                           ModelMap model) {
  28.             EntityManager em = null;
  29.             EntityTransaction tran = null;
  30.  
  31.             try {
  32.  
  33.                 em = Database.createEntityManager();
  34.                 tran = em.getTransaction();
  35.  
  36.                 tran.begin();
  37.                 Unit u = em.find(Unit.class, id);
  38.                 List<Building> b= em.createQuery("SELECT b FROM Building b", Building.class).getResultList();
  39.  
  40.  
  41.  
  42.                 model.put("unit", u);
  43.                 model.put("buildings",b);
  44.                 tran.commit();
  45.             } catch (RuntimeException exc) {
  46.                 if( tran != null && tran.isActive() )
  47.                     tran.rollback();
  48.                 throw exc;
  49.             } finally {
  50.                 if (em != null)
  51.                     em.close();
  52.             }
  53.             return "link-unit-all-buildings";
  54.         }
  55.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement