[JIRA] (HHH-16969) An entity with a parent <-> child relationship on itself with a natural id does not load using it natural id
by Patrice CAVEZAN (JIRA)
Patrice CAVEZAN ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63d0ef2... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiN2I5NjhmOGRk... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16969?atlOrigin=eyJpIjoiN2I5Nj... ) HHH-16969 ( https://hibernate.atlassian.net/browse/HHH-16969?atlOrigin=eyJpIjoiN2I5Nj... ) An entity with a parent <-> child relationship on itself with a natural id does not load using it natural id ( https://hibernate.atlassian.net/browse/HHH-16969?atlOrigin=eyJpIjoiN2I5Nj... )
Issue Type: Bug Affects Versions: 6.2.6, 6.2.7 Assignee: Unassigned Components: hibernate-orm-modules Created: 21/Jul/2023 07:15 AM Priority: Major Reporter: Patrice CAVEZAN ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63d0ef2... )
Hi,
While upgrading our stack to spring boot 3 which brings hibernate 6, we just noticed that a feature does not work anymore. All other features (we used) works well so far.
The issue is about having an entity with a child ↔︎ parent relationship on itself (having a foreign key pointing to a primary key of another row in the same table). The problem occurs only using the ability offers by hibernate to load an entity (via hibernate session) by it natural id.
It seems Hibernate try to load recursively the entity attribute which point to itself to build the right SQL query, even if this attribute is mark as Lazy resulting in the end to a java.lang.StackOverflowError.
I reproduced the issue on simple project in my personal github account:
https://github.com/pcavezzan/parent-child-relationship-hibernate-6
I almost to forgot to mention but the project is in Kotlin ; but for me, it is not dued to Kotlin.
The entity, we’re trying to load looks like the following (you can see in the project):
@Entity
@Table(name = "person")
class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Int = 0
@NaturalId
@Column(name = "email", nullable = false, unique = true)
var email: String = ""
var name: String = ""
@ManyToOne(fetch = FetchType.LAZY, cascade = [CascadeType.REMOVE])
@JoinColumn(name = "parent_id")
var parent: Person? = null
@OneToMany(mappedBy = "parent")
var children: MutableSet<Person> = mutableSetOf()
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Person
return id == other.id
}
override fun hashCode(): Int {
return id
}
override fun toString(): String {
return "Person(id=$id, email='$email', name='$name')"
}
}
Thanks a lot in advance for your help on this.
( https://hibernate.atlassian.net/browse/HHH-16969#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16969#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100232- sha1:4329f54 )
2 years, 8 months