问题
when i fine a Customer by any of his fields, everything goes well, i get the JSON with the global object returned.
i am writing a code to fine a Customer By his lastName. The Customer Entity has and object in wich the field lastName is declared. So i want my Endpoint to return the Customer Global Object as in the first case.
I have into my postman Status 200 OK, but with and empty body. Any solution ? THANKS
here is a sample. The lastName field is declared both into the prefBillAddressUid and prefShipAddressUid objects
@Entity
@Table(name = "tcustomer")
public class Customer implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "pk_sequence",sequenceName = "sequence_tcustomer",initialValue = 1000, allocationSize = 100)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pk_sequence")
private Long uidpk;
@Column(name = "user_id", nullable=false)
private String userId;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "pref_bill_address_uid")
private CustomerAddress prefBillAddressUid;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "pref_ship_address_uid")
private CustomerAddress prefShipAddressUid;
...
//getters and setters
//constructors
}
My repository
@Query(value = "SELECT c FROM CustomerAddress c WHERE c.lastName = :lastName")
CustomerAddress findByLastName( @Param("lastName") String lastName);
Implementation of Service
@Override
public CustomerAddressDto findByLastName(String lastName) {
CustomerAddress result = customerRepository.findByLastName(lastName);
return customerAddressMapper.customerAddressToCustomerAddressDto(result);
}
here is my ressource
@GetMapping(SEARCH_CUSTOMER_LAST_NAME_ENDPOINT)
@ResponseStatus(value = HttpStatus.OK)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK", response = CustomerAddressDto.class),
@ApiResponse(code = 500, message = "Unexpected error", response = CustomerAddressDto.class)
})
@Timed
public ResponseEntity getCustomerByLastName ( @PathVariable String lastName) throws URISyntaxException {
if (log.isDebugEnabled()){
log.debug("[CustomerResource] GET {} : Retrieving Customer ({})", SEARCH_CUSTOMER_LAST_NAME_ENDPOINT, lastName);
}
CustomerAddressDto customerAddressDto = customerService.findByLastName(lastName);
return Optional.ofNullable(customerAddressDto)
.map(result->{
if (log.isDebugEnabled()){
log.debug("[CustomerResource] Customer ({}) retrieved", result.getLastName());
}
return new ResponseEntity<>(HttpStatus.OK);
})
.orElse(new ResponseEntity(new ResponseError(HttpStatus.NOT_FOUND.getReasonPhrase(),
"The Customer with lastName " + lastName + " does not exists"), null,HttpStatus.NOT_FOUND)
);
}
customerAddress Class
@Entity
@Table(name="taddress")
public class CustomerAddress implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "pk_sequence",sequenceName = "sequence_taddress",initialValue = 1000, allocationSize = 100)
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "pk_sequence")
private Long uidpk;
@Column(name = "last_name")
private String lastName;
@Column(name = "first_name")
private String firstName;
@Column(name = "phone_number")
private String phoneNumber;
@Column(name= "fax_number")
private String faxNumber;
@Column(name = "street_1")
private String street1;
@Column(name= "street_2")
private String street2;
@Column(name= "city")
private String city;
@Column(name= "sub_country")
private String subCountry;
@Column(name= "zip_postal_code")
private String zipPostalCode;
@Column(name= "country")
private String country;
@Column(name = "commercial")
private Boolean commercial;
@Column(name = "guid", nullable=false)
private String guid;
@Column(name = "customer_uid")
private Long customerUid;
@Column(name= "type")
private String type;
@Column(name = "organization")
private String organization;
...
//getters ans setters
}
回答1:
Firstly, You cannot return CustomerAddress from CustomerRepository.
Some of the alternatives are
To retrieve CustomerAddress based on last name from
CustomerRepository, you can do something like thisCustomer findByPrefBillAddressUid_LastName(String lastName);
spring-data will formulate the query, You don't need @Query. But notice that the return type if Customer and not CustomerAddress. So you will get a customer object with the correct customerAddress.
If you want to return CustomerAddress, then you need to create CustomerAddressRepository and write a method like this
CustomerAddress findByLastName(String lastName);Use Projections Create a interface like this
interface CustomerShort { CustomerAddress getPrefBillAddressUid(); }and then your CustomerRepository needs to have a method like this
interface CustomerRepository extends CRUDRepository<Customer, UUID> { Collection<CustomerShort> findByPrefBillAddressUid_LastName(String lastName); }
来源:https://stackoverflow.com/questions/48426102/how-can-i-retrieve-the-list-of-object-associated-as-field-into-an-entity-class