我目前正在尝试使用ModelMapper的map方法执行Entity到DTO的映射。在最深的属性映射中,映射结果为null,但应为对象。
这些是我的映射源实体(为简洁起见,省略了Loombok Getter和Setters):
public class Client implements Serializable
{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Long id;
@NotEmpty
public String name;
@NotEmpty
public String surname;
@NotEmpty
@Email
public String email;
@Column(name="creation_date", updatable=false)
@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern="yyyy-MM-dd")
public Date creationDate;
public String photo;
@OneToMany(mappedBy="client", fetch=FetchType.LAZY, cascade=CascadeType.ALL, orphanRemoval=true)
public List<Invoice> invoices;
}
public class Invoice implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
@NotEmpty
public String description;
public String comment;
@Temporal(TemporalType.DATE)
@Column(name = "creation_date", updatable = false)
@DateTimeFormat(pattern = "yyyy-MM-dd")
public Date creationDate;
@ManyToOne(fetch = FetchType.LAZY)
public Client client;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "invoice_id")
public List<InvoiceItem> items;
}
public class InvoiceItem implements Serializable
{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Long id;
public Integer amount;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="product_id")
public Product product;
}
public class Product implements Serializable
{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Long id;
public String name;
public Double prize;
@Temporal(TemporalType.DATE)
@Column(name="creation_date", updatable=false)
@DateTimeFormat(pattern="yyyy-MM-dd")
public Date creationDate;
}
这些是我的目标DTO (为简洁起见,省略了Loombok Getter和Setters):
public class ClientDTO implements Serializable
{
@JsonIgnore
public Long id;
public String name;
public String surname;
public String email;
@JsonIgnore
public Date creationDate;
public String photo;
}
public class ClientWithInvoicesDTO extends ClientDTO implements Serializable
{
@JsonManagedReference
public List<InvoiceDTO> invoices;
}
public class InvoiceDTO implements Serializable
{
@JsonIgnore
public Long id;
public String description;
public String comment;
public Date creationDate;
@JsonBackReference
public ClientDTO client;
public List<InvoiceItemDTO> items;
}
public class InvoiceItemDTO implements Serializable
{
@JsonIgnore
public Long id;
public Integer amount;
public ProductDTO productDTO;
}
public class ProductDTO implements Serializable
{
@JsonIgnore
public Long id;
public String name;
public Double prize;
@JsonIgnore
public Date creationDate;
}
在我的Controller类方法上,我正在像这样进行映射:
@RequestMapping(value = "/{id}/with-invoices", method = RequestMethod.GET)
public ResponseEntity findOneWithInvoices(@PathVariable Long id)
{
Client wantedClient = this.clientService.findOneWithInvoices(id);
ClientWithInvoicesDTO wantedClientWithInvoicesDto = this.modelMapper.map(wantedClient, ClientWithInvoicesDTO.class);
return new ResponseEntity<>(wantedClientWithInvoicesDto, HttpStatus.OK);
}
我的clientService正确返回了数据,但是JSON响应上的“产品”属性为空,并且不应为:
{
"name": "cupiditate",
"surname": "veritatis",
"email": "george.prohaska@example.com",
"photo": "ec0327a5-3de8-328c-a812-9000d6d5507d",
"invoices": [
{
"description": "Distinctio autem id vel necessitatibus unde omnis rerum. Minus maxime quos doloribus. Voluptatem amet praesentium sit magni quia molestiae. Officia aspernatur numquam ut perspiciatis a.",
"comment": "Odio enim libero tempore molestiae.",
"creationDate": "2013-02-16",
"items": [
{
"amount": 6,
"productDTO": null
},
...
有人可以帮我吗?
答案 0 :(得分:0)
您不应该在课程前放置@Entity吗?像这样:
parseInt()
答案 1 :(得分:0)
正如M. Deinum在下面的评论中所述,InvoiceItemDTO的产品属性应命名为“ product”而不是“ productDTO”。现在可以正常使用