2 Replies Latest reply on Mar 5, 2009 2:51 AM by Troy Sellers

    Injecting and persisting entity and relations

    Troy Sellers Newbie

      Hi All,

      Does anyone know what I am doing wrong here?

      I have an Entity that is backing a form, it has a OneToOne relationship with another entity (Form - CustomerDetails)

      When I inject the Form
      private Form theForm;

      the CustomerDetails object is null when referencing in the view
      i.e. #{theForm.customerDetails.name} throws error.

      To get around this I have also injected a CustomerDetails and in an action I build the object
      i.e theForm.customerDetails = customerDetails;

      This doesn't seem the right way to go, but can't figure out how to eagerly create the form object on injection I guess.

      I also have a similar problem when using the entityManager.persist() method call.
      If I persist just the form (entityManager.persist(theForm) ) I get transient errors with respect to the customer details object.
      I need to persist the customerDetails first, then perist theForm.

      Is there a way to eagerly persist my form object?


        • 1. Re: Injecting and persisting entity and relations
          Tim Evers Master

          Ok to fix your persisting problem try using cascade (read the doco on this to get the full info on it). But for example where you have the @OneToOne annotaion you would put.

          @OneToOne(cascade = { CascadeType.ALL })

          What this means is that when you persist your Form entity it will persist the customerDetails entity.
          There is a possibility that this isn't what you want but from what you have said so far it would appear to me that it would be your solution.

          Your other problem can be solved the way the SEAM examples show.
          If you look in the SEAM booking example you will see in the HotelBookingAction.java

          private Booking booking;

          public void bookHotel()
              booking = new Booking(hotel, user);
              Calendar calendar = Calendar.getInstance();
              booking.setCheckinDate( calendar.getTime() );
              calendar.add(Calendar.DAY_OF_MONTH, 1);
              booking.setCheckoutDate( calendar.getTime() );

          What this means is that when the bookHotel method is called (presumably from a create booking button) a new Booking entity is created (hotel and user are set earlier). Once this entity is created and the method invocation is finished the booking variable is outjected and is now accessable via the @In.

          The seam examples are a really good place to get to understand the more fundamental ideas that SEAM is based on. Take some time to go through them all it will help a lot! :D

          • 2. Re: Injecting and persisting entity and relations
            Troy Sellers Newbie
            Hi Tim.

            Thanks for the tips. Using the Cascade elements worked a treat.

            I am not sure what you meant with the example from Hotel Booking however.  If I built the entity and then Outjected, would this work in a similar method to Cascade?

            The child entities in my case are created at the same time as the parent.  The parent is created using @In(create=true) yet this doesn't create the children, so I also inject child objects into the same action (again using create=true) and build them into the parent manually.

            Is there a means of creating the parent so that these OneToOne related objects are also created?