ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Akins, Greg" <gak...@amervideo.com>
Subject RE: 1:M relations, cascading inserts, etc...
Date Tue, 01 Feb 2005 17:56:16 GMT
Will the code I just created (in response to your answer) help in the
FAQ (My own DAO objects, not IbatisDAO)?

public Object insert(Avp avp) throws SQLException {
	//insert Master record
  	Object returnVal = this.getSqlMap().insert("insertAvp", avp) ;
	//iterate through associated List and insert results
	        	        
	for (int i=0; i<(avp).getWares().size();i++) {
		Ware ware = (Ware)(avp).getWares().get(i) ; 
		ware.setAvp(avp.getId()) ; 
		this.getSqlMap().insert("insertWare", ware ) ; 
	}
	        
	return returnVal ;
}
	
        	 

-----Original Message-----
From: Clinton Begin [mailto:clinton.begin@gmail.com] 
Sent: Tuesday, February 01, 2005 12:17 PM
To: ibatis-user-java@incubator.apache.org
Subject: Re: 1:M relations, cascading inserts, etc...


The best practice approach is to have separate <insert> statements and
call each individually from with your DAO code within a normal for loop.
The <iterate> element was never meant to be used for supporting multiple
insert/update/delete statements.

This is a common confusion.  Perhaps something in the FAQ would be a
good idea.

Clinton


On Tue, 1 Feb 2005 10:52:57 -0500, Akins, Greg <gakins@amervideo.com>
wrote:
>  
> Trying to find the best approach to a 1:M relationship.
> (Apologize ahead if I'm missing something) 
>   
>   
> class master { 
>     private List children ; 
>     public void setChildren(List list){} 
>     public List getChildren() {}
> }  
>   
> The suggestions from the mail archives seem to indicate two 
> possibilities
>   
> <sqlMap... 
>         insert into master values(...) ; 
>         <iterate id=children> 
>             insert into Childvalues(..) ; 
>         </iterate>
> </sqlMap 
>   
> or....
>   
> Master master = new Master() ;
>   
> Child child = new Child() ;
> master.getChildren().add(child) ; 
>   
> MasterDAO.save(master) ;
>   
> child.setParent(master) ;
> ChildDAO.save(child) ; 
>   
> Using the first approach causes me to create multiple insert 
> statements for a "Child" insert (One in the Master mapping, one in the

> child mapping... Is there a way to define links to other statements? 
> Using the second approach causes me to use "Relational" constructs to 
> describe a Object mapping.
>   
>   
>   
>  
> Greg Akins
> Software Engineer 
> Sony Electronics, STP Systems 
> 724.696.7322 (Sony) 
> 724.696.6147 (AV) 
> 724.454.7790 (Cell) 
> 412.590.3973 (Pager) 
>

Mime
View raw message