hi ,
i have two tables, A parent and the child relation exists between them.
The child table has a composite key.
The problem i m facing is that while saving the parent child is not getting saved.
Here are the HBM files.
***********************
Dcuments.hbm(PARENT)
<hibernate-mapping >
<class name="dams.ds.cust.Documents" table="DOCUMENTS">
<id name="docID" column="DOC_ID" type="int" unsaved-value="any">
<generator class="sequence">
<param name="sequence">pk_sequence</param>
</generator>
</id>
<property name="state" column="STATE" />
<set name="custPageStates" lazy="false" cascade="all"> <key column="DOC_ID"/> <one-to-many class="dams.ds.cust.CustomizationStatus"/> </set> </class>
</hibernate-mapping>
---------------------------------------------------
CustomizationStatus.hbm(CHILD)
<hibernate-mapping >
<class name="dams.ds.cust.CustomizationStatus" table="CUSTOMIZATION_STATUS">
<composite-id>
<key-many-to-one name="document" class="dams.ds.cust.Documents" column="DOC_ID"/> <key-property name="pageNo" column="PAGE_NO" />
</composite-id>
<property name="status" column="STATUS" />
</class>
</hibernate-mapping>
Java Bean Files
*********************
Documents.java
public class Documents
{
private int docID;
private String state = null;
private Set custPageStates = new HashSet();
public void setCustPageStates(Set custPageStates) { this.custPageStates = custPageStates; }
public void addCustPageState(CustomizationStatus custPageState) { this.custPageStates.add(custPageState); }
public Set getCustPageStates() { return this.custPageStates; }
public int getDocID()
{
return docID;
}
public void setDocID(int docID)
{
this.docID = docID;
}
public String getState()
{
return state;
}
public void setState(String state)
{
this.state = state;
}
}
----------------------------------
CustomizationStatus.java
public class CustomizationStatus implements Serializable
{
private int pageNo;
private Documents document = null;
private String status = null;
public void setPageNo(int pageNo)
{
this.pageNo = pageNo;
}
public int getPageNo()
{
return this.pageNo;
}
public void setDocument(Documents document) { this.document = document; }
public Documents getDocument() { return this.document; }
public void setStatus(String status)
{
this.status = status;
}
public String getStatus()
{
return this.status;
}
public boolean equals(Object other)
{
if (other == this)
return true;
if (other != null && other.getClass().equals(this.getClass()))
{
CustomizationStatus that = (CustomizationStatus) other;
if (this.getDocument().getDocID() == that.getDocument().getDocID()
&& this.getPageNo() == that.getPageNo())
return true;
else
return false;
}
return false;
}
public int hashCode()
{
String hashString = this.getDocument().getDocID() + "_" + this.getPageNo();
return hashString.hashCode();
}
}
-------------
Here is the code i m using to set the objects
i m getting 'numPages' from another method
Documents doc = new Documents();
CustomizationStatus custStatus = null;
for (int pageNo = 1; pageNo <= numPages ; pageNo++)
{
custStatus = new CustomizationStatus();
custStatus.setDocument(doc);
custStatus.setPageNo(pageNo);
custStatus.setStatus("needs-customization");
doc.addCustPageState(custStatus);
}
now i m passing this documents object i.e. doc to another method, where i m opening a session and saving the Documents object.
Session session = getNewSession();//to open a new Session
Transaction tx = session.beginTransaction();
session.save(document);
tx.commit();
if (session != null)
{
sesson.close();
}
|