Anonymous
Spring Boot @onetomany и @manytoone Не сохраняйте иностранный ключ
Сообщение
Anonymous » 05 фев 2025, 00:09
Я использую
@onetomany и
@manytoone в Spring Boot и когда я пытаюсь сохранить в базе данных ( postgressql ) иностранный ключ не спасен. Я попытался следовать всем описанным инструкциям и настройкам, но когда я проверяю DB, таблица «Эмиттеры», все сохраненные элементы всегда имеют внешний ключ NULL < /p>
Код: Выделить всё
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
emitter;
@ElementCollection
@Column(name="synonyms")
private Set 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);
}
}
Обновление 1 по предложению @roar s.
< /p>
Обновление 2 по предложению @roar S. (работает) < /p>
Подробнее здесь:
https://stackoverflow.com/questions/794 ... oreign-key
1738703346
Anonymous
Я использую [b] @onetomany [/b] и [b] @manytoone [/b] в Spring Boot и когда я пытаюсь сохранить в базе данных ( postgressql ) иностранный ключ не спасен. Я попытался следовать всем описанным инструкциям и настройкам, но когда я проверяю DB, таблица «Эмиттеры», все сохраненные элементы всегда имеют внешний ключ NULL < /p> [code]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 emitter; @ElementCollection @Column(name="synonyms") private Set 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); } } [/code] Обновление 1 по предложению @roar s. < /p> Обновление 2 по предложению @roar S. (работает) < /p> Подробнее здесь: [url]https://stackoverflow.com/questions/79412933/spring-boot-onetomany-and-manytoone-dont-save-foreign-key[/url]