1

this is my region class :

    @Entity(name = "REGION")

public class Region {

    private Long id;
    private String region;

    @OneToMany(targetEntity = Compagnie.class, mappedBy = "region", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private Set<Compagnie> compagnie;

this is my Compagnie class :

@Entity(name = "COMPAGNIE")
public class Compagnie {

    private Long id;
    private String compagnie;

    @ManyToOne
    @JoinColumn(name = "REGION_ID")
    private Region region;

this is my edit method from controller class :

@RequestMapping(value = "/compagnie/{id}", method = RequestMethod.GET)
public ResponseEntity<?> getCompagnie(@PathVariable("id") long id) {
    logger.info("Fetching Compagnie with id {}", id);
    Compagnie compagnie = compagnieRepository.findOne(id);
    if (compagnie == null) {
        logger.error("Compagnie with id {} not found.", id);
        return new ResponseEntity(new CustomErrorType("Unable to update. Compagnie with id " + id + " not found."),
                HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<Compagnie>(compagnie, HttpStatus.OK);
}

the json that i use for test is :

{ "compagnie": "test compagnie 1", "region": { "id": 1, "region": "region 1" } }

the compagnie change but the region wont and i cant be able to read the region attributes from the @requestBody .

i really need your help . thanks .

youssef
  • 91
  • 1
  • 7
  • I think you should get the Object graph automatically since you've an object relationship. Please see : https://stackoverflow.com/questions/31943989/spring-data-jpa-and-namedentitygraphs – PAA Oct 23 '17 at 12:44

1 Answers1

0

The Region is not fetched in that case by default by your persistence provider.

Define entity graph details on the entity:

@Entity(name = "COMPAGNIE")
@NamedEntityGraph(name = "Compagnie.region",
  attributeNodes = @NamedAttributeNode("region"))
public class Compagnie {

Then override the definition of the findOne method in your jpa repository interface:

public interface CompagnieRepository extends JpaRepository<Compagnie, String> {

    @EntityGraph("Compagnie.region")
    Compagnie findOne(Long id);
}

Now the dependency will be fetched eagerly and region data should be present in the response body.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63