DTO 引用 Class 未记录,创建后 'null'
DTO Reference Class not recorded, 'null' after create
我有 DTO class 调用 RoomDetailDto 如下
public class RoomDetailDto {
String ID;
@NotEmpty
String cd;
@NotEmpty
String name;
String dscp;
RoomCategoryDto roomCategoryDto;
//setter and getter
roomCategoryDto 引用了另一个名为 RoomCategoryDto 的 DTO,如下所示:
public class RoomCategoryDto {
String ID;
@NotEmpty
@Length(min = 0, max = 5)
String cd;
@NotEmpty
String name;
@NotEmpty
@NumberValue
String cost;
String dscp;
//setter and getter
这个JSON我发给服务器
{
"cd":"dfs23",
"name":"df223",
"dscp":"sfdsfs",
"roomCategoryDto":{
"cd":"KLS2",
"name":"Kelas 2",
"cost":"200000",
"dscp":null,
"id":"7f554d7a-3c5d-4c15-921d-fa4b4c629e6c"
}
}
发送到服务器后,该记录已成功插入,但我的 roomCategoryDto 是 'NULL'
{
"cd": "dfs23",
"name": "df223",
"dscp": "sfdsfs",
"roomCategoryDto": null,
"id": "ed22b38d-2bc3-4c80-b035-8936318c90c7"
}
有人可以指教吗?
请找到我的控制器如下:
@RequestMapping(value = "/updateAction", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public RoomDetailDto update(@Validated @RequestBody RoomDetailDto data) throws ParseException, RecordNotFoundException, java.text.ParseException {
logger.info("add");
RoomDetailEntity add = roomDetailService.updateRoomDetail(convertToEntity(data));
return convertToDto(add);
}
我的实体如下
@Entity(name = "RoomCategoryEntity") @Table(name = "t_room_category") public class RoomCategoryEntity implements Serializable {
public static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "ID", unique = true)
private String ID;
@Column(name = "cd")
private String cd;
@Column(name = "name")
private String name;
@Column(name = "cost")
private String cost;
@Column(name = "dscp")
private String dscp;
@Column(name = "createDate")
private Date createDate;
@Column(name = "updateDate")
private Date updateDate;
@Column(name = "isDel")
private Date isDel; //setter and getter}
============================================= =====================
@Entity(name = "RoomDetailEntity") @Table(name = "t_room_details") public class RoomDetailEntity implements Serializable {
public static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "ID", unique = true)
private String ID;
@Column(name = "cd")
private String cd;
@Column(name = "name")
private String name;
@Column(name = "dscp")
private String dscp;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "room_category_id", referencedColumnName = "ID")
private RoomCategoryEntity roomCategoryEntity;
@Column(name = "createDate")
private Date createDate;
@Column(name = "updateDate")
private Date updateDate;
@Column(name = "isDel")
private Date isDel;
我刚刚尝试将引用 table 更改为实体,并成功插入
public class RoomDetailDto {
String ID;
@NotEmpty
String cd;
@NotEmpty
String name;
String dscp;
// I change below line
RoomCategoryEntity roomCategoryEntity;
[
{
"cd": "df23",
"name": "df23",
"dscp": "sfdsfs",
"roomCategoryEntity": {
"cd": "KLS2",
"name": "Kelas 2",
"cost": "200000",
"dscp": null,
"createDate": 1497535268000,
"updateDate": null,
"isDel": null,
"id": "7f554d7a-3c5d-4c15-921d-fa4b4c629e6c"
},
"id": "02c28356-9783-46a7-8ac5-9e00497d9be2"
}
]
我有 DTO class 调用 RoomDetailDto 如下
public class RoomDetailDto {
String ID;
@NotEmpty
String cd;
@NotEmpty
String name;
String dscp;
RoomCategoryDto roomCategoryDto;
//setter and getter
roomCategoryDto 引用了另一个名为 RoomCategoryDto 的 DTO,如下所示:
public class RoomCategoryDto {
String ID;
@NotEmpty
@Length(min = 0, max = 5)
String cd;
@NotEmpty
String name;
@NotEmpty
@NumberValue
String cost;
String dscp;
//setter and getter
这个JSON我发给服务器
{ "cd":"dfs23", "name":"df223", "dscp":"sfdsfs", "roomCategoryDto":{ "cd":"KLS2", "name":"Kelas 2", "cost":"200000", "dscp":null, "id":"7f554d7a-3c5d-4c15-921d-fa4b4c629e6c" } }
发送到服务器后,该记录已成功插入,但我的 roomCategoryDto 是 'NULL'
{ "cd": "dfs23", "name": "df223", "dscp": "sfdsfs", "roomCategoryDto": null, "id": "ed22b38d-2bc3-4c80-b035-8936318c90c7" }
有人可以指教吗? 请找到我的控制器如下:
@RequestMapping(value = "/updateAction", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public RoomDetailDto update(@Validated @RequestBody RoomDetailDto data) throws ParseException, RecordNotFoundException, java.text.ParseException {
logger.info("add");
RoomDetailEntity add = roomDetailService.updateRoomDetail(convertToEntity(data));
return convertToDto(add);
}
我的实体如下
@Entity(name = "RoomCategoryEntity") @Table(name = "t_room_category") public class RoomCategoryEntity implements Serializable {
public static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "ID", unique = true)
private String ID;
@Column(name = "cd")
private String cd;
@Column(name = "name")
private String name;
@Column(name = "cost")
private String cost;
@Column(name = "dscp")
private String dscp;
@Column(name = "createDate")
private Date createDate;
@Column(name = "updateDate")
private Date updateDate;
@Column(name = "isDel")
private Date isDel; //setter and getter}
============================================= =====================
@Entity(name = "RoomDetailEntity") @Table(name = "t_room_details") public class RoomDetailEntity implements Serializable {
public static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "ID", unique = true)
private String ID;
@Column(name = "cd")
private String cd;
@Column(name = "name")
private String name;
@Column(name = "dscp")
private String dscp;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "room_category_id", referencedColumnName = "ID")
private RoomCategoryEntity roomCategoryEntity;
@Column(name = "createDate")
private Date createDate;
@Column(name = "updateDate")
private Date updateDate;
@Column(name = "isDel")
private Date isDel;
我刚刚尝试将引用 table 更改为实体,并成功插入
public class RoomDetailDto {
String ID;
@NotEmpty
String cd;
@NotEmpty
String name;
String dscp;
// I change below line
RoomCategoryEntity roomCategoryEntity;
[ { "cd": "df23", "name": "df23", "dscp": "sfdsfs", "roomCategoryEntity": { "cd": "KLS2", "name": "Kelas 2", "cost": "200000", "dscp": null, "createDate": 1497535268000, "updateDate": null, "isDel": null, "id": "7f554d7a-3c5d-4c15-921d-fa4b4c629e6c" }, "id": "02c28356-9783-46a7-8ac5-9e00497d9be2" } ]