These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Map of embeddables, the key being a property of the value
PostPosted: Wed Nov 30, 2011 12:00 pm 
Newbie

Joined: Thu Feb 09, 2006 11:52 am
Posts: 2
I am mapping a Map of embeddable objects, where the (compound) map key constitutes a property of the corresponding value

Here is the mapping:

Map owner

Code:
@Entity
@Access(value = AccessType.FIELD)
@Table(name = "map_owner")
public class MapOwner implements Identifiable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "map_owner_values", joinColumns = @JoinColumn(name = "owner", nullable = false, insertable = true, updatable = false))
    @Fetch(FetchMode.JOIN)
    @ForeignKey(name = "fk_map_owner")
    private Map<Key, ValueObject> map = new HashMap<Key, ValueObject>();

    public Long getId() {

        return this.id;
    }

    public Map<Key, ValueObject> getMap() {

        return this.map;
    }
}


Key

Code:
@Embeddable
@Access(AccessType.FIELD)
public class Key implements Comparable<Key> {

    @Column(name = "key_id")
    private Integer id;

    @Column(name = "key_name")
    private String name;

    public Key() {

        // for hibernate
    }

    public Key(Integer traderId, String articleNo) {

        this.id = traderId;
        this.name = articleNo;
    }


    @Column(name = "key_id", nullable = false, updatable = false)
    public Integer getId() {

        return id;
    }

    @Column(name = "key_name", nullable = false, updatable = false)
    public String getName() {

        return name;
    }
   
    // compareTo, equals and hashCode omitted

}


Value

Code:
@Embeddable
@Access(AccessType.FIELD)
public class ValueObject {

    @Transient
    private Key key;

    @Column(name = "value")
    private Double value;

    public ValueObject() {

        // Hibernate
    }

    public ValueObject(Integer keyId, String keyName, Double value) {

        this(new Key(keyId, keyName), value);
    }

    public ValueObject(Key key, Double value) {

        this.key = key;
        this.value = value;
    }

    public Key getKey() {

        return this.key;
    }

    public Double getValue() {

        return this.value;
    }
}


This mapping works.

Is there a possibility (a workaround, maybe) to retain a (non-transient) key property on the value object? With the given mapping, having a non-transient key property results in an exception (repeated column mapping).

Thank you in advance.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.