-->
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.  [ 3 posts ] 
Author Message
 Post subject: How will I set value into many-to-one element-Hibernate
PostPosted: Mon Jul 24, 2006 7:29 am 
Beginner
Beginner

Joined: Thu Jul 13, 2006 10:39 am
Posts: 23
I have a problem while using Hibernate bi-directional relationship.

My hbm file look like this:
-------------------------------------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hibernate">
<class name="Submissions" table="submission">

<id name="submissionId" type="string" unsaved-value="null" >
<column name="submission_id" sql-type="raw(16)" not-null="true"/>
</id>

<property name="submissionNo">
<column name="submission_no" sql-type="VARCHAR2(5)" not-null="true"/>
</property>

<property name="createdTs">
<column name="created_ts" sql-type="Date" not-null="true"/>
</property>

<property name="lastModifiedTs">
<column name="last_modified_ts" sql-type="Date" not-null="true"/>
</property>

...... ....... ........ ...... ...

<many-to-one name="application" insert="false" update="false" class="Application" column="application_id" />

</class>

</hibernate-mapping >

Application.hbm file
---------------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hibernate">

<class name="Application" table="application">

<id name="appId" type="string" unsaved-value="null" >
<column name="application_id" sql-type="raw(16)" not-null="true"/>
</id>

<property name="appName">
<column name="application_name" sql-type="varchar2(100)" not-null="true"/>
</property>

<property name="appFolderName">
<column name="application_folder_name" sql-type="varchar2(2000)" not-null="true"/>
</property>

........ ...... ....... ....... .....

<set name="submissions" inverse="true" cascade="all-delete-orphan">
<key column="application_id"/>
<one-to-many class="Submissions"/>
</set>
</class>

------------------------------------------------------------------------------------------------------------
The problem I nedd to insert a record only into submission table using Hibernate.

My sample code looks like this:

SessionFactory sessionFactory=HibernateFactory.buildsessionFactory();
Session session= sessionFactory.openSession();
Transaction tx=HibernateFactory.begintransaction(session);
Submissions oSub=new Submissions();
// How will I set application id in Submission which has many-to-one relationship to application table
oSub.setSubmissionNo("0005");
oSub.setCreatedTs(new java.util.Date());
oSub.setLastModifiedTs(new Date());
session.saveOrUpdate(oSub);
HibernateFactory.commitTransaction(tx);
System.out.println("Done");
HibernateFactory.close(session);


How will I set application Id to submission table?

Please do favour with me

Thanks and regards,
Sreekanth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 24, 2006 9:10 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
When you specify insert="false" and update="false" in many-to-one element, you can include property element for the same column specified in many-to-one element. That means you have capability to set the value of application_id column using corresponding setter.

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hibernate">
<class name="Submissions" table="submission">

   <id name="submissionId" type="string" unsaved-value="null" >
      <column name="submission_id" sql-type="raw(16)" not-null="true"/>
   </id>

   <property name="submissionNo">
      <column name="submission_no" sql-type="VARCHAR2(5)" not-null="true"/>
   </property>

   <property name="createdTs">
      <column name="created_ts" sql-type="Date" not-null="true"/>
   </property>

   <property name="lastModifiedTs">
      <column name="last_modified_ts" sql-type="Date" not-null="true"/>
   </property>

   ...... ....... ........ ...... ...

   <property name="applicationId">
      <column name="application_id" sql-type="VARCHAR2(5)" not-null="true"/>
   </property>

   <many-to-one name="application" insert="false" update="false" class="Application" column="application_id" />

</class>

</hibernate-mapping >


So now your code with slight modification is below

Code:
SessionFactory sessionFactory=HibernateFactory.buildsessionFactory();
Session session= sessionFactory.openSession();
Transaction tx=HibernateFactory.begintransaction(session);
Submissions oSub=new Submissions();
// How will I set application id in Submission which has many-to-one relationship to application table
oSub.setApplicationId( "007" ); // this sets the application id
oSub.setSubmissionNo("0005");
oSub.setCreatedTs(new java.util.Date());
oSub.setLastModifiedTs(new Date());
session.saveOrUpdate(oSub);
HibernateFactory.commitTransaction(tx);
System.out.println("Done");
HibernateFactory.close(session);


Below code gives you an idea how to persist both Application and Submission at the same time with a call to saveOrUpdate.

Code:
SessionFactory sessionFactory=HibernateFactory.buildsessionFactory();
Session session= sessionFactory.openSession();
Transaction tx=HibernateFactory.begintransaction(session);
Submissions oSub=new Submissions();
// How will I set application id in Submission which has many-to-one relationship to application table
oSub.setApplicationId( "00001" ); // this sets the application id
oSub.setSubmissionNo("0005");
oSub.setCreatedTs(new java.util.Date());
oSub.setLastModifiedTs(new Date());

Application application1 = new Application();
// populate properties including appId
application1.getSubmissions().add( application1 );
oSub.setApplication( application1 );

Application application2 = new Application();
// populate properties including appId
application1.getSubmissions().add( application2 );
oSub.setApplication( application2 );

session.saveOrUpdate(oSub);
HibernateFactory.commitTransaction(tx);
System.out.println("Done");
HibernateFactory.close(session);


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 25, 2006 12:02 am 
Beginner
Beginner

Joined: Thu Jul 13, 2006 10:39 am
Posts: 23
Thank u for ur reply. It is working fine.

Have a nice day,

Thanks and regards,
Sreekanth


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