-->
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.  [ 2 posts ] 
Author Message
 Post subject: Mapping Strings in a Map
PostPosted: Sun Oct 08, 2006 8:51 pm 
Newbie

Joined: Sun Oct 08, 2006 8:27 pm
Posts: 2
Hi
I wish to map some custom attributes of an entity, stored in a Map<String,String>.

The entity is Customer and the map property is customAttributes. They should be stored in a second table:

customerid INTEGER,
name VARCHAR,
value VARCHAR,
Primary Key (customerid, name)

Is this possible with Hibernate Annotations Extentions or do I have to define a second Entity for the customAttributes?

I tried with a second Entity, but I cannot get it to work:

@Entity
public class Customer {
private Long id;
private String lastName;
private Map<String, CustomAttribute> customAttributes;

@OneToMany (cascade=CascadeType.ALL)
@JoinColumn (name="customer_id")
@MapKey (name="name")
public Map<String,CustomAttribute> getCustomAttributes() {
return customAttributes;
}
...
}

@Entity
public class CustomAttribute {
private String name;
private String value;

@Id
public String getName() {
return name;
}

...
}


I get an
IllegalArgumentException occurred calling getter of CustomAttribute.name
caused by:
java.lang.IllegalArgumentException: object is not an instance of declaring class

The second thing is that I don't know how to define the @Id in a correct way (should be composite). I would prefer a simple value mapping without the need for a second entity...

Thanks a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 09, 2006 4:05 pm 
Newbie

Joined: Sun Oct 08, 2006 8:27 pm
Posts: 2
I resolved that problem. The solution was to use @CollectionOfElements:

@CollectionOfElements (fetch=FetchType.EAGER)
@JoinTable(name="customattribute", joinColumns = @JoinColumn(name="customer_id"))
@MapKey (columns=@Column(name="name"))
@Column(name="value", nullable=true)
public Map<String,String> getCustomAttributes() {
return customAttributes;
}

No child entity is needed now.

I am posting it here because I found it quite hard to extract this from the manual, as there is no Map example for that case. Maybe it will be useful for someone.


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

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.