I have tried this a number of ways and most recently tried the mapping this way. The paradigm is so common that i know this has to be handled from the framework - but i am missing something from all the books i have bought, information browsed online, etc...
I have a DomainBean which might contain optional one or more child domains and one or more optional parent domains. I am using a link table to connect the associated rows but the link table is not getting updated when i set the associations in the objects (using Sets) - only the domain table is being inserted into. I tried using a join tag at one point and that didnt work. guess i am missing something fundamental here. Any help would be appreciated.
Hibernate version: 3.2.4
Mapping documents:
<!-- ====================== DOMAIN BEAN ================================== -->
<class name="core.business.domain.DomainBean" table="domain_tbl">
<id name="domainID" column="domain_id" type="integer">
<generator class="native"></generator>
</id>
<set name="subDomains" table="domain_mapping_tbl">
<key column="parent_domain_id"/>
<many-to-many class="core.business.domain.DomainBean" column="child_domain_id" />
</set>
<set name="parentDomains" table="domain_mapping_tbl">
<key column="child_domain_id"/>
<many-to-many class="core.business.domain.DomainBean" column="parent_domain_id" />
</set>
<property type="integer"
column="domain_type_id"
name="domainType"
access="property" />
<property type="integer"
column="domain_owner_id"
name="domainOwnerID"
access="property" />
<property type="string"
column="domain_name"
name="domainName"
access="property" />
<property type="string"
column="domain_desc"
name="domainDescription"
access="property" />
</class>
Code between sessionFactory.openSession() and session.close():
Session session = ApplicationStartupListener.getFactory(this.getServlet().getServletContext()).openSession();
HashMap domainLinksMap = new HashMap();
DomainManager manager = new DomainManager();
if(!errors.isEmpty()){
this.saveErrors(request,errors);
return mapping.getInputForward();
}
if( request.getParameter("domainID") != null && request.getParameter("domainName") == null ){
if(request.getParameter("domainID") != null){
int topDOmainID = Integer.parseInt(request.getParameter("domainID"));
DomainBean topDomain = manager.getDomain(session, topDOmainID);
request.setAttribute("parentDomain", topDomain);
}
} else {
try{
int topDOmainID = Integer.parseInt(request.getParameter("domainID"));
DomainBean topDomain = manager.getDomain(session, topDOmainID);
Map map = this.mapParams(request);
bean = manager.createSubDomain(session, map);
topDomain.getSubDomains().add(bean);
bean.getParentDomains().add(topDomain);
session.update( topDomain );
session.update( bean );
//Query setJoinedCriteria = session.createQuery("Insert Into domain_mapping_tbl values(?,?,?)");
//setJoinedCriteria.setInteger(1, topDomain.getDomainID());
//setJoinedCriteria.setInteger(2, bean.getDomainID());
//setJoinedCriteria.setInteger(3, 0);
// int update = setJoinedCriteria.executeUpdate();
request.setAttribute("domain", bean);
domainLinksMap.put("domainID",bean.getDomainID());
}
catch(Exception e)
{
e.printStackTrace(System.out);
return mapping.getInputForward();
} finally{
session.disconnect();
session.close();
}
Full stack trace of any exception that occurs: No exceptions
Name and version of the database you are using: MySQL 5.1
The generated SQL (show_sql=true):
01:23:06,828 INFO [STDOUT] Hibernate: insert into core.domain_tbl (domain_type_id, domain_owner_id, domain_name, domain_desc) values (?, ?, ?, ?)
01:23:06,906 INFO [STDOUT] Hibernate: select subdomains0_.parent_domain_id as parent1_1_, subdomains0_.child_domain_id as child2_1_, domainbean1_.domain_id as domain1_8_0_, domainbean1_.domain_type_id as domain2_8_0_, domainbean1_.domain_owner_id as domain3_8_0_, domainbean1_.domain_name as domain4_8_0_, domainbean1_.domain_desc as domain5_8_0_ from core.domain_mapping_tbl subdomains0_ left outer join core.domain_tbl domainbean1_ on subdomains0_.child_domain_id=domainbean1_.domain_id where subdomains0_.parent_domain_id=?
Debug level Hibernate log excerpt: N/A
|