简体   繁体   中英

SpringBoot Hibernate, CriteriaBuilder

Is it possible to fetch Data from 3 tables in hibernate where the controller is of a different model.

code AModel.Java

@Entity
@Table(name="demo")
//@NamedQuery(name="Demo.findAll", query="SELECT m FROM Demo m")
public class AModel implements Serializable {

    @Column(name="demo_loc")
    private String location;
    @Column(name="name")
    private String name;
    public String getLocation() {
       return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

BModel.Java
@Entity
@Table(name="location")
//@NamedQuery(name="City.findAll", query="SELECT c FROM City c")
public class BModel implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="locationNoid")
    private int locationNoId;

    @Column(name="Code")
    private int Code;
    public int getLocationNoId() {
        return locationNoId;
    }
    public void setLocationNoId(int locationNoId) {
        this.locationNoId = locationNoId;
    }
    public int getCode() {
        return Code;
    }
    public void setCode(int code) {
        Code = code;
    }
}



CModel.java
@Entity
@Table(name="offers")
public class CModel implements Serializable {
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private int OfferShopid;

        @Column(name="offerCount")
        private int offerCount;

        @Column(name="foodCount")
        private int foodCount;

        public int getOfferShopid() {
        return OfferShopid;
        }
        public void setOfferShopid(int offerShopid) {
            OfferShopid = offerShopid;
        }
        public int getOfferCount() {
            return offerCount;
        }
        public void setOfferCount(int offerCount) {
            this.offerCount = offerCount;
        }
        public int getFoodCount() {
            return foodCount;
        }
        public void setFoodCount(int foodCount) {
            this.foodCount = foodCount;
        }


}




DemoDTO.java

public class LocationDTO {

    private int demoId;
    private String name;
    private int Code;
    private int foodCount;


    public int getDemoId() {
        return demoId;
    }
    public void setDemoId(int demoId) {
        this.demoId = demoId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getCode() {
        return Code;
    }
    public void setCode(int code) {
        Code = code;
    }
    public int getFoodCount() {
        return foodCount;
    }
    public void setFoodCount(int foodCount) {
    this.foodCount = foodCount;
    }
}


DemoController.java
@RestController
@RequestMapping("/api/abcService")
@Api(description = "REST API to list details")

public class DemoController {
@Autowired
private DemoService DemoService;

@RequestMapping(value = "/list/v1/{user_id}/uid/{locationNoId}/location", 
method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "Get all Lists", notes = "Get all Address related 
information")
public ResponseEntity<?> getAll(@PathVariable("user_id") int userId, 
@PathVariable("locationNoId") int locationNoId)
{
    try {
        ModelMapper modelMapper = new ModelMapper();
        Type listType = new TypeToken<List<DemoDTO>>() {
        }.getType();
        List<DemoDTO> listAll=DemoService.ListAll(userId,locationNoId);
        return new ResponseEntity<>(listAll, HttpStatus.OK);
        }catch (Exception ex){
        String errorMessage;
        errorMessage = ex.getMessage();
        return new ResponseEntity<>(errorMessage, HttpStatus.BAD_REQUEST);
    }

}
}

DemoDAOImpl

@Component
@Repository
@Transactional
public class DemoDAOImpl implements DemoDAO {

@PersistenceContext
private EntityManager entityManager; 

@Autowired
private AService aService;

AModel aModel;


@Override
public @ResponseBody List<DemoDTO> ListAll(int User_id, int locationNoId) {
    // TODO Auto-generated method stub



      List<DemoDTO> listDemo=new ArrayList<>();

      CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
      CriteriaQuery<AModel> query =
     criteriaBuilder.createQuery(AModel.class); Root<AModel> root =
      query.from(AModel.class); query.select(root); CriteriaQuery<AModel>
      select = query.select(root); TypedQuery<AModel> typedQuery =
      entityManager.createQuery(select); List<AModel> AllLists =
      typedQuery.getResultList();

      for(AModel listofAll :AllLists ) {
      System.out.println(listofAll.getid());
      System.out.println(listofAll.getname());
      System.out.println(listofAll.getlocation());
      System.out.println(listofAll.getid());
      System.out.println(listofAll.getRating()); 
      listMalls.(AllLists); 

      return listMalls;

}

Base DemoController you have already a DemoService that you can reuse between other controlers. So if you have

@RestController
@RequestMapping("/api/abcServiceA")
public class DemoControllerA {
    @Autowired
    private DemoServiceA demoServiceA;

@RestController
@RequestMapping("/api/abcServiceB")
public class DemoControllerB {
    @Autowired
    private DemoServiceB demoServiceB;

You can create controler:

@RestController
@RequestMapping("/api/abcServiceB")
public class DemoControllerD {
    @Autowired
    private DemoServiceA demoServiceA;
    @Autowired
    private DemoServiceB demoServiceB;

Use this two services in one method:

@RequestMapping(value = "/list/v1/{user_id}/uid/{locationNoId}/location",method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "Get all Lists", notes = "Get all Address related information")
        public ResponseEntity<?>getAll(@PathVariable("user_id")int userId,
        @PathVariable("locationNoId")int locationNoId) {
    try {
        ModelMapper modelMapper = new ModelMapper();
        Type listType = new TypeToken<List<DemoDTO>>() {}.getType();
        List<DemoDTO> listAllFromA = aemoServiceA.ListAll(userId, locationNoId);
        List<DemoDTO> listAllFromB = aemoServiceB.ListAll(userId, locationNoId);
        // do something with listAllFromA and listAllFromB
        return new ResponseEntity<>(listAll, HttpStatus.OK);
    } catch (Exception ex) {
        String errorMessage;
        errorMessage = ex.getMessage();
        return new ResponseEntity<>(errorMessage, HttpStatus.BAD_REQUEST);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM