Search code examples
javajsonspringrestdto

DTO Reference Class not recorded, 'null' after create


I have DTO class called RoomDetailDto as below

public class RoomDetailDto {

String ID;

@NotEmpty
String cd;

@NotEmpty
String name;

String dscp;


RoomCategoryDto roomCategoryDto;

//setter and getter

roomCategoryDto have reference to another DTO called RoomCategoryDto as below:

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

this JSON I send to the server

{ "cd":"dfs23", "name":"df223", "dscp":"sfdsfs", "roomCategoryDto":{ "cd":"KLS2", "name":"Kelas 2", "cost":"200000", "dscp":null, "id":"7f554d7a-3c5d-4c15-921d-fa4b4c629e6c" } }

after send to server, that record successfully inserted but my roomCategoryDto is 'NULL'

{ "cd": "dfs23", "name": "df223", "dscp": "sfdsfs", "roomCategoryDto": null, "id": "ed22b38d-2bc3-4c80-b035-8936318c90c7" }

anyone can advice? Please find my controller as below:

@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);

}

and my entity as below

@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;

Solution

  • I just tried to change reference table to entity, and successfully inserted

    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" } ]