问题描述
当我按客户的任何字段对客户进行罚款时,一切顺利,我得到了JSON 并返回了全局对象.
when i fine a Customer by any of his fields, everything goes well, i get the JSON with the global object returned.
我正在编写代码以根据其姓氏对一个客户进行罚款.客户实体具有 lastName 字段,并且声明了该对象.因此,我希望我的端点像第一种情况一样返回客户全局对象.
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.
我已经进入邮递员状态200,但是身体空着.有什么办法吗?谢谢
I have into my postman Status 200 OK, but with and empty body. Any solution ? THANKS
这是一个示例.在prefBillAddressUid和prefShipAddressUid对象中都声明了lastName字段
@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
}
我的存储库
@Query(value = "SELECT c FROM CustomerAddress c WHERE c.lastName = :lastName")
CustomerAddress findByLastName( @Param("lastName") String lastName);
服务的实现
@Override
public CustomerAddressDto findByLastName(String lastName) {
CustomerAddress result = customerRepository.findByLastName(lastName);
return customerAddressMapper.customerAddressToCustomerAddressDto(result);
}
这是我的资源
@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类
@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
}
推荐答案
首先,您不能从CustomerRepository返回CustomerAddress.
某些替代方法是
Firstly, You cannot return CustomerAddress from CustomerRepository.
Some of the alternatives are
-
要根据
CustomerRepository
中的姓氏检索CustomerAddress,您可以执行以下操作
To retrieve CustomerAddress based on last name from
CustomerRepository
, you can do something like this
Customer findByPrefBillAddressUid_LastName(String lastName);
spring-data将构成查询,您不需要@Query
.但是请注意,返回类型为Customer
而不是CustomerAddress
.因此,您将获得一个具有正确的customerAddress的客户对象.
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.
-
如果要返回CustomerAddress,则需要创建CustomerAddressRepository并编写类似这样的方法
If you want to return CustomerAddress, then you need to create CustomerAddressRepository and write a method like this
CustomerAddress findByLastName(String lastName);
使用投影创建这样的界面
interface CustomerShort {
CustomerAddress getPrefBillAddressUid();
}
然后您的CustomerRepository需要具有这样的方法
and then your CustomerRepository needs to have a method like this
interface CustomerRepository extends CRUDRepository<Customer, UUID> {
Collection<CustomerShort> findByPrefBillAddressUid_LastName(String lastName);
}
这篇关于如何检索作为字段关联到实体类的对象列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!