I have composed the following code for an entity. While fetching data for the ParentEntity
from the database, I successfully obtain the value of parentEntity
. However, when attempting to retrieve the childEntity
value from the parentEntity
, I am encountering a null value(parentEntity.getChildEntity() gives null but it shouldn’t as given child entity exist in DB). It is worth noting that the child_entity_id
does exist in the database. As a result, the value of parentEntity.getChildEntity()
should not be null. I have attempted various solutions, but unfortunately, none of them have proven effective. I would greatly appreciate your assistance in resolving this issue. Thank you in advance.
@Entity
@Table(name = "parent_entity")
@Getter
@Setter
@ToString
public class ParentEntity implements Serializable {
private static final long serialVersionUID = -6607515891184993053L;
public ParentEntity() {
}
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(name = "id", unique = true, nullable = false, length = 40)
private String id;
@ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
@JoinColumn(name = "child_entity_id")
private ChildEntity childEntity;
}
@Entity
@Table(name = "child_entity")
@Builder(toBuilder = true)
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ChildEntity implements Serializable {
private static final long serialVersionUID = -1105051641140339940L;
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(name = "[ID]", unique = true, nullable = false, length = 40)
private String id;
@Builder.Default
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "childEntity")
private Set<ParentEntity> parentEntity = new HashSet<>();
}
Please let me know if you need further assistance or clarification.
P.S. I have tried with FetchType.EAGER it is working but affecting the performance