Bug 359895 - MapKey table is incorrectly defaulted for Maps with JoinTables
Summary: MapKey table is incorrectly defaulted for Maps with JoinTables
Status: NEW
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-10-04 14:39 EDT by Tom Ware CLA
Modified: 2022-06-09 10:30 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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