我试图在spring4中使用外键关联实现延迟加载 我希望在使用客户实体进行反序列化时序列化具有id的实体。 以下是我的实体。
我正在异常
com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "customerId"
为什么会这样?我该怎么做才能实现这种情况。
public class Address {
@JsonIgnore
public Customer getCustomer() {
return customer;
}
@JsonProperty("customerId")
public void setCustomer(Customer customer) {
this.customer = customer;
}
@ManyToOne(targetEntity = Customer.class)
@JoinColumn(name="customerId", insertable=false, updatable = false)
@JsonIgnore
Customer customer;
@JsonProperty
public int getCustomerIdGen() {
return customerIdGen;
}
@JsonIgnore
public void setCustomerIdGen(int customerId) {
this.customerIdGen = customerId;
}
@JsonIgnore
@Column(name="customerId")
int customerIdGen;
String contactName;
}
答案 0 :(得分:1)
您可以添加自定义反序列化器,如下所示:
public class Address {
@JsonDeserialize(using = CustomerDeserializer.class)
@JsonProperty("customerId")
public void setCustomer(Customer customer) {
this.customer = customer;
}
...
}
这是解串器
public class CustomerDeserializer extends JsonDeserializer<Customer>{
@Override
public Customer deserialize(JsonParser jsonparser, DeserializationContext context)
throws IOException, JsonProcessingException
{
int customerId = jsonparser.getInt();
// Now create or find the way to get the customer object
Customer c = new Customer(customerId);
return c;
}
}