This Bugzilla instance is deprecated, and most Eclipse projects now use GitHub or Eclipse GitLab. Please see the deprecation plan for details.
Bug 300051 - MappedSuperclass with an EmbeddedId
Summary: MappedSuperclass with an EmbeddedId
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P2 major with 4 votes (vote)
Target Milestone: ---   Edit
Assignee: Michael OBrien CLA
QA Contact:
URL: http://wiki.eclipse.org/EclipseLink/D...
Whiteboard:
Keywords:
Depends on: 326317 266912
Blocks:
  Show dependency tree
 
Reported: 2010-01-19 09:50 EST by kurt CLA
Modified: 2022-06-09 10:07 EDT (History)
7 users (show)

See Also:


Attachments
Simple eclipse project reproducing this bug (5.12 KB, application/zip)
2010-03-08 10:35 EST, Ed Randall CLA
no flags Details
Simple eclipse project reproducing this bug (5.12 KB, application/octet-stream)
2010-03-08 10:38 EST, Ed Randall CLA
no flags Details
Preliminary patch illustrating option 1 of 2 for handling the case where we have an existing MAPPED_SUPERCLASS_RESERVED_PK_NAME (IdClass should require similar) (4.69 KB, patch)
2010-03-08 16:46 EST, Michael OBrien CLA
no flags Details | Diff
Option 2 patch for handling an EmbeddedId set directly on a MappedSuperclass (7.96 KB, patch)
2010-03-10 10:47 EST, Michael OBrien CLA
no flags Details | Diff
UML class diagram for extended test model (CPU(MS with @EmbeddedId) <-- MultiCoreCPU(E)) (8.00 KB, image/gif)
2010-03-10 17:13 EST, Michael OBrien CLA
no flags Details
JPA Metamodel Test model additions/modifications patch (21.13 KB, patch)
2010-03-10 17:14 EST, Michael OBrien CLA
no flags Details | Diff
10330 backport off 2.0.2 6872 (8.00 KB, patch)
2011-01-10 07:04 EST, Michael OBrien CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description kurt CLA 2010-01-19 09:50:32 EST
Build Identifier: GlassFish Tools Bundle For Eclipse    Version: 1.2  Based on Eclipse build id: M20090917-0800

Having an entity derived from a MappedSuperclass which contains a primarykeyclass does not work - results in following error:

Caused by: Exception [EclipseLink-7163] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Entity class [class bsbs.MSC3] has both an @EmbdeddedId (on attribute [id]) and an @Id (on attribute []. Both ID types cannot be specified on the same entity.

Note that the attribute name for the @Id-attribute is empty (which is right, because there is no @Id-attribute).

Testclasses will be attached.


Reproducible: Always

Steps to Reproduce:
1. create the classes (attached)
2. generate tables from entities
Comment 1 kurt CLA 2010-01-19 09:53:11 EST
here the classes to reproduce the behaviour:

package bsbs;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class TPK
implements Serializable {

private static final long serialVersionUID = 1L;

@Column(name = "INST", nullable = false, length = 2)
private String id;

@Column(name = "NL", nullable = false, length = 2)
private String subid;

public String getId() {
return id;
}

public void setId(final String id) {
this.id = id;
}

public String getSubid() {
return subid;
}

public void setSubid(final String subid) {
this.subid = subid;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((subid == null) ? 0 : subid.hashCode());
return result;
}

@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final TPK other = (TPK) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (subid == null) {
if (other.subid != null)
return false;
} else if (!subid.equals(other.subid))
return false;
return true;
}

}


package bsbs;

import java.io.Serializable;
import java.sql.Timestamp;

import javax.persistence.EmbeddedId;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

@MappedSuperclass
public class MSC3
implements Serializable {

private static final long serialVersionUID = 1L;

@Version
private Timestamp version;

@EmbeddedId
protected TPK id;

public MSC3() {
super();
}

public Timestamp getVersion() {
return version;
}

public TPK getId() {
return id;
}

public void setId(final TPK id) {
this.id = id;
}

}


package bsbs;

import javax.persistence.Entity;

@Entity
public class T3
extends MSC3 {

private static final long serialVersionUID = 1L;

private String name;

public T3() {
super();
}

public String getName() {
return this.name;
}

public void setName(final String name) {
this.name = name;
}

}
Comment 2 Tom Ware CLA 2010-01-21 16:04:29 EST
Setting target and priority.  For details of what this means see: http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines
Comment 3 Xavier Callejas CLA 2010-02-16 11:45:57 EST
+1

