8

I have two models:
Class One:

import javax.persistence.*;
import java.util.Set;

@Entity
public class One {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    @OneToMany(mappedBy = "one")
    private Set<Many> manySet;

    //Constructor, Getter and Setter
}

Class Many:

import javax.persistence.*;

@Entity
public class Many {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    @ManyToOne
    @JoinColumn(name = "one_id")
    private One one;
    //Constructor, Getter and Setter
}

Repository:

import com.hotel.model.Many;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ManyRepository extends JpaRepository<Many, Long> {
}

Controller Class:

@RestController
@RequestMapping(value = "many")
public class ManyController {
    @Autowired
    private ManyRepository manyRepository;

    @GetMapping
    @ResponseBody
    public List<Many> getAllMany() {
        return manyRepository.findAll();
    }

    @PostMapping
    @ResponseBody
    public ResponseEntity createMany(@RequestBody Many many) {
        return new ResponseEntity(manyRepository.save(many), HttpStatus.CREATED);
    }
}

I created One record with id=1. But when I create a Many record with JSON data:

{
    "name": "Foo",
    "one_id": 1
}

I received Many record with one_id is null
Can I using only one request to create new Many record and assign to One record has id = 1? Do I have to use 2 request: create Many and assign to One?

rsu8
  • 491
  • 4
  • 10

2 Answers2

5

You have to update your method like so

@PostMapping
@ResponseBody
public ResponseEntity createMany(@RequestBody ManyDTO many) {
    
    One one = oneRepository(many.getOne_id()); //Get the parent Object
    
    Many newMany  = new Many(); //Create a new Many object
    newMany.setName(many.getName());
    newMany.setOne(one); // Set the parent relationship
    
    
    ...

}

Note: The above answer only explains the way to set the relationships of the entity. Proper service layer should be invoked actually.

Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
  • 1
    Thank for your solution. It worked. Do I alway have to use DTO class to resolve @ManyToOne relationship? – rsu8 Oct 22 '17 at 10:09
  • 1
    Not always. If you don't need to set complex relationships usually simple entity works well. But something similar to the above situation need a DTO because the json is somewhat different from your actual entity and its mapping. – Abdullah Khan Oct 22 '17 at 10:11
  • 1
    facing same problem where foreign key not inserted in child table after following above approach it worked perfectly – Harshal Patil Apr 30 '18 at 03:50
  • @AbdullahKhan how to do the same stuff when we flip the scenario i.e. when we are trying to create a POST method for `One entity` and in JSON object that we pass for creating a record of `One entity`, the nested object of `Many entity` passed doesn't exists in the database. So how will we approach for this problem ? – humbleCodes Apr 02 '23 at 13:46
  • for a clearer explanation to my problem you can have a look at this SO post - https://stackoverflow.com/q/75911520/14106566 – humbleCodes Apr 02 '23 at 13:46
-1

You can persist 1 record in One and multiple record in Many tables only using a single request while your are using the below repository

import com.hotel.model.One;
import org.springframework.data.jpa.repository.JpaRepository;

public interface OneRepository extends JpaRepository<One, Long> {
}

On the other hand you can persist 1 record in One and 1 record in Many tables using the JpaRepository given in your example.

Your question was not clear, whether you want to persist One/Many or retrieve One/Many.

Zenith
  • 1,037
  • 1
  • 10
  • 21