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

Bug 359895

Summary: MapKey table is incorrectly defaulted for Maps with JoinTables
Product: z_Archived Reporter: Tom Ware <tom.ware>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Tom Ware CLA 2011-10-04 14:39:07 EDT
At the moment, you need to specify the table name when using a map that uses a join table

Given:

@Entity
public class EnumKeyHolder {
    
    @GeneratedValue
    @Id
    private int id;
    
    @OneToMany( cascade=CascadeType.ALL )
    @MapKeyEnumerated(EnumType.STRING)
    @MapKeyColumn(name="mymapkeycolulmn")
    Map<MyEnum,EnumKeyProperty> properties = new HashMap<MyEnum,EnumKeyProperty>();

...

And

public enum MyEnum{
   
    VALUE1( "value1" ), // default
    VALUE2( "value2" )


...


@Entity
public class EnumKeyProperty {

    @Id
    private int myKey;
    
    private int myValue;


The following code will not put a value in the table for the map key:

    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestEnumeratedMapKey");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        EnumKeyHolder holder = new EnumKeyHolder();
        EnumKeyProperty prop = new EnumKeyProperty();
        prop.setMyKey(23);
        prop.setMyValue(22);
        holder.getProperties().put(MyEnum.VALUE1, prop);
        em.persist(holder);
        em.persist(prop);
        em.getTransaction().commit();
    }
Comment 1 Tom Ware CLA 2011-10-04 14:40:56 EDT
Workaround:

Specify the table:

    @OneToMany( cascade=CascadeType.ALL )
    @MapKeyEnumerated(EnumType.STRING)
    @MapKeyColumn(table="ENUMKEYHOLDER_ENUMKEYPROPERTY", name="mymapkeycolulmn")
    Map<MyEnum,EnumKeyProperty> properties = new HashMap<MyEnum,EnumKeyProperty>();

Possible fix:

OneToManyAccessor.processManyToManyMapping()

Move the processJoinTable call earlier in the method like this:

    protected void processManyToManyMapping() {
        // Create a M-M mapping and process common collection mapping metadata
        // first followed by specific metadata.
        ManyToManyMapping mapping = new ManyToManyMapping();
        
        // Process the JoinTable metadata.
        processJoinTable(mapping, mapping.getRelationTableMechanism(), getJoinTableMetadata());
        
        process(mapping);
        
        // 266912: If this 1:n accessor is different than the n:n mapping - track this
        mapping.setDefinedAsOneToManyMapping(true);
    }
Comment 2 Tom Ware CLA 2011-10-31 13:30:05 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 3 Eclipse Webmaster CLA 2022-06-09 10:30:23 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink