【问题标题】:Creating a @ManyToOne object using json使用 json 创建 @ManyToOne 对象
【发布时间】:2019-07-17 17:43:28
【问题描述】:

我正在尝试实现多对一关系,其中站点属于区域。如何使用 REST 来实现这一点,只需在 POST 站 json 中传递区域 ID,如图所示。

{
    "name": "New York",
    "code": "MGR",
    "active": "YES",
    "regionid": 1

}

下面是我的控制器。

@PostMapping("/station/create/")
public ResponseObject create(@Valid @RequestBody Station station){
    ResponseObject responseObject = new ResponseObject();
    responseObject.setSuccess(true);
    responseObject.setData(stationDao.save(station));
    responseObject.setMessage("Station created successfully");
    return responseObject;
}

以下是我的模型。

@Entity
@Table(name = "regions")
@EntityListeners(AuditingEntityListener.class)
public class Region implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonProperty("regionID")
    private Long regionID;
    @JsonProperty("name")
    private String name;
    @JsonProperty("active")
    @Enumerated(EnumType.STRING)
    private YesNo active;
}





@Entity
@Table(name = "stations")
@EntityListeners(AuditingEntityListener.class)
public class Station implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonProperty("stationID")
    private Long stationID;

    @JsonManagedReference
    @ManyToOne
    @JsonProperty("regionID")
    @JoinColumn(name = "regionid", referencedColumnName = "regionid")
    private Region region;

    @JsonProperty("name")
    private String name;
    @JsonProperty("code")
    private String code;
    @JsonProperty("active")
    @Enumerated(EnumType.STRING)
    private YesNo active;
}

【问题讨论】:

  • 错误是什么?
  • 16,770 WARN [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter](默认任务1)无法评估Jackson反序列化类型[[简单类型,com.apas.region.model类。站]]:com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法处理托管/反向引用'defaultReference':从类型[简单类型,类com.apas.region.model.Region]中找不到反向引用属性跨度>

标签: hibernate spring-mvc jackson spring-data-jpa


【解决方案1】:
@JoinColumn(name = "regionid", referencedColumnName = "regionid")
    private Region region;

referencedColumnName 在 Regioin 类中不存在。它将是 regionID 而不是 regionid

尝试以下更改:

@JoinColumn(name = "regionid", referencedColumnName = "regionID")
        private Region region;

【讨论】:

  • 这是否允许我使用以下 json 创建一个站?
  • { "name": "New York", "code": "MGR", "active": "YES", "regionid": 1 } 它仍然抛出同样的错误
  • 你通过了 "regionid" : "1" 。 Region 是 Station 类中的对象。你可以像这样传递:{ "name": "New York", "code": "MGR", "active": "YES", "regionID" : { "regionID" : "1"}}
  • 如果发送为 { "name": "New York", "code": "MGR", "active": "YES", "regionid": 1 不创建对象 "regionID" : { "regionID" : "1"}
  • 你可以使用 JsonCreator 或 JsonSetter 来完成
【解决方案2】:

我意识到我不需要@JsonManagedReference 因此我的代码将是

    @ManyToOne
    @JsonProperty("regionID")
    @JoinColumn(name = "regionid", referencedColumnName = "regionID")
    private Region region;

然后嘭嘭…………

【讨论】:

    猜你喜欢
    • 2013-01-29
    • 2016-10-27
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    • 2023-04-03
    • 2013-03-19
    相关资源
    最近更新 更多