Hibernate version 2.1
Name and version of the database you are using: MySQL 4.1.3
Hi everyone,
I need to know how to write a mapping for the following problem:
- Every product in our application has multiple title objects, one per locale. This is stored in a map.
- These title objects themselves contain a map, every key (TitleType) is an object, however, for simplicity, this could be seen as a string. The value is also a string.
- Besides this, title objects are not only in products, but also in other classes. The titles from product should be stored in productTitles, while tiles from another class should be stored in <class>Titles, etc...
This structure enables us to define mutiple 'titles' in mutliple languages.
An example database (ProductTitles) could be:
Code:
productID | Locale | TitleType | Value
1 en title Beatiful product
1 en desc This is a nice product
2 nl desc Dit is een leuk product
Creating a class that is a composite key was a thought, however, since title will be used a lot, it would be good to go easy on the key-objects, since many of these would have to be loaded into memory.
Can someone show me some hibernate mapping xml how this could be done. I figured I could need to build a component in a component or something, but its not getting any clearer for me.
Thanks in advance,
Vincent
Some codeproduct.hbm.xml
Code:
<hibernate-mapping package="test">
<class name="Product">
<id name="id" column="ProductID">
<generator class="native"/>
</id>
<property name="code" length="16"/>
<map name="titles">
<!-- ... help !!!! -->
</map>
<property name="price" type="test.persistence.PriceCompositeUserType">
<column name="priceValue" not-null="true"/>
<column name="priceCurrency" not-null="true" length="3"/>
<column name="priceQtyAmount" not-null="true"/>
<column name="priceQtyUnit" not-null="true" length="16"/>
</property>
</class>
</hibernate-mapping>
Product.java
Code:
...
private Map _titles;
/**
* Set the title map.
*
* See {@link TitleDescriptor}
**/
public void setTitles(Map titles) {
_titles = titles;
}
/**
* Get the title hashmap. By default a hashmap is returned, though
*
* See {@link TitleDescriptor}
**/
public Map getTitles() {
return _titles;
}
...
TitleDescriptor.java
Code:
..
public class TitleDescriptor {
// Locale (language) in which the titles should be supplied in
private final Locale _locale;
// Mapping of title types to titles, i.e. Map <Title, String>
private final Map _titles;
/** Creates a new TitleDescriptor with the supplied Locale. */
public TitleDescriptor(Locale locale) {
_locale = locale;
_titles = new HashMap();
}
/** Retrieves the Locale. */
public Locale getLocale() {
return _locale;
}
/** Returns the corresponding title for the supplied variant. */
public String getTitle(TitleVariant variant) {
return (String) _titles.get(variant);
}
..