# Problem with lazy loading and lack of a backing field

**URL:** <https://discuss.kotlinlang.org/t/problem-with-lazy-loading-and-lack-of-a-backing-field/1415>\
**Category:** Support\
**Created:** [January 17, 2016, 3:52pm UTC](https://discuss.kotlinlang.org/t/problem-with-lazy-loading-and-lack-of-a-backing-field/1415 "2016-01-17T15:52:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![byron](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@byron](https://discuss.kotlinlang.org/u/byron)\
**Post date:** [January 17, 2016, 3:52pm UTC](https://discuss.kotlinlang.org/t/problem-with-lazy-loading-and-lack-of-a-backing-field/1415/1 "2016-01-17T15:52:31Z")

</div>

Working with Entities in JPA I come across something like this:

```
@Entity
class Foo {
	@OneToMany(mappedBy = "foo")
	val bars : MutableList<Bar> by lazy { ArrayList<Bar>() }
}

```

This is a nice pattern such that when the Entity is new, it’ll create a new empty List for Bars, but the underlying framework will set the list (with field access) for existing Foos.

the problem is that kotlin generates the error:

> Kotlin: This annotation is not applicable to target ‘member property without backing field’

Not a big deal, but was wondering if there is a nice way to make this work.

---

<div class="post-metadata">

**Author:** ![cedricbeust](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cedricbeust/32/1429_2.png) [@cedricbeust](https://discuss.kotlinlang.org/u/cedricbeust)\
**Post date:** [January 18, 2016, 3:46am UTC](https://discuss.kotlinlang.org/t/problem-with-lazy-loading-and-lack-of-a-backing-field/1415/2 "2016-01-18T03:46:16Z")

</div>

Move your annotation to the getter:

```auto
    val bars : MutableList<Bar>
        @OneToMany(mappedBy = "foo")
        get() {
            return ArrayList<Bar>() 
        }

```

Note that the code above is not strictly equivalent to your original `lazy` code, though.

---

<div class="post-metadata">

**Author:** ![byron](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@byron](https://discuss.kotlinlang.org/u/byron)\
**Post date:** [January 18, 2016, 5:24am UTC](https://discuss.kotlinlang.org/t/problem-with-lazy-loading-and-lack-of-a-backing-field/1415/3 "2016-01-18T05:24:37Z")

</div>

Thanks for the response. Yea, I investigated your solution but the problem is that as soon as I implement the lazy loading in the get method the compiler is no longer willing to guarantee the getter will never be null, so the property type has to be changed to allow nulls, which I would like to avoid. I just settled on initializing the property directly with an empty array list, and accept the very minor overhead of an unnecessary empty array list creation in some circumstances. It would have been kinda lucky for this to work anyway since the JPA provider is fiddling with the fields underneath the hood, and the lazy delegate wasn’t really designed for this.

---

<div class="post-metadata">

**Author:** ![dico.karssiens](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@dico.karssiens](https://discuss.kotlinlang.org/u/dico.karssiens)\
**Post date:** [January 12, 2019, 2:15am UTC](https://discuss.kotlinlang.org/t/problem-with-lazy-loading-and-lack-of-a-backing-field/1415/4 "2019-01-12T02:15:24Z")

</div>

```auto
        @get:OneToMany(mappedBy = "foo")
	val bars : MutableList<Bar> by lazy { ArrayList<Bar>() }

```

---

<div class="post-metadata">

**Author:** ![JVamcas](https://avatars.discourse-cdn.com/v4/letter/j/8491ac/32.png) [@JVamcas](https://discuss.kotlinlang.org/u/JVamcas)\
**Post date:** [December 13, 2020, 9:30am UTC](https://discuss.kotlinlang.org/t/problem-with-lazy-loading-and-lack-of-a-backing-field/1415/5 "2020-12-13T09:30:35Z")

</div>

I did the same, but now hibernate won’t create the foreign key column.
