# Hibernate 5.1.10 - lazy loading doesn't work

**URL:** <https://discourse.hibernate.org/t/hibernate-5-1-10-lazy-loading-doesnt-work/401>\
**Category:** Hibernate ORM\
**Created:** [March 22, 2018, 11:15am UTC](https://discourse.hibernate.org/t/hibernate-5-1-10-lazy-loading-doesnt-work/401 "2018-03-22T11:15:25Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![arkadyz](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@arkadyz](https://discourse.hibernate.org/u/arkadyz)\
**Post date:** [March 22, 2018, 11:15am UTC](https://discourse.hibernate.org/t/hibernate-5-1-10-lazy-loading-doesnt-work/401/1 "2018-03-22T11:15:25Z")

</div>

Hello,

I am working with Wildfly 11 which uses hibernate 5.1.10

Lazy loading doesn’t work for me, it always makes eager loading. I am doing following:

```
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "device")
@LazyToOne(LazyToOneOption.NO_PROXY)
public DeviceRegistrationEntity getDeviceRegistration() {
	return deviceRegistration;
}

```

In Wildfly 8.2.0 (which user more older version of hibernate) I used org.hibernate.bytecode.internal.javassist.FieldHandled and org.hibernate.bytecode.internal.javassist.FieldHandler but these classes not exist in hibernate 5.1.10

Can you advice please how to switch on lazy-loading mode ?

Thanks,  
Arkady.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [March 22, 2018, 12:03pm UTC](https://discourse.hibernate.org/t/hibernate-5-1-10-lazy-loading-doesnt-work/401/2 "2018-03-22T12:03:26Z")

</div>

Lazy loading works except for the parent side of a `@OneToOne` association. This is because Hibernate has no other way of knowing whether to assign a null or a Proxy to this variable. More details you can find in [this article](https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/).

Now, to fix it, you have two options:

1. You can activate [lazy loading bytecode enhancement](https://vladmihalcea.com/the-best-way-to-lazy-load-entity-attributes-using-jpa-and-hibernate/)
2. Or, you can just remove the parent side and use the client side with `@MapsId` as explained in [this article](https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/). This way, you will find that you don’t really need the parent side since the child shares the same id with the parent so you can easily fetch the child by knowing the parent id.

---

<div class="post-metadata">

**Author:** ![arkadyz](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@arkadyz](https://discourse.hibernate.org/u/arkadyz)\
**Post date:** [March 22, 2018, 3:39pm UTC](https://discourse.hibernate.org/t/hibernate-5-1-10-lazy-loading-doesnt-work/401/3 "2018-03-22T15:39:10Z")

</div>

I tried to use @Maps but it didn’t help. Still makes eager loading. Here is my code:

```auto
@SuppressWarnings("serial")
@Entity
@Table(name = "DEVICE")
public class DeviceEntity implements Serializable{
       private DeviceRegistrationEntity deviceRegistration;
       private Integer id;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "ID")
	public Integer getId() {
		return id;
	}
    
	@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "device")
	public DeviceRegistrationEntity getDeviceRegistration() {
		return deviceRegistration;
	}
}

@SuppressWarnings("serial")
@Entity
@Table(name = "DEVICE_REGISTRATION")
public class DeviceRegistrationEntity implements Serializable{
     private DeviceEntity device;

	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "DEVICE_ID", insertable = false, updatable = false)
	@MapsId
	public DeviceEntity getDevice() {
		return device;
	}

	public void setDevice(DeviceEntity device) {
		this.device = device;
	}
}

```

During loading DeviceEntity, it loads also the child, i.e. DeviceRegistrationEntity

Any help is appreciated.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [March 22, 2018, 3:43pm UTC](https://discourse.hibernate.org/t/hibernate-5-1-10-lazy-loading-doesnt-work/401/4 "2018-03-22T15:43:14Z")

</div>

If you read that article carefully, you will see that by using `@MapsId`, you don’t really need to use the `@OneToOne` with mappedBy on the parent side.

Just adding `@MapsId` to your client-side does not solve the eager on the parent side, but it makes the `mappedBy` side redundant.

So, remove this:

```auto
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "device")
public DeviceRegistrationEntity getDeviceRegistration() {
	return deviceRegistration;
}

```

Since you can now fetch the `DeviceRegistrationEntity` like this:

```auto
DeviceEntity deviceEntity = ...
DeviceRegistrationEntity dre = entityManager.find(DeviceRegistrationEntity.class, deviceEntity.getId());

```

If you really need to have the mappedBy `@OneToOne`, then you will find another solution in my previous answer.

---

<div class="post-metadata">

**Author:** ![arkadyz](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@arkadyz](https://discourse.hibernate.org/u/arkadyz)\
**Post date:** [March 22, 2018, 4:18pm UTC](https://discourse.hibernate.org/t/hibernate-5-1-10-lazy-loading-doesnt-work/401/5 "2018-03-22T16:18:19Z")

</div>

I can’t remove reference to child object on parent side.

I solved the problem by adding “optional = false” to all my OneToOne relations.  
Works fine, but I don’t understand why does it work. Can you explain please ?

Anyway, I am very appreciate your help.

Thanks,  
Arkady.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [March 22, 2018, 4:33pm UTC](https://discourse.hibernate.org/t/hibernate-5-1-10-lazy-loading-doesnt-work/401/6 "2018-03-22T16:33:31Z")

</div>

The optional trick does not work on every version of Hibernate, so it might break if you upgrade.

---

<div class="post-metadata">

**Author:** ![arkadyz](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@arkadyz](https://discourse.hibernate.org/u/arkadyz)\
**Post date:** [March 23, 2018, 8:37am UTC](https://discourse.hibernate.org/t/hibernate-5-1-10-lazy-loading-doesnt-work/401/7 "2018-03-23T08:37:42Z")

</div>

Suppose I take this solution and remove reference to “deviceRegistration” from “DeviceEntity”. You wrote:

DeviceEntity deviceEntity = …  
DeviceRegistrationEntity dre = entityManager.find(DeviceRegistrationEntity.class, deviceEntity.getId());

As far as I know, “find” function of EntityManager works by primary key. In my case primary key of DeviceRegistrationEntity is not the same as DeviceEntity, it has it’s own ID field, it also has

```
   private Integer id;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
public Integer getId() {
	return id;
}

```

So how to fetch it knowing DeviceEntity ? By named query ?

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [March 23, 2018, 8:48am UTC](https://discourse.hibernate.org/t/hibernate-5-1-10-lazy-loading-doesnt-work/401/8 "2018-03-23T08:48:26Z")

</div>

A one-to-one association without sharing the PK is a design smell. According to database table relationship, a one-to-one association always shares the PK.

In your case, unless you change your design, you’ll have to stick to using a separate PK which doubles the amount of memory that requires indexing since you need an index for the PK and another one for the FK.

---

<div class="post-metadata">

**Author:** ![arkadyz](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@arkadyz](https://discourse.hibernate.org/u/arkadyz)\
**Post date:** [March 23, 2018, 5:50pm UTC](https://discourse.hibernate.org/t/hibernate-5-1-10-lazy-loading-doesnt-work/401/9 "2018-03-23T17:50:54Z")

</div>

Vlad, thank you very-very much for quick and detailed answers. Now everything is clear me with one-to-one association.

Do you have an article about lazy loading in one-to-many association ? We also have problem there and I want to know how to do it correctly.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [March 23, 2018, 7:20pm UTC](https://discourse.hibernate.org/t/hibernate-5-1-10-lazy-loading-doesnt-work/401/10 "2018-03-23T19:20:42Z")

</div>

For OneToMany, the lazy loading is straightforward so you don’t have to worry about it. Just use LAZY and you will be ok.

---

<div class="post-metadata">

**Author:** ![vkotecha91](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vkotecha91/32/2922_2.png) [@vkotecha91](https://discourse.hibernate.org/u/vkotecha91)\
**Post date:** [March 15, 2019, 9:31am UTC](https://discourse.hibernate.org/t/hibernate-5-1-10-lazy-loading-doesnt-work/401/12 "2019-03-15T09:31:32Z")

</div>

Hi @vlad thanks for the great solutions. Its always good to read your articles. I have one question - what if we have more than one “OneToOne” mappings? The @MapsId won’t be applicable in that case, right?

---

<div class="post-metadata">

**Author:** ![NAVEEN\_KUMAR](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/naveen_kumar/32/913_2.png) [@NAVEEN\_KUMAR](https://discourse.hibernate.org/u/NAVEEN_KUMAR)\
**Post date:** [January 4, 2020, 12:04pm UTC](https://discourse.hibernate.org/t/hibernate-5-1-10-lazy-loading-doesnt-work/401/13 "2020-01-04T12:04:41Z")

</div>

Lazy loading not working with hql.  
_ **table names = human , pet** _

The result of this query contains pets also

```auto
Query query = session.createQuery("from human");
List<human> humans = query.list();

```

PET Entity

```auto
@Entity
public class pet implements Serializable
{
	@Id
	//@ManyToOne(targetEntity = human.class)
	//@JoinColumn(name="id")
	public long id;

	public String name;
}

```

human Entity

```auto
	@Id
	@Column(name="id")
	private int id;

	@Column(name="name", length=20, nullable=true)
	private String name;

	@OneToMany(fetch = FetchType.LAZY)
	@JoinColumn(name="id")
	public List<pet> pets = new ArrayList<>();

	public human(int id, String name)
	{
		this.name = name;
		this.id = id;
	}

	public human()
	{
	}

```

Kindly assist me. Thanks in Advance!