I do have the same problem (EclipseLink v2).

rgds.
xavier
Comment 4 Ed Randall CLA 2010-03-08 10:35:40 EST
Created attachment 161305 [details]
Simple eclipse project reproducing this bug

We are also experiencing this bug, reproducable against versions 1.2.0 and 2.0.1
Comment 5 Ed Randall CLA 2010-03-08 10:38:53 EST
Created attachment 161306 [details]
Simple eclipse project reproducing this bug

We are also experiencing this bug, reproducable against versions 1.2.0 and 2.0.1
Comment 6 Ed Randall CLA 2010-03-08 10:42:01 EST
Sorry about the duplicate attachment, seemed to be having problems uploading.
Comment 7 Michael OBrien CLA 2010-03-08 11:39:54 EST
>We have a composite EmbeddedId EmbeddedPK on the Entity parent of the MappedSuperclass CoordinateMS in our JPA test model
>However, we do not have an EmbeddedID directly on a MappedSuperclass (CoordinateMS)

>See UML class diagram
http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#Mapped_Superclass_Test_Model

>See Design Issue 100
http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_100:_20100120:_EmbeddedId_on_MappedSuperclass_fails_metadata_validation_on_reserved_temp_metamodel_PK
Comment 8 Michael OBrien CLA 2010-03-08 15:51:37 EST
>reproduction: move @EmbeddedId down from E:GalacticPosition to MS:CoordinateMS (It does not need to be composite)

    // Any reference to this embedded key requires a bidirectional relationship (not unidirectional)
    @EmbeddedId
    @Column(name="GALACTIC_ID")    
    protected EmbeddedPK primaryKey;

Exception Description: Entity class [class org.eclipse.persistence.testing.models.jpa.metamodel.CoordinateMS] has both an @EmbdeddedId (on attribute [primaryKey]) and an @Id (on attribute []. Both ID types cannot be specified on the same entity.
	at org.eclipse.persistence.exceptions.ValidationException.embeddedIdAndIdAnnotationFound(ValidationException.java:845)
	at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.EmbeddedIdAccessor.process(EmbeddedIdAccessor.java:154)
	at org.eclipse.persistence.internal.jpa.metadata.MetadataDescriptor.processAccessors(MetadataDescriptor.java:1271)
	at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor.processAccessors(ClassAccessor.java:825)
	at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.MappedSuperclassAccessor.processMetamodelDescriptor(MappedSuperclassAccessor.java:1088)
	at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage2(MetadataProject.java:1331)
	at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:461)
	at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:390)
	at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:947)


>Back in 2010 Jan - when we ran into this we decided on the following

See EmbeddedIdAccessor:154 - The fake table name inserted so that we can process metamodel types is leaking into metadataprocessing. 
Either skip validation for this reserved temporary PK or delay inserting it in metadata processing until after this step if not required for metamodel pre-processing.
Comment 9 Michael OBrien CLA 2010-03-08 16:00:24 EST
>This validation exception is occurring because metadata processing does not expect the fake table names
(previously we did not have descriptors for MappedSuperclasses - before we introduced the Metadata JPA 2.0 API)
>The fix will be to "not count" the fake ID's or either not add them in the presence of an IdClass or EmbeddedID
Comment 10 Michael OBrien CLA 2010-03-08 16:46:07 EST
Created attachment 161366 [details]
Preliminary patch illustrating option 1 of 2 for handling the case where we have an existing MAPPED_SUPERCLASS_RESERVED_PK_NAME (IdClass should require similar)



>Option 100-1: Ignore fake MappedSuperclass IDs
If we check the id for our fake MappedSuperclass id as follows 
if (owningDescriptor.hasPrimaryKeyFields() && 
                    !owningDescriptor.getPrimaryKeyField().getQualifiedName().equalsIgnoreCase(
                                MetadataConstants.MAPPED_SUPERCLASS_RESERVED_PK_NAME)) {
                    throw ValidationException.embeddedIdAndIdAnnotationFound(getJavaClass(), getAttributeName(), owningDescriptor.getIdAttributeName());
                }

We get the following instead of the validation ex 
cmpPolicy	CMP3Policy  (id=154)	
	descriptor	RelationalDescriptor  (id=124)	
	fieldToAccessorMap	null	
	forceUpdate	null	
	keyClassFields	null	
	mappedClass	null	
	modificationDeferralLevel	2	
	nonDeferredCreateTime	0	
	pessimisticLockingPolicy	null	
	pkClass	null	
	pkClassName	"org.eclipse.persistence.testing.models.jpa.metamodel.EmbeddedPK" (id=160)	
	updateAllFields	null


