Akshay
Akshay

Reputation: 11

OneToMany fetch lazy not working with spring boot + JPA

I have two entities. Student and department.One department has many student. I have specified fetch type lazy. still when I am trying to fetch only department entity it is showing student data as well.

 @Entity(name = "tbl_department")
public class DepartmentEntity implements Serializable {

private static final long serialVersionUID = 1560990434803170482L;

@Id
@GeneratedValue
private Integer id;

@NotNull(message = "Department name can not be null")
private String departmentName;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "department", fetch = FetchType.LAZY)
private Set<StudentEntity> studentList;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getDepartmentName() {
    return departmentName;
}

public void setDepartmentName(String departmentName) {
    this.departmentName = departmentName;
}

public Set<StudentEntity> getStudentList() {
    return studentList;
}

public void setStudentList(Set<StudentEntity> studentList) {
    this.studentList = studentList;
}


 @Entity(name = "tbl_student")
 public class StudentEntity implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 2969421115938100526L;

@Id
@GeneratedValue
private Integer id;

@NotNull(message = "First Name Can not null")
private String firstName;

@NotNull(message = "Last Name Can not null")
private String lastName;

@NotNull(message = "Date Of Birth Can not null")
private Date dateOfBirth;

private Integer age;

@NotNull(message = "Address Can not null")
private String address;

@JsonIgnore
@ManyToOne(cascade = CascadeType.ALL)
private DepartmentEntity department;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public Date getDateOfBirth() {
    return dateOfBirth;
}

public void setDateOfBirth(Date dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
}

public Integer getAge() {
    return age;
}

public void setAge(Integer age) {
    this.age = age;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public DepartmentEntity getDepartment() {
    return department;
}

public void setDepartment(DepartmentEntity department) {
    this.department = department;
}

So when I am trying to fetch department entity using departmentRepo it shows all students mapped to that department even if I specified fetch type lazy

Upvotes: 1

Views: 1631

Answers (1)

rajasekhar pippalla
rajasekhar pippalla

Reputation: 195

Can you declare your entities as below and try once

Student Entity:

@OrderBy("id ASC")
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="departmentid")
@JsonIgnore
private Department department;

Department Entity:

@OrderBy("id ASC")
@OneToMany(fetch=FetchType.LAZY, mappedBy="department")
 private Set<Student> students = new LinkedHashSet<Student>(0);

Upvotes: 1

Related Questions