简体   繁体   English

使用 jpa,一对多关系的许多方面都没有得到持久化

[英]With jpa, many side of a one-to-many relationship is not getting persisted

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:这是 ddl 脚本:

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;

You're missing CascadeType.ALL on your @OneToMany annotation, code should be as follows:您在@OneToMany注释中缺少CascadeType.ALL ,代码应如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM