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

Bug 398881

Summary: JOINED Inheritance fail when @Id on base class haves no @Column Mapping
Product: z_Archived Reporter: Eduardo Frazão <edufrazao>
Component: EclipselinkAssignee: Project Inbox <eclipselink.orm-inbox>
Status: CLOSED INVALID QA Contact:
Severity: major    
Priority: P3 CC: christopher.delahunt
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Whiteboard:
Attachments:
Description Flags
Related entities (Real use case) none

Description Eduardo Frazão CLA 2013-01-23 09:18:23 EST
Created attachment 225980 [details]
Related entities (Real use case)

I Have an entity class that servers as base from another, with JOINED Inheritance strategy, and another class exteding, with a @PrimaryKeyJoinColumn.

This base class haves on @OneToOne mapping as @Id, with @JoinColumn

This is the resume of mapping: 

--
@Entity
@Table(name="enti_empresa", schema="cadastro")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="tipo_empresa", discriminatorType=DiscriminatorType.CHAR, length=1)
@DiscriminatorValue("M")
public class Empresa implements DefaultEntity<PessoaJuridica> {

	private static final long serialVersionUID = 7175307548694472116L;
	
	@Id
	@OneToOne(fetch=FetchType.EAGER, optional=false)
	@JoinColumn(name="enti_empresa_pk_fk", referencedColumnName="pess_juridica_pk_fk", updatable=false, insertable=false)
	private PessoaJuridica id;
--

And this is the subclass:

--
@Entity
@Table(name="enti_filial", schema="cadastro")
@PrimaryKeyJoinColumn(name="enti_empr_filial_pk_fk", referencedColumnName="enti_empresa_pk_fk")
@DiscriminatorValue("F")
public class Filial extends Empresa {

	private static final long serialVersionUID = 4753041286089829659L;
--

On the entity manager setup phase, I got this error: 

[2013-01-23 12:07:18.603] ERROR http-bio-8080-exec-1         System.err                                                        Caused by: java.util.NoSuchElementException 
[2013-01-23 12:07:18.603] ERROR http-bio-8080-exec-1         System.err                                                        	at java.util.ArrayList$Itr.next(ArrayList.java:794) 
[2013-01-23 12:07:18.603] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.MetadataDescriptor.getPrimaryKeyField(MetadataDescriptor.java:991) 
[2013-01-23 12:07:18.603] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.MetadataDescriptor.getPrimaryKeyTable(MetadataDescriptor.java:1064) 
[2013-01-23 12:07:18.603] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.accessors.MetadataAccessor.getReferencedField(MetadataAccessor.java:498) 
[2013-01-23 12:07:18.603] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.accessors.MetadataAccessor.getReferencedField(MetadataAccessor.java:420) 
[2013-01-23 12:07:18.603] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.addMultipleTableKeyFields(EntityAccessor.java:201) 
[2013-01-23 12:07:18.603] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.processInheritancePrimaryKeyJoinColumns(EntityAccessor.java:1007) 
[2013-01-23 12:07:18.604] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.inheritance.InheritanceMetadata.processInheritanceSubclass(InheritanceMetadata.java:298) 
[2013-01-23 12:07:18.604] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.inheritance.InheritanceMetadata.process(InheritanceMetadata.java:233) 
[2013-01-23 12:07:18.604] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.processTableAndInheritance(EntityAccessor.java:1210) 
[2013-01-23 12:07:18.604] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.process(EntityAccessor.java:619) 
[2013-01-23 12:07:18.604] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage2(MetadataProject.java:1686) 
[2013-01-23 12:07:18.604] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:532) 
[2013-01-23 12:07:18.604] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:550) 
[2013-01-23 12:07:18.604] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1469) 


--

Ive attached all related classes on this bug
Comment 1 Chris Delahunt CLA 2013-02-12 10:16:33 EST
The mapping shown in the description does not match what is in the Empresa entity which is:
	@Id
	@OneToOne(fetch=FetchType.EAGER, optional=false)
	@JoinColumn(name="enti_empresa_pk_fk", referencedColumnName="pess_juridica_pk_fk", updatable=false, insertable=false)
	@PrimaryKeyJoinColumn(name="enti_empresa_pk_fk", referencedColumnName="pess_juridica_pk_fk")
	private PessoaJuridica id;

In this mapping, the @PrimaryKeyJoinColumn incorrectly duplicates what is defines in the JoinColumn definition.  Both of which are probably not exactly what is needed.

@PrimaryKeyJoinColumn is used mostly for secondary tables and joined inheritance to specify the foriegn key name in the second table.  When used on a mapping, it allows specifying that this Entity's primary key field is a foreign key to the target entities primary key.  This JPA 1.0 annotation essentially makes the mapping read-only, since it also means that another field within the current entity must map the ID field for it to be set.  It should not be used with JPA 2.0 when setting the relationship mapping to be the ID.

Try instead:

	@Id
	@OneToOne(fetch=FetchType.EAGER, optional=false)
	@JoinColumn(name="enti_empresa_pk_fk", referencedColumnName="pess_juridica_pk_fk")
	private PessoaJuridica id;

which will allow the enti_empresa_pk_fk field to be set with the value from PessoaJuridica and be used as the entity's Id.

Closing as this works for me.
Comment 2 Eclipse Webmaster CLA 2022-06-09 10:05:40 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink