Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 339594

Summary: Unidirectional OneToMany with single join column to target with composite fails
Product: z_Archived Reporter: Shaun Smith <shaun.smith>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: CLOSED INVALID QA Contact:
Severity: normal    
Priority: P2 CC: dazeydev.3, mail, tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Patch for deactivating the verification that all primaryKeyColumns must be used in OneToMany-Associations none

Description Shaun Smith CLA 2011-03-10 15:48:08 EST
Situation: Undirectional OneToMany from single PK parent to composite PK children.

A unidirectional OneToMany without join table and without ManyToOne back which is joined only on the source Entity id throws a validation error during JPA metatdata parsing (below).  

Mapping the relationship in a customizer works fine (below).

@Entity
public class Parent  {

	@Id
	private int id;

	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
	@JoinColumn(name="id", referencedColumnName="id_1")
	private List<Child> children;


....


@Entity
@IdClass(ChildPK.class)
public class Child {

	@Id
	private int id_1;
	@Id
	private int id_2;
...

	public void customize(ClassDescriptor descriptor) throws Exception {
		OneToManyMapping oneToManyMapping = new OneToManyMapping();
		oneToManyMapping.setCascadeAll(true);
		oneToManyMapping.dontUseIndirection(); // EAGER
		oneToManyMapping.setAttributeName("children");
		oneToManyMapping.setReferenceClass(Child.class);
		oneToManyMapping.setTargetForeignKeyFieldName("ID_1");
		Vector<String> sourceKeyFields = new Vector<String>();
		sourceKeyFields.add("ID");
		oneToManyMapping.setSourceKeyFieldNames(sourceKeyFields);
		descriptor.addMapping(oneToManyMapping);
	}

..

[EL Config]: The access type for the persistent class [class model.Parent] is set to [FIELD].
[EL Config]: The target entity (reference) class for the one to many mapping element [field children] is being defaulted to: class model.Child.
[EL Config]: The access type for the persistent class [class model.Child] is set to [FIELD].
[EL Config]: The alias name for the entity class [class model.Parent] is being defaulted to: Parent.
[EL Config]: The table name for entity [class model.Parent] is being defaulted to: PARENT.
[EL Config]: The column name for element [field id] is being defaulted to: ID.
[EL Config]: The alias name for the entity class [class model.Child] is being defaulted to: Child.
[EL Config]: The table name for entity [class model.Child] is being defaulted to: CHILD.
[EL Config]: The column name for element [field id_2] is being defaulted to: ID_2.
[EL Config]: The column name for element [field id_1] is being defaulted to: ID_1.
Exception in thread "main" Local Exception Stack: 
Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.2.0.v20110107-r8783): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@11b86e7
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.2.0.v20110107-r8783): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [example] failed.
Internal Exception: Exception [EclipseLink-7220] (Eclipse Persistence Services - 2.2.0.v20110107-r8783): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The @JoinColumns on the annotated element [field children] from the entity class [class model.Parent] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.
	at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:126)
	at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:105)
	at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
	at org.eclipse.jpt.eclipselink.core.ddlgen.Main.execute(Main.java:76)
	at org.eclipse.jpt.eclipselink.core.ddlgen.Main.main(Main.java:64)
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.2.0.v20110107-r8783): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [example] failed.
Internal Exception: Exception [EclipseLink-7220] (Eclipse Persistence Services - 2.2.0.v20110107-r8783): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The @JoinColumns on the annotated element [field children] from the entity class [class model.Parent] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.
	at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1127)
	at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.callPredeploy(JPAInitializer.java:101)
	at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:96)
	... 3 more
Caused by: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.2.0.v20110107-r8783): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [example] failed.
Internal Exception: Exception [EclipseLink-7220] (Eclipse Persistence Services - 2.2.0.v20110107-r8783): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The @JoinColumns on the annotated element [field children] from the entity class [class model.Parent] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.
	at org.eclipse.persistence.exceptions.EntityManagerSetupException.predeployFailed(EntityManagerSetupException.java:210)
	... 6 more
Caused by: Exception [EclipseLink-7220] (Eclipse Persistence Services - 2.2.0.v20110107-r8783): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The @JoinColumns on the annotated element [field children] from the entity class [class model.Parent] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.
	at org.eclipse.persistence.exceptions.ValidationException.incompleteJoinColumnsSpecified(ValidationException.java:1777)
	at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappingAccessor.getJoinColumnsAndValidate(MappingAccessor.java:566)
	at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappingAccessor.getJoinColumns(MappingAccessor.java:516)
	at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.OneToManyAccessor.processUnidirectionalOneToManyMapping(OneToManyAccessor.java:244)
	at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.OneToManyAccessor.process(OneToManyAccessor.java:134)
	at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processOwningRelationshipAccessors(MetadataProject.java:1350)
	at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage3(MetadataProject.java:1572)
	at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:486)
	at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:454)
	at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1081)
	... 5 more
Comment 1 Tom Ware CLA 2011-03-18 09:46:26 EDT
Setting target and priority.  See the following page for the meanings of these fields:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines

Community: Please vote for this bug if it is important to you.  Votes are one of the main criteria we use to determine which bugs to fix next.
Comment 2 Tobias Kahl CLA 2016-07-05 01:38:14 EDT
Created attachment 262905 [details]
Patch for deactivating the verification that all primaryKeyColumns must be used in OneToMany-Associations
Comment 3 Will Dazey CLA 2020-04-09 14:00:42 EDT
Closing as invalid test

According to the JPA specification, section 11.1.25 JoinColumn Annotation:

```
referencedColumnName (Optional) : ... When used with a unidirectional OneToMany foreign key mapping, the referenced column is in the table of the source entity. ...
```

The referenceColumnName being set here is "id_1", which is the ID field for the TARGET entity (Child), not the SOURCE entity (Parent). The name and referenceColumnName are backwards and invalid for a OneToMany unidirectional mapping.
Comment 4 Eclipse Webmaster CLA 2022-06-09 10:33:33 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink