0

I have three entities. Device, DeviceSetting, and DeviceState. Device has a non-null OneToMany relationship mapping to DeviceSetting called settings. DeviceSetting has a nullable ManyToOne relationship to DeviceState called state. In the database this is referred to by a foreign key in the Device_Setting table called Device_State_id. This foreign key can sometimes be null as this field is not used at times.

For clarity...

Device.settings -> [DeviceSetting].state -> DeviceState

I'm trying to represent this within my entity objects. However I keep getting an empty List of settings in my Device object whenever a DeviceSetting has a null foreign key pointing to DeviceState. Here's the particular code of DeviceSetting in question...

@DBObjectGet(table="Device_State", tableColumn="id", fieldForColumn="Device_State_id")
@ManyToOne(cascade = CascadeType.PERSIST, fetch=FetchType.LAZY, optional = true)
@JoinFetch(JoinFetchType.INNER)
@JoinColumn(columnDefinition="integer", name = "Device_State_id", nullable = true)
public DeviceState              state;

As you can see here I've tried using the optional and nullable fields in the annotation to note that this relationship is potentially a null relationship. My expectation is that this state field would then be null with in the DeviceState object. However, again, what happens instead is that the parent Device object has an empty List of settings.

If I add the @Transient annotation to the DeviceState state field then everything works. But obviously if the foreign key is not-null then I would want this state object to be filled out. So this isn't a solution.

I believe I'm misunderstanding the use of the optional/nullable flags? Welcome any thoughts.

For reference, I'm using EclipseLink 2.7.2.

2
  • 1
    Remove the JoinFecth annotation.
    – JB Nizet
    Aug 17, 2018 at 19:03
  • You're amazing.. Thank you JB Nizet!
    – Etep
    Aug 17, 2018 at 19:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.