Reputation: 417
I have a few OneToOne and ManyToOne relationships where I want to use the same primary key for every element, "chaining" the key from parent to children.
My model (MWE) has three entities: Grandparent, Parent, and Child. Grandparent and Parent are related One to One, while Child and Parent are related Many To One, with Child having a composite key with the related Parent as a member.
The implementations are as follows:
Grandparent:
@Entity
@Table(name = "GRANDPARENT")
public class Grandparent implements Serializable {
@Id
@Column(length = 20)
String grandparentId;
@OneToOne(mappedBy = "grandparent", cascade = CascadeType.ALL)
Parent parent;
}
Parent:
@Entity
@Table(name = "PARENT")
public class Parent implements Serializable {
@Id
@OneToOne
@JoinColumn(name = "grandparent")
Grandparent grandparent;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
List<Child> child;
}
Child:
@Entity
@IdClass(ChildId.class)
@Table(name = "CHILD")
public class Child implements Serializable {
@Id
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "parent", referencedColumnName = "grandparent", columnDefinition = "varchar(20)")
Parent parent;
@Id
@Column(length = 20)
String childStringId;
}
ChildId:
public class ChildId implements Serializable {
Grandparent parent; // should have the type of the Id of "parent", according to the JPA spec
String childStringId;
// hashCode and equals methods
...
}
This fails on deploy with the following exception:
14:03:52,579 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 85) MSC000001: Failed to start service jboss.persistenceunit."hibernateMWE.war#pu": org.jboss.msc.service.StartException in service jboss.persistenceunit."hibernateMWE.war#pu": java.util.NoSuchElementException
at [email protected]//org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:198)
at [email protected]//org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:128)
at [email protected]//org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:649)
at [email protected]//org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:212)
at [email protected]//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at [email protected]//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1982)
at [email protected]//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
at [email protected]//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
at java.base/java.lang.Thread.run(Thread.java:835)
at [email protected]//org.jboss.threads.JBossThread.run(JBossThread.java:485)
Caused by: java.util.NoSuchElementException
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:999)
at [email protected]//org.hibernate.internal.util.collections.JoinedIterator.next(JoinedIterator.java:47)
at [email protected]//org.hibernate.cfg.annotations.TableBinder.linkJoinColumnWithValueOverridingNameIfImplicit(TableBinder.java:724)
at [email protected]//org.hibernate.cfg.PkDrivenByDefaultMapsIdSecondPass.doSecondPass(PkDrivenByDefaultMapsIdSecondPass.java:37)
at [email protected]//org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1696)
at [email protected]//org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1653)
at [email protected]//org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:287)
at [email protected]//org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:904)
at [email protected]//org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:935)
at [email protected]//org.jboss.as.jpa.hibernate5.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44)
at [email protected]//org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:170)
... 9 more
14:03:52,580 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 2) WFLYCTL0013: Operation ("full-replace-deployment") failed - address: ([]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.persistenceunit.\"hibernateMWE.war#pu\"" => "java.util.NoSuchElementException
Caused by: java.util.NoSuchElementException"}}
If I change the type of the field "parent" in ChildId from Grandparent to Parent, the deploy fails with a MappingException: Unable to find column with logical name: grandparent in org.hibernate.mapping.Table(PARENT) and its related supertables and secondary tables
. If I change it to String instead, it deploys successfully, but persisting the entities raises an IllegalArgumentException: Can not set java.lang.String field entity.ChildId.parent to entity.Parent
.
I have also attempted to remove the JoinColumn annotation from Child, but this results in the following exception:
Caused by: org.hibernate.MappingException: Foreign key (FKdjmtwrjnm98ag78day8q0okub:CHILD [])) must have same number of columns as the referenced primary key (PARENT [grandparent])"}
I'm using Hibernate 5.3.10.Final on wildfly 17.0.1.Final, and my database is MariaDB 10.0.38. I've managed to this mapping with @MapsId, but I would rather use just @Ids for the sake of simplicity.
Upvotes: 0
Views: 284
Reputation: 3276
You might try mapping Parent
and Child
like this, leaving Grandparent
the way it is:
@Entity
@Table(name = "PARENT")
public class Parent implements Serializable {
@Id
String id;
@OneToOne
@JoinColumn(name = "grandparent")
@MapsId
Grandparent grandparent;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
List<Child> child;
}
@Entity
@IdClass(ChildId.class)
@Table(name = "CHILD")
public class Child implements Serializable {
@Id
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "parent", referencedColumnName = "grandparent", columnDefinition = "varchar(20)")
Parent parent;
@Id
@Column(length = 20)
String childStringId;
}
public class ChildId implements Serializable {
String parent; // should have the type of the Id of "parent", according to the JPA spec
String childStringId;
// hashCode and equals methods
...
}
Upvotes: 1