2017-08-14 61 views
0

我初學者使用java和spring jpa(expecially Jhipster)。我想在對象創建的對象是這樣的:如何在對象中創建對象Spring JPA(Jhipster)

enter image description here

但我總是得到這樣的:

enter image description here

財產buildingsDTO總是空的,這是我的代碼,請糾正我的代碼爲了得到第一張照片。

location.java(域)

public class Location implements Serializable { 

private static final long serialVersionUID = 1L; 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private Long id; 

@NotNull 
@Column(name = "content_location", nullable = false) 
private String content_location; 

@OneToMany(mappedBy = "location") 
@JsonIgnore 
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
private Set<Building> buildings = new HashSet<>(); 

public Long getId() { 
    return id; 
} 

public void setId(Long id) { 
    this.id = id; 
} 

public String getContent_location() { 
    return content_location; 
} 

public Location content_location(String content_location) { 
    this.content_location = content_location; 
    return this; 
} 

public void setContent_location(String content_location) { 
    this.content_location = content_location; 
} 

public Set<Building> getBuildings() { 
    return buildings; 
} 

public Location buildings(Set<Building> buildings) { 
    this.buildings = buildings; 
    return this; 
} 

public Location addBuilding(Building building) { 
    this.buildings.add(building); 
    building.setLocation(this); 
    return this; 
} 

public Location removeBuilding(Building building) { 
    this.buildings.remove(building); 
    building.setLocation(null); 
    return this; 
} 

public void setBuildings(Set<Building> buildings) { 
    this.buildings = buildings; 
} 

@Override 
public boolean equals(Object o) { 
    if (this == o) { 
     return true; 
    } 
    if (o == null || getClass() != o.getClass()) { 
     return false; 
    } 
    Location location = (Location) o; 
    if (location.getId() == null || getId() == null) { 
     return false; 
    } 
    return Objects.equals(getId(), location.getId()); 
} 

@Override 
public int hashCode() { 
    return Objects.hashCode(getId()); 
} 

@Override 
public String toString() { 
    return "Location{" + 
     "id=" + getId() + 
     ", content_location='" + getContent_location() + "'" + 
     "}"; 
}} 

locationDTO.java

public class LocationDTO implements Serializable { 

private Long id; 

@NotNull 
private String content_location; 

private Set<BuildingDTO> buildings = new HashSet<>(); 

public Set<BuildingDTO> getBuildingsDTO() { 
    return buildings; 
} 

public void setBuildingsDTO(Set<BuildingDTO> buildings) { 
    this.buildings = buildings; 
} 

public Long getId() { 
    return id; 
} 

public void setId(Long id) { 
    this.id = id; 
} 

public String getContent_location() { 
    return content_location; 
} 

public void setContent_location(String content_location) { 
    this.content_location = content_location; 
} 

@Override 
public boolean equals(Object o) { 
    if (this == o) { 
     return true; 
    } 
    if (o == null || getClass() != o.getClass()) { 
     return false; 
    } 

    LocationDTO locationDTO = (LocationDTO) o; 
    if(locationDTO.getId() == null || getId() == null) { 
     return false; 
    } 
    return Objects.equals(getId(), locationDTO.getId()); 
} 

@Override 
public int hashCode() { 
    return Objects.hashCode(getId()); 
} 

@Override 
public String toString() { 
    return "LocationDTO{" + 
     "id=" + getId() + 
     ", content_location='" + getContent_location() + "'" + 
     "}"; 
}} 

locationMapper.java

public interface LocationMapper extends EntityMapper <LocationDTO, Location> { 

@Mapping(target = "buildings", ignore = true) 
Location toEntity(LocationDTO locationDTO); 
default Location fromId(Long id) { 
    if (id == null) { 
     return null; 
    } 
    Location location = new Location(); 
    location.setId(id); 
    return location; 
}} 

buildingDTO.java

public class BuildingDTO implements Serializable { 

private Long id; 

@NotNull 
private String content_building; 

private Long locationId; 

public Long getId() { 
    return id; 
} 

public void setId(Long id) { 
    this.id = id; 
} 

public String getContent_building() { 
    return content_building; 
} 

public void setContent_building(String content_building) { 
    this.content_building = content_building; 
} 

public Long getLocationId() { 
    return locationId; 
} 

public void setLocationId(Long locationId) { 
    this.locationId = locationId; 
} 

@Override 
public boolean equals(Object o) { 
    if (this == o) { 
     return true; 
    } 
    if (o == null || getClass() != o.getClass()) { 
     return false; 
    } 

    BuildingDTO buildingDTO = (BuildingDTO) o; 
    if(buildingDTO.getId() == null || getId() == null) { 
     return false; 
    } 
    return Objects.equals(getId(), buildingDTO.getId()); 
} 

@Override 
public int hashCode() { 
    return Objects.hashCode(getId()); 
} 

@Override 
public String toString() { 
    return "BuildingDTO{" + 
     "id=" + getId() + 
     ", content_building='" + getContent_building() + "'" + 
     "}"; 
}} 

請任何人幫助我。 謝謝。

+0

請勿將代碼作爲鏈接發佈到圖像。以代碼形式發佈代碼。然後發佈**相關**代碼:一個帽子有一個錯誤。 –

+0

您的「BuildingsDTO」課程在哪裏? – sunkuet02

+0

我剛剛發送了BuildingDTO類。 @ sunkuet02 –

回答

0

默認jHipster將迎來任何一對多的實體關係作爲@JsonIgnore,這樣設置的建築物沒有在JSON返回:

@OneToMany(mappedBy = "location") 
@JsonIgnore 
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
private Set<Building> buildings = new HashSet<>(); 

如果你想這在JSON顯示出來,那麼你應該刪除該註釋並用一個熱切的加載策略標記它,以便按照您的期望加載該組建築物:

@OneToMany(mappedBy = "location", fetch = FetchType.EAGER) 
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
private Set<Building> buildings = new HashSet<>(); 
+0

謝謝,我已經嘗試了您的建議,但仍然是空的建築物DTO。也許,你有沒有勸誡。 。? –

+0

@andrewMK否..除此之外,我認爲它應該工作。雖然我不得不說我爲什麼會期望在JSON中使用一個名爲buildingsDTO的字段,但我感到困惑。沒有任何地方叫做buildingDTO。如果你想避免混淆,我會避免使用DTO。當您通過jHipster生成實體時,您可以選擇此選項作爲選項。 – Plog

+0

非常感謝。 @Plog –