>Option 100-2: Do not add fake MappedSuperclass IDs when IdClass or EmbeddedId will exist
>Infeasible
At this point we do not have the Id accessors populated yet - to check 
if (!metadataDescriptor.hasIdAccessor()) {
                relationalDescriptor.addPrimaryKeyFieldName(MetadataConstants.MAPPED_SUPERCLASS_RESERVED_PK_NAME);
            }
 
m_idAccessors	HashMap<K,V>  (id=91)	
	entrySet	HashMap$EntrySet  (id=128)	
	size	0	
 
Thread [main] (Suspended (breakpoint at line 467 in MetadataProject))	
	MetadataProject.addMetamodelMappedSuperclass(MetadataClass, MappedSuperclassAccessor) line: 467	
	EntityAccessor.addPotentialMappedSuperclass(MetadataClass, boolean) line: 188	
	EntityAccessor.discoverMappedSuperclassesAndInheritanceParents(boolean) line: 300	
	EntityAccessor.preProcess(boolean) line: 539	
	EntityAccessor.preProcess() line: 522	
	MetadataProject.processStage1() line: 1303	
	MetadataProcessor.processORMMetadata() line: 460	
	PersistenceUnitProcessor.processORMetadata(MetadataProcessor, boolean) line: 390	
	EntityManagerSetupImpl.predeploy(PersistenceUnitInfo, Map) line: 947	
	JavaSECMPInitializer(JPAInitializer).callPredeploy(SEPersistenceUnitInfo, Map, PersistenceInitializationHelper, String, String) line: 88	
	JavaSECMPInitializer.initPersistenceUnits(Archive, Map, PersistenceInitializationHelper) line: 256	
	JavaSECMPInitializer.initialize(Map) line: 216

>Solution 100:
We will need to use option 1 - ignore validation exceptions when the existing Id is the temporary fake one for processing MappedSuperclasses 
MetadataConstants.MAPPED_SUPERCLASS_RESERVED_PK_NAME="__PK_METAMODEL_RESERVED_IN_MEM_ONLY_FIELD_NAME";
Comment 11 Michael OBrien CLA 2010-03-10 10:47:51 EST
Created attachment 161616 [details]
Option 2 patch for handling an EmbeddedId set directly on a MappedSuperclass

>See option 2 for the case where the EmbeddedId is set on a mappedSuperclass child of an Entity/MappedSuperclass.

http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#Option_100-2:_Do_not_add_fake_MappedSuperclass_IDs_when_IdClass_or_EmbeddedId_will_exist
Comment 12 Michael OBrien CLA 2010-03-10 17:13:27 EST
Created attachment 161683 [details]
UML class diagram for extended test model (CPU(MS with @EmbeddedId) <-- MultiCoreCPU(E))
Comment 13 Michael OBrien CLA 2010-03-10 17:14:46 EST
Created attachment 161684 [details]
JPA Metamodel Test model additions/modifications patch 

>code reviewed by Guy
>I'll check in tomorrow after the milestone build completes tonight
Comment 14 Michael OBrien CLA 2010-03-11 10:35:07 EST
>see SVN Rev# 6784
http://fisheye2.atlassian.com/changelog/eclipselink/?cs=6784
Comment 15 Michael OBrien CLA 2010-09-24 11:52:37 EDT
>This fix will solve the issue in GlassFish 13557 (in EclipseLink 2.0.1)
https://glassfish.dev.java.net/issues/show_bug.cgi?id=13557
Comment 17 Xavier Callejas CLA 2010-12-01 18:26:48 EST
Hi,

How can I use the fixed version in Glassfish v3.0.1 ? 

Please help.

Thank you.
Comment 18 Michael OBrien CLA 2011-01-05 11:48:29 EST
>Xavier, verify that the EclipseLink startup log on predeploy() reads post SVN rev# 6784
Comment 19 Michael OBrien CLA 2011-01-05 11:50:53 EST
10393900 for 2.0.x BP
Comment 20 Michael OBrien CLA 2011-01-10 07:04:47 EST
Created attachment 186380 [details]
10330 backport off 2.0.2 6872
Comment 21 Eclipse Webmaster CLA 2022-06-09 10:07:49 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink