-->
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: HashMap Object or compound keyMap
PostPosted: Fri Aug 04, 2006 4:00 pm 
Newbie

Joined: Fri Aug 04, 2006 3:45 pm
Posts: 4
Location: Chile
Hi, is there a way to define a compund key as the keyMap for a Hashmap?

Here is my test code.

User Object
Code:
@Entity
@Table(name="TEST_USER")
public class User {
   
   private int id;
   private String name;
   private UserEnum type;
   private Map<AttributeInstance,UserAttribute> attributes=new HashMap<AttributeInstance,UserAttribute>();
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO, generator="USER_GENERATOR")
   @SequenceGenerator(name="USER_GENERATOR", sequenceName="USER_SEQUENCE")
   @Column(name="ID")
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   @Column(name="name")
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   @Enumerated(EnumType.STRING)
   @Column(name="type")
   public UserEnum getType() {
      return type;
   }
   public void setType(UserEnum type) {
      this.type = type;
   }
   /**
    * @return the attributes
    */
   @OneToMany(cascade=CascadeType.ALL)
   @JoinColumn(name="USER_ID")
   @MapKey(name="attributeInstance")
   public Map<AttributeInstance, UserAttribute> getAttributes() {
      return attributes;
   }
   /**
    * @param attributes the attributes to set
    */
   public void setAttributes(Map<AttributeInstance, UserAttribute> attributes) {
      this.attributes = attributes;
   }

   public Object getAttribute(String name, int index) {
      return attributes.get(new AttributeInstance(name, index));
   }
}


UserAttribute Class
Code:
@Entity
@Table(name="USER_ATTRIBUTE")
public class UserAttribute {

   private Integer id;
   private String value;
   private AttributeInstance attributeInstance=new AttributeInstance();
   /**
    * @return the id
    */
   @Id
   @Column(name="ID")
   @GeneratedValue(strategy=GenerationType.AUTO, generator="USERATT_GENERATOR")
   @SequenceGenerator(name="USERATT_GENERATOR", sequenceName="USERATT_SEQUENCE")
   public Integer getId() {
      return id;
   }
   /**
    * @param id the id to set
    */
   public void setId(Integer id) {
      this.id = id;
   }
   /**
    * @return the name
    */
   public String getName() {
      return attributeInstance.name;
   }
   /**
    * @param name the name to set
    */
   public void setName(String name) {
      this.attributeInstance.name = name;
   }
   /**
    * @return the value
    */
   public String getValue() {
      return value;
   }
   /**
    * @param value the value to set
    */
   public void setValue(String value) {
      this.value = value;
   }
   public AttributeInstance getAttributeInstance() {
      return attributeInstance;
   }
   /**
    * @param attributeInstance the attributeInstance to set
    */
   public void setAttributeInstance(AttributeInstance attributeInstance) {
      this.attributeInstance = attributeInstance;
   }
   /**
    * @return the index
    */
   public int getIdx() {
      return attributeInstance.index;
   }
   /**
    * @param index the index to set
    */
   public void setIdx(int index) {
      this.attributeInstance.index = index;
   }
   
}


AtrributeInstance class contains name and index key. This properties identify an attribute.

I need to get a HashMap for user attributes indexed by pair (name,index). The idea is to have in one attribute name several values referenced by index property.

Thanks in advance.
Agustín Almonte


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.