hi,
i am using hibernation, and i want to insert a parent and child record, my hbm code is as follows
<class name="com.ParentForm" table="PARENT"> <id name="pk_parent_id" column="pk_parent_id"> <generator class="sequence"> <param name="sequence">seq_parent</param> </generator> </id> <bag name="childDet" cascade="all" > <key column="parent_id" /> <one-to-many class="com.ChildForm"/> </bag> <property name="p_name" column="p_name" type="java.lang.String"/> <property name="p_age" column="p_age" type="java.lang.String"/> </class> <class name="com.ChildForm" table="CHILD"> <id name="pk_child_id"> <generator class="sequence"> <param name="sequence">seq_child</param> </generator> </id> <property name="c_name" column="c_name" type="java.lang.String"/> <property name="c_age" column="c_age" type="java.lang.String"/> </class>
then in the code, org.hibernate.Transaction tx =null; ParentForm pform = ParentForm(form); int count = pform.getPName().length; String result="Success"; String sqlResult=null; try { session = sessionFactory.openSession(); tx = session.beginTransaction(); for(int i =0 ; i< count ; i++) { ChildForm cForm = new ChildForm(); // absDetChild.setAbstract_id(pkValue); cForm.setCName((absForm.getCName())[i]); cForm.setCAge((absForm.getCAge())[i]); }//End For session.save(absForm); }catch (JDBCException e){ sqlResult = e.getCause().toString(); e.printStackTrace(); } finally { try{ // session.save(pForm); sqlResult ="Success"; session.flush(); try{ tx.commit(); }catch(Exception e){ e.printStackTrace(); } session.close(); }catch(JDBCException e){ sqlResult = e.getCause().toString(); e.printStackTrace(); } result=displayMessages(sqlResult); traceLogger.info(":::::::result:::::::"+result); the code which i have written is missing some important thing, could some one help, as the insertion in both the tables are not taking place....
Annu
|