Skip to content
Advertisement

Spring boot JPA save manytomany relationship

I am trying to store a manytomany relationship but it not stores the relationship.

The following code has generated 3 tables.

Soldier, medal and soldier_medals.

The service just make a call to a CRUD Interface with save(soldier).

It stores correctly the soldier, but it not fill any row at the soldier_medals table.

This is the JSON I send to the server:

{
        "abbreviatedSequence": "XDF",
        "medals": [
            {
                "name": "Purple",
                "id": 1
            },
            {
                "name": "Red",
                "id": 2
            }
        ],
        "sequence": "XDFREE",
        "name": "Savier"
    }

Employee.java

@Entity(name="employees")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class Employee {
    @Id
    @GeneratedValue(generator="increment")
    @GenericGenerator(name="increment", strategy = "increment")
    private Long id;
    
    private String name;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

Soldier.java

@Entity(name="soldier")
public class Soldier extends Employee{
    
    private String sequence;
    
    private String abbreviatedSequence;
    
    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "soldiers")
    private List<Medal> medals;
    
    public List<Medal> getMedals() {
        return medals;
    }
    
    public void setMedals(List<Medal> medals) {
        this.medals = medals;
    }
    
    public String getSequence() {
        return sequence;
    }

    public void setSequence(String sequence) {
        this.sequence = sequence;
    }

    public String getAbbreviatedSequence() {
        return abbreviatedSequence;
    }

    public void setAbbreviatedSequence(String abbreviatedSequence) {
        this.abbreviatedSequence = abbreviatedSequence;
    }
    
}

Medal.java

@Entity
@Table(name = "medal")
public class Medal {
    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "medal_seq")
    @SequenceGenerator(name = "medal_seq", sequenceName = "medal_seq", allocationSize = 1)
    private Long id;

    @Column(name = "NAME", length = 50, unique = true)
    @NotNull
    @Size(min = 3, max = 50)
    private String name;
    
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "soldier_medals", joinColumns = {
            @JoinColumn(name = "medal_id", nullable = false, updatable = false) },
            inverseJoinColumns = { @JoinColumn(name = "soldier_id",
                    nullable = false, updatable = false) })
    private List<Soldier> soldiers;

    
    public List<Soldier> getSoldiers() {
        return soldiers;
    }
    
    public void setSoldiers(List<Soldier> soldiers) {
        this.soldiers = soldiers;
    }
    

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

SoldierController.java

@RestController
public class SoldierController {
    @Autowired
    private SoldierService soldierService;
    
    @RequestMapping(value="/api/contextsource/soldier",method= RequestMethod.POST)
    public Soldier addSoldier(@RequestBody Soldier soldier) {       
        return soldierService.addSoldier(soldier);
    }
    
}

Advertisement

Answer

I was persisting the entity with in the wrong order. This is how I solved it.

@RequestMapping(value="/api/contextsource/soldier",method= RequestMethod.POST)
    public Soldier addPeptide(@RequestBody Soldier soldier) {       
        for(Medal s: soldier.getMedals()) {
            Medal ss = medalService.getMedalById(s.getId());
            ss.getMedals().add(soldier);
            medalService.addMedal(ss);
        }           
        return null;
    }
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement