-->
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.  [ 2 posts ] 
Author Message
 Post subject: insert two tables in single action ?
PostPosted: Tue Jul 18, 2006 4:57 am 
Beginner
Beginner

Joined: Tue Jun 06, 2006 7:56 am
Posts: 20
Hi

i am using struts with hibernate

i am using mssql2000 server. i have 2 tables

1. displayname (FieldID,Title)
2. MainTable (FieldID, Description)


i have one jsp. it contains title and description

i want to insert title, in the first table(displayname, see above) and
i want to insert description, in the second table(mainTable, see above)

Could you tell me the in single submission, i want to insert two databases.

How Hibernate Query looks like. thanks
Thanks
edward


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 18, 2006 10:12 am 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
If you are asking for 1 HQL to insert into both tables, I believe that is not possible and practical. Even outside of hibernate, if you have to insert into 2 tables you will have to use 2 insert queries. You can however leverage triggers on 1 table to insert into the other but in this case you don't have values for hte second table available.

Alerternatively, you could use the following mapping to acheive the same:

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

<hibernate-mapping package="com.hibernate.forums">
   <class
      name="Displayname"
      table="DISPLAYNAME"
   >
      <meta attribute="sync-DAO">false</meta>

      <id
         name="Id"
         column="ID"
         type="integer"
         length="10"
      >
         <generator class="assigned"/>
      </id>
      <property
         name="Title"
         column="TITLE"
         type="string"
         not-null="false"
         length="10"
      />
      <one-to-one name="mainTable" class="Maintable" cascade="save-update"/>
      


   </class>   
</hibernate-mapping>


Maintable.hbm.xml
Code:
<hibernate-mapping package="com.hibernate.forums">
   <class
      name="Maintable"
      table="MAINTABLE"
   >
      <meta attribute="sync-DAO">false</meta>
         <id
            name="Id"
            column="ID"
            type="integer"
            length="10"
         >
              <generator class="assigned"/>
           </id>
      <property
         name="Description"
         column="DESCRIPTION"
         type="string"
         not-null="false"
         length="10"
      />

   </class>   
</hibernate-mapping>


Sampe code for a cascading save:

Code:
SessionFactory factory  = cfg.buildSessionFactory();
      Session tSession = factory.openSession();
      System.out.println("Configurations Ok!!!");
      Maintable maintable = new Maintable();
      maintable.setDescription("Descript");
      Displayname name = new Displayname();
      name.setId(new Integer(1));
      name.setTitle("Title");
      name.setMainTable(maintable);
      Transaction tx = tSession.beginTransaction();
      tSession.save(name);
      tx.commit();
      tSession.close();


Hope that helps.


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