Home > Enterprise >  Spring JPA not returning Foreign keys in response
Spring JPA not returning Foreign keys in response

Time:05-07

I have a database with some entities, ( in parent child relationship )I can say and when When I try to make a query to child table to get all the rows, I only get the fields which are not foreign keys. How can I include the foreign key in the response json. Can anyone help me figure this out ?

Parent Class which is working fine when I try to repository.findAll() and it works as expected.

@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;
}

Child class:


@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;
}

Here is the repository class for Address Entity

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

When I try AddressRepository.findAll()

What I get:

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

what I want to get:

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

What I tried is I updated my Employee column in Address Entity as follow but no luck


@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;
}

CodePudding user response:

You could use a JPA projection:

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();
}

CodePudding user response:

Use @JsonProperty on each Foreign Key.

@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;
}
  • Related