Spring JPA 未返回外键作为响应

问题描述 投票:0回答:3

我有一个包含一些实体的数据库,(在父子关系中)我可以说,当我尝试对子表进行查询以获取所有行时,我只获取不是外键的字段。如何在响应 json 中包含外键。谁能帮我解决这个问题吗?

当我尝试

repository.findAll()
时,父类工作正常,并且按预期工作。

@Entity
@Table(name = "Employees")
@Data
@NoArgsConstructor
public class Employee {
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
  @SequenceGenerator(name = "sequenceGenerator")
  private long id;
  private String name;
  private String description;

  @OneToMany(fetch = FetchType.EAGER, mappedBy = "employee")
  private List<Projects> projects;

  @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "employee")
  private Address address;
}

少儿班:


@Entity
@Table(name = "Address")
@Data
@NoArgsConstructor
public class Address {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long id;
  private String city;
  private String state;

  @OneToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "emp_id", nullable = false)
  @JsonBackReference
  private Employee employee;
}

这是地址实体的存储库类

@Repository
public interface AddressRepository extends JpaRepository<Address, Long> {
}

当我尝试时

AddressRepository.findAll()

我得到了什么:

[{
        "id": 1,
        "city": "new york",
        "state": "new york"
    }]

我想要得到什么:

        "id": 1,
        "city": "new york",
        "state": "new york",
        "emp_id": 1 //which is the foreign key referring to Employee table
    }]

我尝试的是按如下方式更新地址实体中的员工列,但没有成功


@Entity
@Table(name = "Address")
@Data
@NoArgsConstructor
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String city;
private String state;


@OneToOne(fetch = FetchType.EAGER)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "client_id", scope = Client.class)
@JsonIdentityReference(alwaysAsId = true)
@JoinColumn(name = "client_id", nullable = false)
@JsonBackReference
@JsonProperty("clientId")
private Employee employee;
}

java json spring-boot spring-data-jpa
3个回答
1
投票

您可以使用 JPA 投影:

public class AddressDto {
    private long id;
    private String city;
    private String state;
    private long employeeId;

    public AddressDto(long id, String city, String state, Employee employee) {
        this.id = id;
        this.city = city;
        this.state = state;
        this.employeeId = employee.getId();
    }
    // getters etc..
}


@Repository
public interface AddressRepository extends JpaRepository<Address, Long> {
    List<AddressDto> findAllProjectedBy();
}


0
投票

在每个外键上使用@JsonProperty。

@Entity
@Table(name = "Employees")
@Data
@NoArgsConstructor
public class Employee {
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
  @SequenceGenerator(name = "sequenceGenerator")
  private long id;
  private String name;
  private String description;
  
  @JsonProperty
  @OneToMany(fetch = FetchType.EAGER, mappedBy = "employee")
  private List<Projects> projects;

  @JsonProperty
  @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "employee")
  private Address address;
}

0
投票

如果您尝试返回 JSON 中的外键值,我遇到了同样的问题,并按如下方式修复了它。

您可以在类中定义一个新属性:

@Entity
@Table(name = "Address")
@Data
@NoArgsConstructor
public class Address {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long id;
  private String city;
  private String state;

  @OneToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "emp_id", nullable = false)
  @JsonBackReference
  private Employee employee;

  // -------------------------
  // Add this to your class
  // To display the value in the response only
  @Column(name = "emp_id", insertable = false, updatable = false)
  private Long empId;
  // -------------------------

}

请注意,“emp_id”应该已经存在于您的数据库表“Address”中,因此您需要在实体类中添加该属性,以便框架可以显示它。

并且由于您仅使用它来显示需要指定的值 “insertable = false, updatable = false”以避免与重复属性相关的问题。

我不确定这个解决方案将来会引起任何问题,但我认为值得尝试。

© www.soinside.com 2019 - 2024. All rights reserved.