Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. @GetMapping("/api/link-unit-all-buildings")
  2. public UnitBuildingsDto getAllBuildings( @RequestParam("id") long id ) {
  3.    
  4.     UnitBuildingsDto unit_building_dto;
  5.    
  6.     EntityManager em = null;
  7.     EntityTransaction tran = null;
  8.    
  9.     try {
  10.        
  11.         em = Database.createEntityManager();
  12.         tran = em.getTransaction();
  13.        
  14.         tran.begin();
  15.        
  16.             List< BuildingDto > buildings = em.createQuery("SELECT b FROM Building b", Building.class)
  17.                                               .getResultList()
  18.                                               .stream()
  19.                                               .map(BuildingDto::make)
  20.                                               .collect(Collectors.toList());
  21.        
  22.         tran.commit();
  23.        
  24.         Unit u = em.find(Unit.class, id);
  25.         if(u == null)
  26.             throw new NotFoundException("link-unit-all-buildings");
  27.        
  28.         unit_building_dto.setUnit( UnitDto.make( u ) );
  29.         unit_building_dto.setBuildings( buildings );
  30.        
  31.     } catch (RuntimeException exc) {
  32.         if (tran != null && tran.isActive())
  33.             tran.rollback();
  34.  
  35.         throw exc;
  36.     } finally {
  37.         if (em != null)
  38.             em.close();
  39.     }
  40.  
  41.     return unit_building_dto;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement