我正在使用

问题描述 投票:0回答:1
)时,核心键不能保存。我试图遵循所有描述的说明和设置

package org.cnr.plantvocdb.entity; import java.time.OffsetDateTime; import java.util.Set; import java.util.UUID; import jakarta.persistence.*; import lombok.*; import org.apache.commons.lang3.StringUtils; import org.cnr.plantvocdb.enums.LeafHabitus; import org.cnr.plantvocdb.enums.PlantsRanks; @Entity @AllArgsConstructor @NoArgsConstructor @Builder @Getter @Setter @Table(name = "plants_voc") public class PlantVocEntity { @Id @GeneratedValue(strategy = GenerationType.UUID) @Column(name="id", length = 50, nullable = false, updatable = false) private UUID id; @Column(name="ipni", length = 50) private String ipni; @Column(name="full_name_plain", length = 50) private String fullNamePlain; @Column(name="full_name_no_authors_plain", length = 50) private String fullNameNoAuthorsPlain; @Setter(AccessLevel.NONE) @Column(name="name", length = 30, nullable = false) private String name; @Setter(AccessLevel.NONE) @Column(name="family", length = 30, nullable = false) private String family; @Setter(AccessLevel.NONE) @Column(name="genus", length = 20, nullable = false) private String genus; @Setter(AccessLevel.NONE) @Column(name="species", length = 20) private String species; @Column(name="valid_nomenclature") private boolean validNomenclature; @Column(name="rank") @Enumerated(EnumType.STRING) private PlantsRanks rank; @Column(name="leaf_habitus") @Enumerated(EnumType.STRING) private LeafHabitus leafHabitus; @OneToMany( fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "plant" ) private Set<PlantEmitterEntity> emitter; @ElementCollection @Column(name="synonyms") private Set<String> synonyms; @Column(name="created_datetime_utc", updatable = false) // creation_datetime_utc private OffsetDateTime createdDatetimeUTC; @Column(name="updated_datetime_utc") // last_modified_datetime_utc private OffsetDateTime updatedDatetimeUTC; public void setName(String name) { this.name = name.toLowerCase(); } public void setFamily(String family) { this.family = StringUtils.capitalize(family.toLowerCase()); } public void setGenus(String genus) { this.genus = StringUtils.capitalize(genus.toLowerCase()); } public void setSpecies(String species) { this.species = species.toLowerCase(); } } package org.cnr.plantvocdb.entity; import jakarta.persistence.*; import lombok.Getter; import lombok.Setter; @Entity @Table(name="emitters") @Getter @Setter public class PlantEmitterEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name="emits") private boolean emits; @Column(name="doi") private String doi; @ManyToOne @JoinColumn(name = "fk_plant_id", nullable = false, updatable = true, insertable = true) private PlantVocEntity plant; } package org.cnr.plantvocdb.service; import org.cnr.plantvocdb.dto.RequestPlantVocDTO; import org.cnr.plantvocdb.dto.ResponsePlantVocDTO; import org.cnr.plantvocdb.entity.PlantVocEntity; import org.cnr.plantvocdb.repository.PlantsVocRepository; import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.time.OffsetDateTime; import java.time.ZoneOffset; import java.util.List; @Service public class PlantsVocService { private final PlantsVocRepository repository; private final ModelMapper mapper; @Autowired public PlantsVocService(PlantsVocRepository repository, ModelMapper mapper) { this.repository = repository; this.mapper = mapper; } public ResponsePlantVocDTO CreateNewPlantVoc(RequestPlantVocDTO plantDTO){ // map DTO to Entity PlantVocEntity plantEntity = mapper.map(plantDTO, PlantVocEntity.class); // set datetime in UTC OffsetDateTime odt = OffsetDateTime.now(ZoneOffset.UTC); plantEntity.setCreatedDatetimeUTC(odt); plantEntity.setUpdatedDatetimeUTC(odt); // save new plant in DB PlantVocEntity savedPlantEntity = repository.save(plantEntity); // map Entity to DTO return mapper.map(savedPlantEntity, ResponsePlantVocDTO.class); } }

您不显示执行保存操作的服务的代码,但我怀疑您不初始化enter image description herePlantEmitterEntity#plant.

中class
PlantVocEntity
java postgresql spring-boot foreign-keys one-to-many
1个回答
0
投票
PlantEmitterEntity

实例

    void addPlantEmitterEntity(PlantEmitterEntity emitterEntity) {
        emitterEntity.setPlant(this);
        emitter.add(emitterEntity);
    }

在您的服务类中使用此方法。

	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.