Внешний ключ JPA в отношениях @OneToOne не отображается в запросе

#spring-boot #jpa #one-to-one

#весенняя загрузка #jpa #взаимно однозначный

Вопрос:

Я пытаюсь создать отношение @OneToOne между объектами FacilityGeneral и FacilityLocation, в котором объект FacilityLocation владеет внешним ключом. Я могу запустить приложение без каких-либо ошибок, но когда я запрашиваю объект FacilityGeneral, используя встроенную инструкцию findAll(), именованное поле (внешний ключ) не отображается как значение поля. Значения действительно существуют в таблице базы данных. Что здесь происходит?

Общая сущность FacilityGeneral:

 @Entity
public class FacilityGeneral {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "facilityGenIdSeq")
@SequenceGenerator(name = "facilityGenIdSeq", initialValue = 567, allocationSize = 1)
@Column(updatable = false, unique = true)
private Long generalIdPk;

@NotBlank(message = "Facility Name is required")
private String facilityName;

@Column(length = 200)
private String facilityNameHistory;

@Column(length = 15)
private String phoneNumber;

private Integer phoneExtension;
@Column(length = 50)
private String facilityType;
@Column(length = 50)
private String facilityApprovalType;
@Column(length = 50)
private String landfillClass;
@Column(length = 50)
private String facilityStatus;
@Column(length = 50)
private String firstReceiptOfWaste; //This is a String because legacy data is inconsistent (some only years, others full dates)

@Column(length = 50)
private String ownershipType;
@Column(length = 50)
private String dshwStaffPerson;
@Column(length = 50)
private String legAndGovApproval;

@JsonFormat(pattern = "MM/dd/yyyy")
private LocalDate dateOfLegApproval;

@JsonFormat(pattern = "MM/dd/yyyy")
private LocalDate dateOfGovApproval;

@Column(length = 200)
private String govLegHyperlink;
@JsonFormat(pattern = "MM/dd/yyyy")
private LocalDate localApprovalDate;
private String localApprovalDescription;
@Column(length = 500)
private String comments;
@Column(length = 50)
private String tireRegistrationNumber;
@Column(length = 50)
private String tireRegistrationFeePaid;
@Column(length = 50)
private String tireRegAppTrackingNumber; /*Tire Registration Application Tracking Numner*/
@JsonFormat(pattern = "MM/dd/yyyy")
private LocalDate tireRegInsActivationDate; /*Tire Registration Insurance Application Date*/
@JsonFormat(pattern = "MM/dd/yyyy")
private LocalDate tireRegInsExpirationDate; /*Tire Registration Insurance Expiration Data*/

@OneToOne(fetch = FetchType.EAGER, mappedBy = "facilityGeneral", cascade = 
CascadeType.PERSIST)
private FacilityLocation location;
  

Объект местоположения объекта

 @Entity
public class FacilityLocation {

@Id
@SequenceGenerator(name = "facilityLocationSeq", initialValue = 374, allocationSize = 1)
@GeneratedValue (strategy = GenerationType.SEQUENCE, generator = "facilityLocationSeq")
@Column(updatable= false, nullable = false, unique = true)
private Long locationIdPk;

@OneToOne (fetch = FetchType.EAGER)
@JoinColumn (name="facilityIdFk", referencedColumnName ="generalIdPk", nullable = false)
@JsonIgnore
private FacilityGeneral facilityGeneral;

private Double latitudeDegrees;
private Double latitudeMinutes;
private Double latitudeSeconds;
private Double longitudeDegrees;
private Double longitudeMinutes;
private Double longitudeSeconds;
private Double northing;
private Double easting;
private Double xCoordinate;
private Double yCoordinate;
private String descriptiveDirections;
private String county;
private String healthDepartment;
private String healthDepartContact;
private String facilityAddressStreet;
private String facilityAddressCity;
private Integer facilityZipFiveDigit;
private Integer facilityZipFourDigit;
private Integer townshipNumber;
private String townshipDirection;
private Integer rangeNumber;
private String rangeDirection;
private String section;
private String quarterSection;
private String qtrQtrSection;
private String baseline;

@JsonFormat(pattern = "MM/dd/yyyy")
private LocalDate created_on;

private Integer politicalDistrictHouse;
private Integer politicalDistrictSenate;
private String comments;
private Double latitudeDecimalDegrees;
private Double longitudeDecimalDegrees;
  

Результирующий объект из запроса FacilityLocation НЕ содержит поля facilityIdFk, которое мне нужно:

  {
    "locationIdPk": 53,
    "latitudeDegrees": 38,
    "latitudeMinutes": 10,
    "latitudeSeconds": 10.16,
    "longitudeDegrees": 100,
    "longitudeMinutes": 10,
    "longitudeSeconds": 10.10,
    "northing": 0.000,
    "easting": 0.00,
    "xCoordinate": 000.0000,
    "yCoordinate": 4241085.372,
    "descriptiveDirections": "Approximately three miles northwest of Beaver and one mile east of Interstate 15",
    "county": "Beaver",
    "healthDepartment": "Public Health Department",
    "healthDepartContact": null,
    "facilityAddressStreet": null,
    "facilityAddressCity": "Beaver",
    "facilityZipFiveDigit": 0,
    "facilityZipFourDigit": 0,
    "townshipNumber": 29,
    "townshipDirection": "S",
    "rangeNumber": 7,
    "rangeDirection": "W",
    "section": "8",
    "quarterSection": "NW",
    "qtrQtrSection": "NE",
    "baseline": "SLBM",
    "created_on": "07/03/2006",
    "politicalDistrictHouse": null,
    "politicalDistrictSenate": 28,
    "comments": "Facility is located in the NE of the NW, the SE of the NW, and the NE of the NE quarter of section 8",
    "latitudeDecimalDegrees": 0.000,
    "longitudeDecimalDegrees": 0.000,
    "modified_on": null
},
  

Комментарии:

1. Рассмотрите возможность удаления @JsonIgnore on FacilityLocation#facilityGeneral . Похоже, что поле опущено во время сериализации JSON из-за этой аннотации.

2. Спасибо @seb.wired — в тот раз это сработало.