Java Spring: Post method does not insert relationship id

Asked

Viewed 76 times

0

I’m learning the Spring framework, I tried to follow some 1-1 relationship tutorials where a library has an address. When uploading library data and address id in the body, a new record is inserted in the library table, but the address_id that references an address is not entered, even passing the request payload.

My model Library:

@Entity
@Table(name = "Bibliotecas")
public class Library implements Serializable {
  private static final long serialVersionUID = 1L;

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

  private String name;

  @OneToOne
  @JoinColumn(name = "address_id", referencedColumnName = "id")
  private Address address;

  public Library() {
  }

  public long getId() {
    return this.id;
  }

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

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Address getAddress() {
    return this.address;
  }

  public void setAddress(Address address) {
    this.address = address;
  }

}

My model Address:

@Entity
public class Address {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long id;

  @Column(nullable = false)
  private String location;

  public long getId() {
    return this.id;
  }

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

  public String getLocation() {
    return this.location;
  }

  public void setLocation(String location) {
    this.location = location;
  }

  @OneToOne(mappedBy = "address", fetch = FetchType.LAZY, optional = false)
  private Library library;

}

Mine repositories:

public interface LibraryRepository extends JpaRepository<Library, Long> {}
public interface AddressRepository extends JpaRepository<Address, Long> {}

My library Resource:

@RestController
@RequestMapping(value = "/api")
public class LibraryResource {

  @Autowired
  LibraryRepository libraryRepository;

  @GetMapping("/libraries")
  public List<Library> listaBibliotecas() {
    return libraryRepository.findAll();
  }

  @PostMapping("/library")
  public Library salvaBiblioteca(@RequestBody Library library) {
    return libraryRepository.save(library);
  }
}

And then I pass on my request:

{
    "name": "Biblioteca test",
    "address_id": 1
}

Note: There is an address with id 1, I get return:

{
    "id": 5,
    "name": "Biblioteca test",
    "address": null
}

In my return address is null, and when I give a select in the database, the created record has address_id null. How can I correct the link between these two models? Because this is happening?

1 answer

2


Your request should be so.

I set an example here at Github based on their classes.

{
    "name": "Biblioteca test",
    "address": {
        "id":"1" 
    }
}

Response

inserir a descrição da imagem aqui

  • It worked that way, @rafaelchagasb, thank you. Can you tell me why Location returns null even when the related record has data in the Location column?

  • 1

    save will not return the object of your relationship in a managed way. It will return the data you send to it. In this case here, it will try to make the relationship with the id 1 Adress and will return the object with the other properties you sent. { "name": "Library test", "address": { "id":"1", "Location": "object" } } .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.