-->
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.  [ 5 posts ] 
Author Message
 Post subject: annotations and MAP mapping, column for map-key still empty
PostPosted: Tue Feb 05, 2008 6:59 am 
Beginner
Beginner

Joined: Mon Jan 07, 2008 4:13 am
Posts: 22
Hi,

i use hibernate-annotations and i can store to DB java.util.Map (java.util.HashMap), but there is problem with column in DB for map-key, this column is still empty, but all other columns are ok.

House.java:

Code:
@OneToMany(mappedBy = "house", fetch = FetchType.LAZY, cascade = CascadeType.ALL, targetEntity = com.xxx.model.HousePropertyValue.class)
@MapKey(name = "valueKey")
private Map<String, HousePropertyValue> housePropertyValues = new HashMap<String, HousePropertyValue>();

//get/set housePropertyValues


HousePropertyValue.java abstract class, DB table house_property_value, and value_key column for map-key, and value_type for discriminator

Code:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="value_type",
discriminatorType=DiscriminatorType.STRING
)
@Table(name="house_property_value")
@javax.persistence.SequenceGenerator(
name="seq_house_property_value",
sequenceName="s_house_property_value",
allocationSize=1
)
public abstract class HousePropertyValue implements Serializable {

@Column(name = "value_key")
private String valueKey;

@ManyToOne (fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "fk_house_id", referencedColumnName = "id")
private House house;

...
public String getValueKey() {
return valueKey;
}
public void setValueKey(String valueKey) {
this.valueKey = valueKey;
}

@Transient
public abstract Object getValue();
public abstract void setValue(Object value);
}


HousePropertyIntegerValue - implementation of HousePropertyValue :

Code:
@Entity
@DiscriminatorValue("INTEGER")
@javax.persistence.SequenceGenerator(
name="seq_house_property_value",
sequenceName="s_house_property_value",
allocationSize=1
)
public class HousePropertyIntegerValue extends HousePropertyValue {
@Column(name = "value_integer", nullable = false)
private Integer value;

public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
public void setValue(Object value) {
this.value = (Integer) value;
}
....
}


but value_key column in house_property_value table si still empty.

for example :
Code:
house.getHousePropertyValues().put("description", new HousePropertyIntegerValue(10));


please, where is the problem ?

hibernate configuration:
Code:
<hibernate-configuration>
    <session-factory>
class="com.xxx.model.HousePropertyValue"/>
        <mapping class="com.xxx.model.HousePropertyIntegerValue"/>
    </session-factory>
</hibernate-configuration>


thanks!

Ivan


Last edited by termi on Tue Feb 05, 2008 7:16 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 4:46 pm 
Beginner
Beginner

Joined: Mon Jan 07, 2008 4:13 am
Posts: 22
and versions:

Hibernate 3.2.5.ga
Hibernate annotations 3.3.0.ga


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 7:17 pm 
Beginner
Beginner

Joined: Mon Jan 07, 2008 4:13 am
Posts: 22
DB is PostgreSQL 8.2


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 06, 2008 11:52 am 
Beginner
Beginner

Joined: Mon Jan 07, 2008 4:13 am
Posts: 22
Hi!,

my solution is setup map-key property manually:

Code:
@OneToMany(mappedBy = "house",  fetch = FetchType.LAZY, cascade = CascadeType.ALL, targetEntity = com.xxx.model.HousePropertyValue.class)
@MapKey(name = "valueKey")
private Map<String, HousePropertyValue> housePropertyValues = new HashMap<String, HousePropertyValue>();

house.getHousePropertyValues().put("map-key1", new HousePropertyIntegerValue(10));

house.getHousePropertyValues().get("map-key1").setValueKey("map-key1");



but i don`t know, if in Hibernate, it is neccessary to set-up this property manually.

Ivan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 06, 2008 12:05 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
termi wrote:
but i don`t know, if in Hibernate, it is neccessary to set-up this property manually.

Ivan



Ivan, this is what hibernate document says:

Quote:
Be aware that once loaded, the key is no longer kept in sync with the property, in other words, if you change the property value, the key will not change automatically in your Java model (for true map support please refers to Hibernate Annotation Extensions). Many people confuse <map> capabilities and @MapKey ones. These are two different features. @MapKey still has some limitations, please check the forum or the JIRA tracking system for more informations.



so you will have to set the both values anyways.


Farzad-


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.