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

Bug 315915

Summary: Incorrect canonical metamodel generation for BasicMap and BasicCollection
Product: [WebTools] Dali JPA Tools Reporter: Doug Clarke <douglas.clarke>
Component: GeneralAssignee: Karen Butzke <karenfbutzke>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P2 CC: brian.vosburgh, guy.pelletier, karenfbutzke, neil.hauge, peter.krogh
Version: 3.0Flags: neil.hauge: review+
Target Milestone: 2.3.1   
Hardware: All   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Reproduction Project
none
proposed patch against head none

Description Doug Clarke CLA 2010-06-06 18:40:13 EDT
When mapping a class with a BasicCollection mapping as:

@Entity
public class Employee {

    @Id
    @Column(name = "EMP_ID")
    private int id;

    @Column(name = "F_NAME")
    private String firstName;

    @Column(name = "L_NAME")
    private String lastName;

    @BasicCollection(valueColumn = @Column(name = "RESPONS_DESC"))
    @CollectionTable(name = "RESPONS")
    private List<String> responsibilities;

    
The Dali JPA 2.0 model gen generates:

@Generated(value="Dali", date="2010-06-06T15:16:16.703-0400")
@StaticMetamodel(Employee.class)
public class Employee_ {
	public static volatile SingularAttribute<Employee, Integer> id;
	public static volatile SingularAttribute<Employee, String> firstName;
	public static volatile SingularAttribute<Employee, String> lastName;
	public static volatile SingularAttribute<Employee, List> responsibilities;
}

When invoking getMetamodel I get the following ValidationException

[EL Config]: Connected: jdbc:oracle:thin:@localhost:1521:ORCL
	User: SCOTT
	Database: Oracle  Version: Oracle Database 11g Release 11.1.0.0.0 - Production
	Driver: Oracle JDBC driver  Version: 11.1.0.6.0-Production
[EL Info]: basic-collection-example login successful
Exception in thread "main" Local Exception Stack: 
Exception [EclipseLink-7215] (Eclipse Persistence Services - @VERSION@.@QUALIFIER@): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Could not load the field named [responsibilities] on the class [class model.Employee_]. Ensure there is a corresponding field with that name defined on the class.
Internal Exception: java.lang.IllegalArgumentException: Can not set static javax.persistence.metamodel.SingularAttribute field model.Employee_.responsibilities to org.eclipse.persistence.internal.jpa.metamodel.ListAttributeImpl
	at org.eclipse.persistence.exceptions.ValidationException.invalidFieldForClass(ValidationException.java:2611)
	at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.initializeCanonicalMetamodel(EntityManagerSetupImpl.java:2030)
	at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.getMetamodel(EntityManagerSetupImpl.java:1998)
	at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getMetamodel(EntityManagerFactoryImpl.java:495)
	at test.Main.main(Main.java:18)
Caused by: java.lang.IllegalArgumentException: Can not set static javax.persistence.metamodel.SingularAttribute field model.Employee_.responsibilities to org.eclipse.persistence.internal.jpa.metamodel.ListAttributeImpl
	at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
	at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
	at sun.reflect.UnsafeQualifiedStaticObjectFieldAccessorImpl.set(Unknown Source)
	at java.lang.reflect.Field.set(Unknown Source)
	at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.initializeCanonicalMetamodel(EntityManagerSetupImpl.java:2026)
	... 3 more

I am not sure if the error is in EclipseLink's support for its own advanced mappings when using the Metamodel or if Dali is incorrectly generating the canonical model.
Comment 1 Doug Clarke CLA 2010-06-06 18:42:20 EDT
Created attachment 171224 [details]
Reproduction Project

Built using latest JavaEE Helios RC
Comment 2 Doug Clarke CLA 2010-06-06 19:15:47 EDT
To work-around this issue I disabled Dali's model-gen and changed the SingularAttribute for responsibilities to a ListAttribute as:

@Generated(value="Dali", date="2010-06-06T19:10:57.187-0400")
@StaticMetamodel(Employee.class)
public class Employee_ {
	public static volatile SingularAttribute<Employee, Integer> id;
	public static volatile SingularAttribute<Employee, String> firstName;
	public static volatile SingularAttribute<Employee, String> lastName;
	public static volatile ListAttribute<Employee, List<String>> responsibilities;
}

I was unable to get the EclipseLink annottaion processor running on this project to compare generated models. I am assigning this to Dali for now but would like to verify that the ElipseLInk model gen is correct or possibly release note the issue for 2.1.
Comment 3 Doug Clarke CLA 2010-06-06 19:19:35 EDT
I figured out my PEBKAC issue with the EL model-gen. Its generated model class does not include the BasicCollection mapping:

@Generated("EclipseLink-2.1.0.v20100529-r7411 @ Sun Jun 06 19:18:03 EDT 2010")
@StaticMetamodel(Employee.class)
public class Employee_ { 

    public static volatile SingularAttribute<Employee, Integer> id;
    public static volatile SingularAttribute<Employee, String> lastName;
    public static volatile SingularAttribute<Employee, String> firstName;

}
Comment 4 Brian Vosburgh CLA 2010-06-07 11:02:24 EDT
Note that, instead of disabling Dali's metamodel generation, you could simply remove the @Generated annotation on the Employee_ class and Dali will continue to generate all the other classes and leave the Employee_ class untouched. (At least that's the hope. :-) ) Of course, you will need to sync Employee_ manually whenever you change Employee.
Comment 5 Doug Clarke CLA 2010-06-07 19:01:24 EDT
I changed the BasicCollection mapping to an ElementCollection mapping to resolve the issue. While this is a minimal impact issue it may be tough to diagnose. Dali incorrectly generates the meta model and EclipseLink throws a cryptic error initializing it.

We should reinforce the deprecated mappings and mention a potential issue using them with the JPA 2.0 meta model with Dali.
Comment 6 Karen Butzke CLA 2010-06-18 16:20:24 EDT
Doug/Guy, have you opened a corresponding bug against the EclipseLink metamodel generator since it does not generate a metamodel attribute for basic-collection?
Comment 7 Karen Butzke CLA 2010-06-18 16:21:31 EDT
Created attachment 172256 [details]
proposed patch against head

This patch fixes the Dali metamodel generation for BasicCollection and BasicMap in both java and orm.
Comment 8 Guy Pelletier CLA 2010-06-21 13:37:31 EDT
I just tried this on the EclipseLink metamodel generator (off trunk) and it generates just fine. The EclipseLink metamodel has always be generated against the mappings (returned from metadata processing) so I would be very surprised to see it omit BasicCollection and BasicMap mappings.
Comment 9 Karen Butzke CLA 2010-06-24 17:33:41 EDT
checked in to HEAD for inclusion in 2.3.1 maintenance
Comment 10 Karen Butzke CLA 2010-07-20 10:53:16 EDT
verified fixed in build wtp-sdk-M-3.2.1-20100717062943