2016-06-01 3 views
0

в моей текущей реализации с использованием Spring-Boot, -HATEOAS, -Rest-Data. Я пытаюсь сэкономить некоторые дополнительные вызовы отдыха и увеличить ресурс отдыха для кредитов, чтобы также предоставлять отношения кредита (см. ниже учетную запись как ManyToOne и creditBookingClassPayments как OneToMany). Проблема в том, что я не могу запустить ее. При вызове всегда выполняются пустые отношения. Я действительно был бы признателен за помощь в этом.Усовершенствованный Spring Data Rest поставляет пустые отношения

Вот окружение:

Credit.java

@Entity 
@Getter 
@Setter 
public class Credit { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Setter(NONE) 
    @Column(name = "id") 
    private Long itemId; 

    @ManyToOne(fetch=FetchType.LAZY) 
    @JoinColumn(name="account_id", nullable = false) 
    private Account account; 

    @OneToMany(mappedBy = "credit") 
    private List<CreditBookingClassPayment> creditBookingClassPayments = new ArrayList<>(); 

    @NotNull(message="Please enter a valid short name.") 
    @Column(length = 10, nullable = false) 
    private String shortName; 

    @NotNull(message="Please enter a valid name.") 
    @Column(nullable = false) 
    private String name; 

    ... 
} 

CreditRepositoryCustomImpl.java

использует QueryDsl для повышения кредитных ресурсов с realation

...  
    @Override 
    public List<Credit> findDistinctByAccountItemIdNew(Long accountId) { 
     QCredit credit = QCredit.credit; 
     QAccount account = QAccount.account; 
     QCreditBookingClassPayment creditBookingClassPayment = QCreditBookingClassPayment.creditBookingClassPayment; 
     QBookingClass bookingClass = QBookingClass.bookingClass; 

     BooleanExpression hasAccountItemId = credit.account.itemId.eq(accountId); 
     List<Credit> credits = from(credit).where(hasAccountItemId) 
       .innerJoin(credit.account, account) 
       .leftJoin(credit.creditBookingClassPayments, creditBookingClassPayment) 
       .leftJoin(creditBookingClassPayment.bookingClass, bookingClass).groupBy(credit.itemId).fetch(); 
     return credits; 
    } 
... 

CreditController.java

смотрит в responseBody здесь все (счета и кредитные платежи) предоставляется по кредитам

@RepositoryRestController 
public class CreditController { 

    @Autowired 
    private CreditRepository creditRepository; 

    @RequestMapping(value = "/credit/search/findAllByAccountItemIdNew", method= RequestMethod.GET, produces = MediaTypes.HAL_JSON_VALUE) 
    @ResponseBody 
    public ResponseEntity<Resources<PersistentEntityResource>> findAllByAccountItemIdNew(@RequestParam Long accountId, PersistentEntityResourceAssembler persistentEntityResourceAssembler) { 
     List<Credit> credits = creditRepository.findDistinctByAccountItemIdNew(accountId); 
     Resources<PersistentEntityResource> responseBody = new Resources<PersistentEntityResource>(credits.stream() 
       .map(persistentEntityResourceAssembler::toResource) 
       .collect(Collectors.toList())); 
     return ResponseEntity.ok(responseBody); 
    } 

} 

CreditResourceIntegrTest.java

здесь creditResourcesEntity держать кредит, но счет null и creditBookingClassPayment - пустой массив

@Test 
public void testFindAllByAccountItemId() throws URISyntaxException { 
    URIBuilder builder = new URIBuilder(creditFindAllByAccountItemIdRestUrl); 
    builder.addParameter("accountId", String.valueOf(EXPECTED_ACCOUNT_ID)); 
    builder.addParameter("projection", "base"); 

    RequestEntity<Void> request = RequestEntity.get(builder.build()) 
      .accept(MediaTypes.HAL_JSON).acceptCharset(Charset.forName("UTF-8")).build(); 
    ResponseEntity<Resources<Resource<Credit>>> creditResourcesEntity = 
      restTemplate.exchange(request, new ParameterizedTypeReference<Resources<Resource<Credit>>>() {}); 

    assertEquals(HttpStatus.OK, creditResourcesEntity.getStatusCode()); 
    //assertEquals(EXPECTED_CREDIT_COUNT, creditResourcesEntity.getBody().getContent().size()); 
} 

Пропустить что-нибудь?

Благодарим за помощь! Karsten

ответ

0

Хорошо, PersistentEntityResourceAssembler не поддерживает отношения. Но это может быть связано с использованием прогнозов.

CreditProjection.java

@Projection(name = "base" , types = Credit.class) 
public interface CreditProjection { 

    String getShortName(); 
    String getName(); 
    List<CreditBookingClassPaymentProjection> getCreditBookingClassPayments(); 
    BigDecimal getValue(); 
    BigDecimal getInterestRate(); 
    BigDecimal getMonthlyRate(); 

} 

CreditBookingClassPaymentProjection.java

@Projection(name = "base" , types = CreditBookingClassPayment.class) 
public interface CreditBookingClassPaymentProjection { 

    BookingClass getBookingClass(); 
    CreditPaymentType getCreditPaymentType(); 

} 

CreditController.java

@RepositoryRestController 
public class CreditController { 

    @Autowired 
    private ProjectionFactory projectionFactory; 

    @Autowired 
    private CreditRepository creditRepository; 

    @RequestMapping(value = "/credit/search/findAllByAccountItemIdNew", method = RequestMethod.GET, produces = MediaTypes.HAL_JSON_VALUE) 
    @ResponseBody 
    public ResponseEntity<Resources<?>> findAllByAccountItemIdNew(@RequestParam Long accountId, 
      PersistentEntityResourceAssembler persistentEntityResourceAssembler) { 
     List<Credit> credits = creditRepository.findDistinctByAccountItemIdNew(accountId); 

     List<PersistentEntityResource> creditResources = new ArrayList<>(); 
     for (Credit credit : credits) { 
      // credit.getCreditBookingClassPayments() 
      PersistentEntityResource creditResource = persistentEntityResourceAssembler.toResource(credit); 
      creditResources.add(creditResource); 
     } 

     Resources<CreditProjection> responseBody = new Resources<CreditProjection>(credits.stream() 
       .map(credit -> projectionFactory.createProjection(CreditProjection.class, credit)) 
       .collect(Collectors.toList())); 

     return ResponseEntity.ok(responseBody); 
    } 

} 

 Смежные вопросы

  • Нет связанных вопросов^_^