-->
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.  [ 3 posts ] 
Author Message
 Post subject: Can anyone help me with this mapping -- entity to value type
PostPosted: Thu Dec 06, 2007 2:04 am 
Newbie

Joined: Thu Dec 06, 2007 1:53 am
Posts: 2
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.java

Code:
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 06, 2007 6:45 am 
Newbie

Joined: Tue Nov 27, 2007 3:17 am
Posts: 5
I don't think this is the problem with your hibernate mapping. it is there in your JSP expression. I have three doubt about your JSP code

1) How can you access a Map object as an array object?. You need to use get() method.

2) Even if you are allowed to access as an array how can you pass a Key as String object(which return null), where you have specified it as an integer type in mapping.

3) Even if you are allowed to pass String object as key to the map you will get object of type Object not a CategoryDescription object. So, you have to Typecast it.

So, plz reply me for my clarification about these doubts.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 06, 2007 12:04 pm 
Newbie

Joined: Thu Dec 06, 2007 1:53 am
Posts: 2
Thanks for you reply, really appreciate it.

pokuri wrote:
I don't think this is the problem with your hibernate mapping. it is there in your JSP expression. I have three doubt about your JSP code

1) How can you access a Map object as an array object?. You need to use get() method.


Yes, in JSP EL you can access a Map, List, Array, using #{COLLECTION_NAME [map_key/list_index]}

Quote:
2) Even if you are allowed to access as an array how can you pass a Key as String object(which return null), where you have specified it as an integer type in mapping.

JSP engine will convert whatever what put in the [] after map/list name to the
key type or index, it will complain if the type you provided can not be converted.

Quote:
3) Even if you are allowed to pass String object as key to the map you will get object of type Object not a CategoryDescription object. So, you have to Typecast it.


What happens behind the scene is that JSP engine will convert
#{category.description["1"].name}
to category.getDescription ().getValue (new Integer (1)).getName ()

So it will return the category's name

Quote:
So, plz reply me for my clarification about these doubts.

Let me know if I you need more clarification, and thanks again for your reply.


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