can anyone help me with how to do this mapping? thanks in advance.
table 1:
categories
Code:
categories_id categories_image parent_id sort_order date_added last_modified
------------- ---------------- ----------- ---------- ----------- --------------
1 test image 0 1
2 0 2
3 0 3
4 0 4
5 0 5
10 1
11 1
12 1
13 1
14 1
table 2: categories_description:
Code:
categories_id language_id categories_name
------------- ----------- --------------------------------
0 1 Watercolors on Paper
1 1 Shop by Medium
2 1 Shop by Subject
3 1 Shop by Style
4 1 Shop by Decoration
5 1 Shop by Artist
10 1 Acrylics on Canvas
11 1 Charcoals and Pencils
12 1 Gouaches on Paper
13 1 Oils on Canvas
0 2 A Name in another language
Two Java classes:
Category.javaCode:
package com.mydomain.domain;
import java.util.Date;
import java.util.Map;
import java.util.Set;
public class Category extends BaseEntity {
private int parentId;
private int sortOrder;
private String image;
private Date addedDate;
private Date modifiedDate;
private Map description;
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
public int getSortOrder() {
return sortOrder;
}
public void setSortOrder(int sortOrder) {
this.sortOrder = sortOrder;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Date getAddedDate() {
return addedDate;
}
public void setAddedDate(Date addedDate) {
this.addedDate = addedDate;
}
public Date getModifiedDate() {
return modifiedDate;
}
public void setModifiedDate(Date modifiedDate) {
this.modifiedDate = modifiedDate;
}
public Map getdescription() {
return description;
}
public void setdescription(Map description) {
this.description = description;
}
}
CategoryDescription.java:Code:
package com.mydomain.types;
public class CategoryDescription extends NamedType {
private int languageId;
public int getLanguageId() {
return languageId;
}
public void setLanguageId(int languageId) {
this.languageId = languageId;
}
}
code for NamedType.java:
Code:
package com.mydomain.types;
public class NamedType {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
In the Hibernate DAO implementation class (CategoryDAOHibernate.java) I have:
Code:
package com.mydomain.dao.hibernate;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.mydomain.dao.CategoryDAO;
import com.mydomain.domain.Category;
public class CategoryDAOHibernate extends HibernateDaoSupport implements
CategoryDAO {
@Override
public List<Category> getAllCategories() {
return (List<Category>)getHibernateTemplate ().find ("from Category c " +
"where c.parentId = 0 order by c.id");
}
............
}
My mapping file -- category.hbm.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Hibernate mapping file for the Category domain object -->
<hibernate-mapping package="com.paintingmax.domain" auto-import="true" default-lazy="false">
<class name="Category" table="categories">
<id name="id" column="categories_id">
<generator class="identity" />
</id>
<property name="image" column="categories_image" type="string" />
<property name="parentId" column="parent_id" />
<property name="sortOrder" column="sort_order" />
<property name="addedDate" column="date_added" type="date" />
<property name="modifiedDate" column="last_modified" type="date" />
<map name="description" table="categories_description">
<key column="categories_id" />
<map-key column="language_id" type="integer" />
<composite-element class="com.paintingmax.types.CategoryDescription">
<property name="name" type="string" column="categories_name" not-null="true" />
</composite-element>
</map>
</class>
</hibernate-mapping>
In my category.jsp file (using JSF framework, beanManager is JSF managed bean, JSF will inject Spring managed bean ServiceFacade, and the ServiceFacade bean will in turn inject the DAOs ..) I have:
Code:
<c:forEach var="category" items="#{beanManager.categories}">
Category ID: #{category.id}<br />
Category Image: #{category.image}<br/>
Category Name: #{category.description["1"].name}<br/><br/>
</c:forEach>
and it displays like this:
Category ID: 1
Category Image: test image
Category Name:
Category ID: 2
Category Image:
Category Name:
Category ID: 3
Category Image:
Category Name:
Category ID: 4
Category Image:
Category Name:
Category ID: 5
Category Image:
Category Name:
.......
Basically, the
category name is not displayed, and the query from log4J looks like this:
Code:
Hibernate: select category0_.categories_id as categories1_0_, category0_.categories_image as categories2_0_, category0_.parent_id as parent3_0_, category0_.sort_order as sort4_0_, category0_.date_added as date5_0_, category0_.last_modified as last6_0_ from categories category0_ where category0_.parent_id=0 order by category0_.categories_id
Hibernate: select descriptio0_.categories_id as categories1_0_, descriptio0_.categories_name as categories2_0_, descriptio0_.language_id as language3_0_ from categories_description descriptio0_ where descriptio0_.categories_id=?
.......
Any one can help whats wrong with my mapping from categories to categories_description?
Any help is appreciated.