# @ManyToOne lazy loading gives null value for child object

**URL:** <https://discourse.hibernate.org/t/manytoone-lazy-loading-gives-null-value-for-child-object/8131>\
**Category:** Hibernate ORM\
**Created:** [August 16, 2023, 9:14am UTC](https://discourse.hibernate.org/t/manytoone-lazy-loading-gives-null-value-for-child-object/8131 "2023-08-16T09:14:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vivek\_Ikhar](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vivek_ikhar/32/2619_2.png) [@Vivek\_Ikhar](https://discourse.hibernate.org/u/Vivek_Ikhar)\
**Post date:** [August 16, 2023, 9:14am UTC](https://discourse.hibernate.org/t/manytoone-lazy-loading-gives-null-value-for-child-object/8131/1 "2023-08-16T09:14:39Z")

</div>

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.

```auto
@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

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [August 16, 2023, 12:55pm UTC](https://discourse.hibernate.org/t/manytoone-lazy-loading-gives-null-value-for-child-object/8131/2 "2023-08-16T12:55:33Z")

</div>

Which Hibernate version are you using? Did you try updating to the latest version yet? If so, and the problem persists, please create an issue in the issue tracker([https://hibernate.atlassian.net](https://hibernate.atlassian.net)) with a test case([https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java)) that reproduces the issue.

---

<div class="post-metadata">

**Author:** ![Vivek\_Ikhar](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vivek_ikhar/32/2619_2.png) [@Vivek\_Ikhar](https://discourse.hibernate.org/u/Vivek_Ikhar)\
**Post date:** [August 17, 2023, 5:43am UTC](https://discourse.hibernate.org/t/manytoone-lazy-loading-gives-null-value-for-child-object/8131/3 "2023-08-17T05:43:12Z")

</div>

Thank you for your reply.

I have tried the latest Hibernate version, but the issue still persists. Sometimes it works, but other times it doesn’t, even with accurate DB data. It’s exhibiting strange behavior.

---

<div class="post-metadata">

**Author:** ![Alexis\_Nikas](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/alexis_nikas/32/2578_2.png) [@Alexis\_Nikas](https://discourse.hibernate.org/u/Alexis_Nikas)\
**Post date:** [January 15, 2024, 5:29pm UTC](https://discourse.hibernate.org/t/manytoone-lazy-loading-gives-null-value-for-child-object/8131/4 "2024-01-15T17:29:35Z")

</div>

hello , i have the same issue . Did you find the reason why is that happening ?
