-->
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.  [ 7 posts ] 
Author Message
 Post subject: HashMap is not getting persisted
PostPosted: Thu Oct 05, 2006 8:56 am 
Newbie

Joined: Wed Oct 04, 2006 3:28 pm
Posts: 6
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]


Last edited by hi2sachin on Thu Oct 05, 2006 10:34 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 05, 2006 9:19 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
You are missing the cascade attribute on the <map> mapping, as explained here:

http://www.hibernate.org/hib_docs/v3/re ... ns-mapping

If you use <map cascade="save-update" ...> all inserts and updates will be propagated

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 05, 2006 9:24 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Cascading is implicit on collections of value types. Without the code it's impossible to say what the problem here is.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 05, 2006 11:03 am 
Newbie

Joined: Wed Oct 04, 2006 3:28 pm
Posts: 6
[quote="christian"]Cascading is implicit on collections of value types. Without the code it's impossible to say what the problem here is.[/quote]

I am also in Switzerland and stuck up very badly. can you please help me


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 05, 2006 11:08 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
"Without the code it's impossible to say what the problem here is."

http://www.hibernate.org/ForumMailingli ... AskForHelp


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 05, 2006 11:09 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Oh, I didn't see the code. Probably because it's not in code tags. Don't have time right now to read through that garbled mess.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 05, 2006 11:37 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Remove the inverse attribute on the set. You are asking Hibernate to ignore that side of the mapping , which is probably why its not inserting records into the join table.
Refer to the Hibernate reference docs Section 1.3.6. Working bi-directional links


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