Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Primary key foreign key entity relationship issue

Hello,

The exception is occurring because the object model is not complete. The specification requires that composite primary keys use an IdClass or an EmbeddedId so that there is a key class containing all fields. By removing the EmbeddedId, you have removed your pk class - you will need to specify the @IdClass to use instead. Also, using derrivedIds means that the child class must incorporate its parent id into its own Id. That means if A has a composite PK, B's ID must also include that composite pk class as well as any additional fields it uses. I cannot determine which class in your tree is involved in the problem, but a simple example would be

@Entity
class A {

	@Id
	@Column(name="A_ID")
	@GeneratedValue(strategy = GenerationType.AUTO)
	private String aId;
}

@Entity
@IDClass(BID.class)
class B {

	@Column(name="B_ID")
	@Id
	private int bId;
	
	
	@Id
   @ManyToOne(cascade = ALL)
	@JoinColumn(name="A_ID", referencedColumnName="A_ID")
	@XmlTransient
	private A a;
}

class BID{
 int bId; //same type and name as the id from B
 String a;//must be the same type as A's id, and the attribute must use the name of the relationship from B
}

If A has a composite pk and uses an embeddable or pk class, B would look the same (except for additional join columns of course) but BID might look like:

class BID{
 int bId;
AID a; //the type is the same as A's idclass/embeddedid class. }

The specification has some examples at:
 http://jcp.org/en/jsr/detail?id=317

Best Regards,
Chris

luke2010 wrote:
I am using EclipseLink 2.0.1. the error i mentioned is shown up in eclipse
IDE, if I ignore this error, and continue running my testcase, it will throw
error like below:

==========================

junit.framework.AssertionFailedError: Exception in constructor: test1 (Local
Exception Stack: Exception [EclipseLink-30005] (Eclipse Persistence Services -
2.0.1.v20100213-r6600):
org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for
persistence archives with ClassLoader:
sun.misc.Launcher$AppClassLoader@1aa6b1
Internal Exception: javax.persistence.PersistenceException: Exception
[EclipseLink-28018] (Eclipse Persistence Services - 2.0.1.v20100213-r6600):
org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [brancoch] failed.
Internal Exception: java.lang.ClassCastException:
org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.IdAccessor
	at
org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:126)
	at
org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:133)
	at
org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:65)
	at
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
	at
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)
	at
ca.gc.rcmp.rtid.entities.test.TestAfisSubject.<init>(TestAfisSubject.java:31)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at junit.framework.TestSuite.createTest(TestSuite.java:61)
	at junit.framework.TestSuite.addTestMethod(TestSuite.java:283)
	at junit.framework.TestSuite.<init>(TestSuite.java:146)
	at
org.junit.internal.runners.JUnit38ClassRunner.<init>(JUnit38ClassRunner.java:67)
	at
org.junit.internal.builders.JUnit3Builder.runnerForClass(JUnit3Builder.java:14)
	at
org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
	at
org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
	at
org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
	at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
	at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:29)
	at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
	at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:40)
	at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:30)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: javax.persistence.PersistenceException: Exception
[EclipseLink-28018] (Eclipse Persistence Services - 2.0.1.v20100213-r6600):
org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [brancoch] failed.
Internal Exception: java.lang.ClassCastException:
org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.IdAccessor
	at
org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:991)
	at
org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.callPredeploy(JPAInitializer.java:88)
	at
org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:124)
	... 25 more
Caused by: Exception [EclipseLink-28018] (Eclipse Persistence Services -
2.0.1.v20100213-r6600):
org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [brancoch] failed.
Internal Exception: java.lang.ClassCastException:
org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.IdAccessor
	at
org.eclipse.persistence.exceptions.EntityManagerSetupException.predeployFailed(EntityManagerSetupException.java:210)
	... 28 more
Caused by: java.lang.ClassCastException:
org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.IdAccessor
	at
org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ObjectAccessor.processId(ObjectAccessor.java:369)
	at
org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ObjectAccessor.processOwningMappingKeys(ObjectAccessor.java:595)
	at
org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.OneToOneAccessor.process(OneToOneAccessor.java:158)
	at
org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.RelationshipAccessor.processRelationship(RelationshipAccessor.java:546)
	at
org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor.processDerivedId(ClassAccessor.java:969)
	at
org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.processDerivedId(EntityAccessor.java:804)
	at
org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processAccessorsWithDerivedIDs(MetadataProject.java:1009)
	at
org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage3(MetadataProject.java:1352)
	at
org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:462)
	at
org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:390)
	at
org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:945)
	... 27 more
)
	at junit.framework.Assert.fail(Assert.java:47)
	at junit.framework.TestSuite$1.runTest(TestSuite.java:97)
	at junit.framework.TestCase.runBare(TestCase.java:134)
	at junit.framework.TestResult$1.protect(TestResult.java:110)
	at junit.framework.TestResult.runProtected(TestResult.java:128)
	at junit.framework.TestResult.run(TestResult.java:113)
	at junit.framework.TestCase.run(TestCase.java:124)
	at junit.framework.TestSuite.runTest(TestSuite.java:232)
	at junit.framework.TestSuite.run(TestSuite.java:227)
	at
org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
	at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

===========================================

The error I mentioned is
christopher delahunt wrote:
What version are you using?  Can you provide the error and the stack
trace?

Regards,
Chris

luke2010 wrote:
In my case under eclipseLink 2.0, there are A, B  and C entity class.  A
has
1:M relationship to B, B has 1:1 relationship to C.  A has PK aId, B has
PK
aId, bId, C has PK aId and bId, aId is part of PK of B and C, and also FK
to
A.
@Entity
class A {

	@Id
	@Column(name="A_ID")
	@GeneratedValue(strategy = GenerationType.AUTO)
	private String aId;
	
	@OneToMany( mappedBy="a",  cascade = ALL, targetEntity=B.class)
	@PrivateOwned
	private Set bs;
}

1. why mappedBy="a" is always complaining that Attribute named "a" has
invalid mapping for this relationship. If I removed the @Id in class B
for
@ManyToOne, this complain will disappear.
@Entity
class B {

	@Column(name="B_ID")
	@Id
	private int bId;
	
	
    @ManyToOne(cascade = ALL)
	@JoinColumn(name="A_ID", referencedColumnName="A_ID")
	@Id
	@XmlTransient
	private A a;
	
	
	@OneToOne(optional=false, mappedBy="b", cascade = ALL, targetEntity =
C.class)
    @PrivateOwned
    private C c;
}


1. why mappedBy="b" is always complaining that Attribute named "b" has
invalid mapping for this relationship. If I removed the @Id in class C
for
@ManyToOne, this complain will disappear. But it will compain other thing
"Entity "C" has no Id or EmbeddedId"

@Entity
Class C{

	@OneToOne(optional = false)
	@JoinColumns({
		@JoinColumn(name="A_ID", referencedColumnName="A_ID", insertable =
false,
updatable = false),
		@JoinColumn(name="B_ID", referencedColumnName="B_ID", insertable =
false,
updatable = false),
		})
    @XmlTransient		
    @Id
	private B b;
}


_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users





Back to the top