You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by su...@wipro.com on 2006/05/09 07:07:41 UTC

I am new to iBatis. Is it possible to insert into multiple tables using one parameter class using iBatis

I have two classes :

One is Product and other is Service. One product can have multiple
services.



Product details are stored in product table. Service details in service
details.

Service table has foreign key referencing prioduct_id which is primary
key in product table.



While doing select iBatis supports selecting data from multiple tables
using one query (avoiding N+1 selects).

Is there a way to insert into multiple tables using one insert query.










The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments.

WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.

www.wipro.com

Re: I am new to iBatis. Is it possible to insert into multiple tables using one parameter class using iBatis

Posted by Nathan Maves <Na...@Sun.COM>.
While some of the drivers might support multiple statements I would  
suggest against it.

I would write this into the DAO level of your code.

something like....

public void insertProduct(Product p) {
   startTransaction();
   executeInstert("insertProduct", p);
   for(Service s:p.getServices()) {
     executeInsert("insertService", s);
   }
   commitTransaction();
  .....
}

Not perfect but you get the picture :)

Nathan
On May 8, 2006, at 11:07 PM, suresh.dodda@wipro.com wrote:

> I have two classes :
>
> One is Product and other is Service. One product can have multiple  
> services.
>
>
>
> Product details are stored in product table. Service details in  
> service details.
>
> Service table has foreign key referencing prioduct_id which is  
> primary key in product table.
>
>
>
> While doing select iBatis supports selecting data from multiple  
> tables using one query (avoiding N+1 selects).
>
> Is there a way to insert into multiple tables using one insert query.
>
>
>
>
>
>
>
>
> The information contained in this electronic message and any  
> attachments to this message are intended for the exclusive use of  
> the addressee(s) and may contain proprietary, confidential or  
> privileged information. If you are not the intended recipient, you  
> should not disseminate, distribute or copy this e-mail. Please  
> notify the sender immediately and destroy all copies of this  
> message and any attachments.
>
> WARNING: Computer viruses can be transmitted via email. The  
> recipient should check this email and any attachments for the  
> presence of viruses. The company accepts no liability for any  
> damage caused by any virus transmitted by this email.
>
> www.wipro.com
>


Re: I am new to iBatis. Is it possible to insert into multiple tables using one parameter class using iBatis

Posted by Jerome Gagner <ph...@gmail.com>.
hahah disregard that, didn't really answer the question. Just like I said, I
just woke up.


On 5/8/06, Jerome Gagner <ph...@gmail.com> wrote:
>
> public class Product {
>     private String name;
>     private Integer id;
>
>     public String getName() {
>         return name;
>     }
>
>     public void setName(String name) {
>         this.name = name;
>     }
>
>     public Integer getId() {
>         return id;
>     }
>
>     public void setId(Integer id) {
>         this.id = id;
>     }
> }
>
> public class Service {
>     private Integer name;
>     private Product product;
>
>     public Integer getName() {
>         return name;
>     }
>
>     public void setName(Integer name) {
>         this.name = name;
>     }
>
>     public Product getProduct() {
>         return product;
>     }
>
>     public void setProduct(Product product) {
>         this.product = product;
>     }
> }
>
> <sqlMap>
>    <resultMap id="ProductMap">
>        <result property="id" column="id"/>
>        <result property="name" column="name"
>     <resultMap id="ServiceMap" class="Service" groupBy="name">
>        <result property="product" resultMap="product" />
>        <result property="name" column="name"/>
>    </resultMap>
>     <select id="getAllServices" resultMap="ServiceMap">
>           select services.*, products.* from services,product where
> services.product_id =  product.id
>    </select>
> </sqlMap>
>
>
> It's pretty rough because I wrote it in gmail's email editor thing and I
> just woke up from a nap. You should get the point.
>
> On 5/8/06, suresh.dodda@wipro.com <su...@wipro.com> wrote:
> >
> >  I have two classes :
> >
> > One is Product and other is Service. One product can have multiple
> > services.
> >
> >
> >
> > Product details are stored in product table. Service details in service
> > details.
> >
> > Service table has foreign key referencing prioduct_id which is primary
> > key in product table.
> >
> >
> >
> > While doing select iBatis supports selecting data from multiple tables
> > using one query (avoiding N+1 selects).
> >
> > Is there a way to insert into multiple tables using one insert query.
> >
> >
> >
> >
> >
> >
> >
> > The information contained in this electronic message and any attachments
> > to this message are intended for the exclusive use of the addressee(s) and
> > may contain proprietary, confidential or privileged information. If you are
> > not the intended recipient, you should not disseminate, distribute or copy
> > this e-mail. Please notify the sender immediately and destroy all copies of
> > this message and any attachments.
> >
> > WARNING: Computer viruses can be transmitted via email. The recipient
> > should check this email and any attachments for the presence of viruses. The
> > company accepts no liability for any damage caused by any virus transmitted
> > by this email.
> >
> > www.wipro.com
> >
>
>

Re: I am new to iBatis. Is it possible to insert into multiple tables using one parameter class using iBatis

Posted by Jerome Gagner <ph...@gmail.com>.
public class Product {
    private String name;
    private Integer id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

public class Service {
    private Integer name;
    private Product product;

    public Integer getName() {
        return name;
    }

    public void setName(Integer name) {
        this.name = name;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }
}

<sqlMap>
   <resultMap id="ProductMap">
       <result property="id" column="id"/>
       <result property="name" column="name"
    <resultMap id="ServiceMap" class="Service" groupBy="name">
       <result property="product" resultMap="product" />
       <result property="name" column="name"/>
   </resultMap>
    <select id="getAllServices" resultMap="ServiceMap">
          select services.*, products.* from services,product where
services.product_id =  product.id
   </select>
</sqlMap>


It's pretty rough because I wrote it in gmail's email editor thing and I
just woke up from a nap. You should get the point.
On 5/8/06, suresh.dodda@wipro.com <su...@wipro.com> wrote:
>
>  I have two classes :
>
> One is Product and other is Service. One product can have multiple
> services.
>
>
>
> Product details are stored in product table. Service details in service
> details.
>
> Service table has foreign key referencing prioduct_id which is primary key
> in product table.
>
>
>
> While doing select iBatis supports selecting data from multiple tables
> using one query (avoiding N+1 selects).
>
> Is there a way to insert into multiple tables using one insert query.
>
>
>
>
>
>
>
> The information contained in this electronic message and any attachments
> to this message are intended for the exclusive use of the addressee(s) and
> may contain proprietary, confidential or privileged information. If you are
> not the intended recipient, you should not disseminate, distribute or copy
> this e-mail. Please notify the sender immediately and destroy all copies of
> this message and any attachments.
>
> WARNING: Computer viruses can be transmitted via email. The recipient
> should check this email and any attachments for the presence of viruses. The
> company accepts no liability for any damage caused by any virus transmitted
> by this email.
>
> www.wipro.com
>