reset password
Author Message
afetiso
Posts: 84
Posted 21:26 Feb 12, 2015 |

I have Stage model with id, name. I have a plan model: with id list of stages and list of runways, list of cells

When I create new stage it appears in stage table in db, but it not appears in stages_planes db where i have:

stage_id       plan_id

 

Attachments:
afetiso
Posts: 84
Posted 21:27 Feb 12, 2015 |

It create a new stage with new id, but it doesn't add it to plan

Controller

   @RequestMapping(value = "/addStage.html", method = RequestMethod.GET)
    public String addStage( @RequestParam Integer id, ModelMap models )
    {    
        //models.put("runways", userDao.getRunways());
        //models.put("stages", userDao.getStages());
        models.put( "stage", new Stage() );
        models.put("planid", id);
        return "addStage";
    }
    
    @RequestMapping(value = "/addStage.html", method = RequestMethod.POST)
    public String addStage( @RequestParam Integer planid, String sname, @ModelAttribute Stage stage, BindingResult bindingResult )
    {
        System.out.println("Entering stage : "+sname+"planid "+planid);
        //planValidator.validate( plan, bindingResult );
        //if( bindingResult.hasErrors() ) return "addPlan";
        //userDao.savePlan( plan );
        List<Plan> plist = userDao.getList(planid);
        Stage stg = new Stage();
        stg.setStage(sname);
        
        stg.setPlanes(plist);
        
        userDao.addStage(stg);
        
      
        return "redirect:/plan.html?id="+planid;
    }

afetiso
Posts: 84
Posted 21:28 Feb 12, 2015 |

  DaoImp:

  @Override
    @Transactional
    public Stage addStage ( Stage stage)
    {        // entityManager.createQuery( "from Plandetail where id =: plamd 
        System.out.println("Enterd userimp");
        return entityManager.merge( stage );
    }

afetiso
Posts: 84
Posted 21:29 Feb 12, 2015 |

JSP

 

<form:form  commandName="stage" method="POST" action="addStage.html">
<div style="margin-left: 40px;">
<p><strong>Golden Eagle Flight Plan - Add A Stage</strong></p>

<table border="1" cellpadding="2" cellspacing="2">
    <tbody>
        <tr>
            <th scope="row" style="text-align: right;">Stage:</th>
            <td><%-- <form:input path="stage" /> --%>
            <input type="text" name="sname"/>
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" style="text-align: right;">
            <input type="hidden" name="planid" value="${planid}"/>
            <input type="submit" name="add" value="Add" />
            </td>
        </tr>
    </tbody>
</table>
</div>
</form:form>

afetiso
Posts: 84
Posted 21:30 Feb 12, 2015 |

@Entity
@Table(name = "plans")
public class Plan implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue
    Integer id;
    
    @Column(nullable = false, unique = true)
    String name;
    
    @ManyToMany(mappedBy="planes", cascade={CascadeType.PERSIST, CascadeType.MERGE} )
    List<Stage> stages;
    
    @ElementCollection
    List<Runway> runways;
    
    @OneToMany
    Set<Cell> cells;

    Date publishedDate;

afetiso
Posts: 84
Posted 21:31 Feb 12, 2015 |

@Entity
@Table(name = "stages")
public class Stage implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue
    private Integer id;
    
    String stage;
    
    @ManyToMany (cascade={CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(name="stages_plans", joinColumns = @JoinColumn(name = "stages_id"), inverseJoinColumns = @JoinColumn(name = "planes_id"))
    List<Plan> planes;

afetiso
Posts: 84
Posted 21:32 Feb 12, 2015 |

stages tabel

Attachments:
cysun
Posts: 2935
Posted 15:22 Feb 13, 2015 |

Because Stage-Plan is a bidirectional association, you need to add plan to stage (which you did), but also add stage to plan before you do merge.

And next time please don't post a bunch of actual code in the forum. If you do this again not only I'll delete your posts, I'll treat it as facilitate cheating.

afetiso
Posts: 84
Posted 15:54 Feb 13, 2015 |
cysun wrote:

Because Stage-Plan is a bidirectional association, you need to add plan to stage (which you did), but also add stage to plan before you do merge.

And next time please don't post a bunch of actual code in the forum. If you do this again not only I'll delete your posts, I'll treat it as facilitate cheating.

Sorry, for that wouldn't do again.