Home > Enterprise >  findById() is returning last row only instead of returning all the data of that ID
findById() is returning last row only instead of returning all the data of that ID

Time:07-15

I am using findById(), which should return all data associated with the ID. But It's returning only last row in the table.

Parent entity

   @Entity
   @Table(uniqueConstraints = @UniqueConstraint(columnNames = { "category_name" }))
public class screenerCategory {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "category_name")
    private String categoryName;

    @OneToMany(targetEntity = screenerCriteria.class, cascade = CascadeType.ALL,  fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumn
    @JsonManagedReference
    private List<screenerCriteria> screenerCriteria;

//getters and setters...
}

Child entity

@Entity
@Table( uniqueConstraints = @UniqueConstraint(columnNames = { "criteria_name" }))
public class screenerCriteria {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "criteria_name")
    private String criteriaName;

    @ManyToOne(targetEntity = screenerCategory.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "screener_category_id")
    @JsonBackReference
    private screenerCategory screenerCategory;

//getters and setters...
}

Service class

public List<screenerCategory> fetchData(int id) {
        List<screenerCategory> screenerCategory = screenerCategoryRepo.findById(id);
        return screenerCategory;
    }

Controller class

@GetMapping("getData/{id}")
public List<screenerCategory> getData(@PathVariable int id) {
        return screenerCategoeyService.fetchData(id);
    }

Response in Postman

[
    {
        "id": 2,
        "categoryName": "Fixed Income",
        "screenerName": [],
        "formulaBuilder": [],
        "screenerCriteria": [
            {
                "id": 22,
                "criteriaName": "Yield to Maturity16"
                "createdAt": "2022-07-14T12:40:35.000 00:00",
                "modifiedAt": "2022-07-14T12:40:35.000 00:00"#
            }
        ],
        "createdAt": null,
        "modifiedAt": "2022-07-14T12:40:35.000 00:00"
    }
]

Child table screenshot enter image description here

CodePudding user response:

You have incorrectly setup the screenerCategory-screenerCriteria relationship in JPA, and have specified the @PrimaryKeyJoinColumn incorrectly. Presumably you only have a single relationship that you are trying to map with a bidirectional OneToMany from screenerCategory-> screenerCriteria, and then a ManyToOne screenerCriteria->screenerCategory, it should be:

@Entity
public class screenerCategory {
    ..
    @OneToMany(mappedBy="screenerCategory",cascade = CascadeType.ALL,  fetch = FetchType.LAZY)
    private List<screenerCriteria> screenerCriteria;
    ..
}

@Entity
public class screenerCriteria {

    @ManyToOne(targetEntity = screenerCategory.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "screener_category_id")
    private screenerCategory screenerCategory;

//getters and setters...
}

Using mappedBy in the OneToMany has it using the foreign key defined in your child table, which references the parent's ID. When you used PrimaryKeyJoinColumn from your question, you were telling JPA that the Id in the parent is a Foreign key to the Child's Id (not the 'screener_category_id' column that is the actual FK).

  • Related