Hello, I have a scenario where I have the following structure.
Database
Table avocado_banana {
avocado_id, -- primaryKey
banana_id
}
When trying a bidirectional relationship in SecondaryTable, hibernate cannot perform the relationship through mappedBy. The entity that is declaring mappedBy is always loaded as null, even though the generated SQL brings all the necessary information to fill these relationships.
Below is an example of my structure
@Entity
@SecondaryTable(name = "avocado_banana", pkJoinColumns = @PrimaryKeyJoinColumn(name = "avocado_id"))
public abstract class Avocado extends AbstractEntity{
@OneToOne(fetch = FetchType.LAZY, targetEntity = AbstractBanana.class, cascade = CascadeType.ALL)
@JoinColumn(name = "banana_id", table = "avocado_banana")
private Banana banana;
}
@Entity
public abstract class Banana extends AbstractEntity{
@OneToOne(fetch = FetchType.LAZY, mappedBy = "banana", targetEntity = Avocado.class, optional = true)
@ToString.Exclude
private Avocado avocado; //always loads null
}
@Entity
@DiscriminatorValue("BANANA")
public class ConcreteBanana extends Banana {
//
}
@Entity
@DiscriminatorValue("AVOCADO")
public class ConcreteAvocado extends Avocado {
//
}
So, does anyone know of a similar problem?
Or where I could investigate further to understand why mappedBy would not be working correctly.
This problem started to occur when I migrated my application from spring 2.7.2 → spring 3.2.10
that is, hibernate 5.6.14.Final → 6.4.10.Final
Analyzing the generated SQL, the query returns all the fields necessary to successfully populate the mappedBy, but the object is always loaded as null.