Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hi friends,
I want to persist a Region Object. Region Object contains a Map also.
When I do the session.save(region), the data is only gets inserted for region table not for the map table.
The mapping file is below. Can anyone help me on the same.
Hibernate version:3.0
Mapping documents:
<hibernate-mapping>
<class name="kh.doc.ejb.impl.Region" table="REGION">
<id name="id">
<column name="REGION_ID" length="50"/>
<generator class="assigned"/>
</id>
<map name="map" inverse="true">
<key column="REGION_ID"/>
<index column="Language" type="string"/>
<element type ="string" column = "Name" />
</map>
</class>
</hibernate-mapping>
Invocation Code
************************************************************
Region region = new Region();
region.setId(7);
HashMap name = new HashMap();
name.put("EN","MALASIYA");
region.setMap(name);
new RegionDAO().persistRegion(region);
*************************************************************
DAO
*************************************************************
public final class RegionDAO {
private static final SessionFactory sessionFactory;
// Member variable: remember Hibernate session
private Session mSession;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public RegionDAO() {
mSession = sessionFactory.openSession();
}
public void persistRegion(Region region){
mSession.flush();
mSession.save(region);
}
}
*************************************************************
Region POJO
public class Region {
protected int id;
public Map map = new HashMap();
/**
* @roseuid 45127D7E0394
*/
public Region() {
}
/**
* Access method for the id property.
*
* @return the current value of the id property
*/
public int getId() {
return id;
}
/**
* Sets the value of the id property.
*
* @param aId the new value of the id property
*/
public void setId(int aId) {
id = aId;
}
/**
* Access method for the map property.
*
* @return the current value of the map property
*/
public Map getMap() {
return map;
}
/**
* Sets the value of the map property.
*
* @param aMap the new value of the map property
*/
public void setMap(Map aMap) {
map = aMap;
}
}
************************************************************
[/code]