2 Replies Latest reply on May 3, 2007 3:43 PM by alrubinger

    Entitymanager Persistent

    poyge394

      Hi,

      i have 2 entity bean, Rollingstock and Schedule.
      Hear is some code:

      rollingstock = new Rollingstock();
      rollingstock.setNextRoute( "STH" );
      rollingstock.setRoute( "HALL" );
      schedule = new Schedule();
      rollingstock.setSchedule( schedule );
      


      So now i whant to persist the entitys, i thought that i just neded to do:
      manager.persist(rollingstock);

      then bouth rollingstock and schedule would be persist, but i get a exception when i do that.
      i must do this to get both persist:
      manager.persist(rollingstock);
      manager.persist(schedule);
      


      is thair any better way to persist entitys, Because in my Rollingstock entity i have a lot off relations, so when i get a Rollingstock i just whant to run persist(rollingstocko);
      now i must take out evrything and do persist on each entity.

      any clarification ?




        • 1. Re: Entitymanager Persistent

          What is the exception?

          Regards

          Felix

          • 2. Re: Entitymanager Persistent
            alrubinger

            Well, unless the relationship defined is annotated with @Cascade (CascadeType.PERSIST), you can't persist one entity and expect the relationship to be inserted as well.

            So that's one solution.

            However, I typically keep the Cascading strategies to NONE as a default (to keep close tabs on what operations the container might do without my explictly telling it so; this is a perference only), and would do the following:

            // Create Rollingstock
            rollingstock = new Rollingstock();
            rollingstock.setNextRoute( "STH" );
            rollingstock.setRoute( "HALL" );
            
            // Create Schedule
            schedule = new Schedule();
            
            // Persist Each
            manager.persist(rollingstock);
            manager.persist(scheulde);
            
            // Associate (bi-directionally) after Persisted
            rollingstock.setSchedule(schedule);
            schedule.setRollingstock(rollingstock);