Home > OS >  With jpa, many side of a one-to-many relationship is not getting persisted
With jpa, many side of a one-to-many relationship is not getting persisted

Time:11-09

I am having two entities having one-to-many & many-to-one relationship. One side:

@Entity
@Table(name = "GAME_BLIND_STRUCTURE")
@Builder
@Getter
@Setter
public class GameBlindStructureEntity implements Serializable {
   private static final long serialVersionUID = -7800120016594245121L;
   @Id
   @Column(name = "BLIND_ID")
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long blindId;
        
   @Column(name = "BLIND_STRUCTURE_NAME", unique = true)
   private String blindStructureName;

   @OneToMany(mappedBy = "gameBlindStructure")
   private List<GameBlindStructureDetailsEntity> gameBlindStructureDetailsEntities;
 }

Many-sided entity:

@Entity
@Table(name = "GAME_BLIND_STRUCTURE_DETAILS")
@Builder
@Getter
@Setter
public class GameBlindStructureDetailsEntity implements Serializable {
   private static final long serialVersionUID = -7800120016594245121L;
   @Id
   @Column(name = "BLIND_DETAILS_ID")
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long blindDetailsId;

   @ManyToOne
   @JoinColumn(name = "BLIND_ID")
   private GameBlindStructureEntity gameBlindStructure;

   @Column(name = "LEVEL")
   private String level;

   @Column(name = "SMALL_BLIND")
   private Integer smallBlind;

   @Column(name = "BIG_BLIND")
   private Integer bigBlind;

   @Column(name = "ANTE")
   private Integer ante;

   @Column(name = "TIME_BANK")
   private String timeBank;

   @Column(name = "MINUTES")
   private Integer minutes; 
 }

In the service method, I am trying to persist these entities to database.

public BlindStructureResponseDto createBlindStructure(BlindStructureDto blindStructureDto) {
   GameBlindStructureEntity gameBlindStructureEntity = GameBlindStructureEntity.builder()
        .blindStructureName(blindStructureDto.getName())
        .build();

List<BlindStructureDetailsDto> blindStructureDetailsDtos = blindStructureDto.getBlindStructureDetailsDtos();
List<GameBlindStructureDetailsEntity> gameBlindStructureDetailsEntities = new ArrayList<>();

for(BlindStructureDetailsDto blindStructureDetailsDto : blindStructureDetailsDtos) {
    GameBlindStructureDetailsEntity gameBlindStructureDetailsEntity = mapper.convertToGameStructureDetailsEntity(blindStructureDetailsDto);
    gameBlindStructureDetailsEntity.setGameBlindStructure(gameBlindStructureEntity);
    gameBlindStructureDetailsEntities.add(gameBlindStructureDetailsEntity);
    
}
gameBlindStructureEntity.setGameBlindStructureDetailsEntities(gameBlindStructureDetailsEntities);

GameBlindStructureEntity savedEntity =  blindStructureRepository.save(gameBlindStructureEntity);

BlindStructureResponseDto blindStructureResponseDto = BlindStructureResponseDto.builder()
        .name(savedEntity.getBlindStructureName())
        .blindId(savedEntity.getBlindId())
        .build();
return blindStructureResponseDto;
}

Though the entity on one-side is getting persisted to the database, the many sided entity is not getting saved. Here is the ddl script:

DROP TABLE  IF EXISTS `GAME_BLIND_STRUCTURE`;
CREATE TABLE `GAME_BLIND_STRUCTURE`
   ( `BLIND_ID` INT AUTO_INCREMENT,
    `BLIND_STRUCTURE_NAME` VARCHAR(255) NOT NULL,
    PRIMARY KEY (`BLIND_ID`),
    UNIQUE KEY `UNIQUE_BLIND_STRUCTURE_NAME` (`BLIND_STRUCTURE_NAME`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `GAME_BLIND_STRUCTURE_DETAILS`;
CREATE TABLE `GAME_BLIND_STRUCTURE_DETAILS`
(`BLIND_DETAILS_ID` INT AUTO_INCREMENT,
`BLIND_ID` INT NOT NULL,
`LEVEL` VARCHAR(255) NOT NULL,
`SMALL_BLIND` INT NOT NULL,
`BIG_BLIND` INT NOT NULL,
`ANTE` INT NOT NULL,
`TIME_BANK`VARCHAR(255) NOT NULL,
`MINUTES` INT NOT NULL,
PRIMARY KEY(`BLIND_DETAILS_ID`),
CONSTRAINT `FK_BLIND_ID` FOREIGN KEY (`BLIND_ID`) REFERENCES `GAME_BLIND_STRUCTURE` (`BLIND_ID`)
)ENGINE=INNODB DEFAULT CHARSET=latin1;

CodePudding user response:

You're missing CascadeType.ALL on your @OneToMany annotation, code should be as follows:

   @OneToMany(mappedBy = "gameBlindStructure", cascade = CascadeType.ALL)
   private List<GameBlindStructureDetailsEntity> gameBlindStructureDetailsEntities;
  • Related