2017-08-14 3 views
0

Je suis débutant en utilisant java et le printemps jpa (surtout Jhipster). Je veux créer un objet dans l'objet comme celui-ci:Comment créer un objet dans l'objet JPA de printemps (Jhipster)

enter image description here

Mais je reçois toujours comme ça:

enter image description here

propriété buildingsDTO toujours vide, voici mon code, s'il vous plaît corriger mon code ordre que je reçois comme première image.

location.java (domaine)

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() + "'" + 
     "}"; 
}} 

s'il vous plaît quelqu'un m'aider. merci.

+0

Ne pas poster de code en tant que liens vers des images. Code postal comme code. Ensuite, postez le code ** pertinent **: le chapeau a un bug. –

+0

Où est votre classe 'BuildingsDTO'? – sunkuet02

+0

Je viens d'envoyer une classe BuildingDTO. @ sunkuet02 –

Répondre

0

Par défaut jHipster marquera toutes les relations d'entités OneToMany comme @JsonIgnore afin que l'ensemble des bâtiments est pas retourné dans le JSON:

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

Si vous voulez que cela apparaisse dans le JSON alors vous devriez retirer cette annotation et marquez-la également avec une stratégie de chargement ardente pour que l'ensemble des bâtiments soit chargé comme prévu:

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

Merci, j'ai essayé votre suggestion, mais encore vide la propriété bâtiments DTO. Peut-être, avez-vous une anysuggestion. ? –

+0

@andrewMK Non ... au-delà de ça je pense que ça devrait marcher. Bien que je dois dire que je suis confus quant à la raison pour laquelle vous attendez un champ appelé buildingsDTO dans votre JSON. Il n'y a aucun champ appelé buildingDTO n'importe où. Si vous voulez éviter la confusion j'éviterais d'utiliser des DTO. Vous pouvez sélectionner cette option lorsque vous générez vos entités via jHipster. – Plog

+0

Merci beaucoup. @Plog –