1 Reply Latest reply on Mar 12, 2009 11:44 AM by Mark Varley

    EntityHome update parent problem

    Mark Varley Newbie

      I have the following entity relationship modelled in my app: Vendor one-to-many Booking one-to-one BookingReview.  Once a customer has completed a booking they get sent an email inviting them to review their booking experience, they sign in, access a list of bookings (modelled as an EntityQuery) and if the booking has no review they can add one - this takes them to a standard BookingReviewEdit screen as per the seam-gen generated screens.


      On persist of the BookingReview I want to run a method on the Vendor to refresh the average ratings for that Vendor (these are calculated and stored in the vendor table for performance reasons).  I use the following code in my BookingReviewHome class:


      @Override
      public String persist() {
           String outcome = super.persist();
           if (outcome != null)
                getInstance().getBooking().getVendor().calculateAveRating();
           return outcome;
      }
      



      The problem is that the average ratings are not being populated.  I put a breakpoint in calculateAverageRating() it seems that at the point this is being run, as it iterates over the list of bookings it does not find the review I just added.  I checked the log at this point and can see the SQL to insert the review however this has not yet been flushed to the database.


      I have tried to add a getEntityManager().flush() statement just after super.persist() but this makes no difference.  I have also tried to set flush-mode="AUTO" in BookingReview.page.xml but again, no luck, calculateAveRating() still does not find my review.  I have also tried a different approach entirely putting the following in my VendorHome class:


      @Observer("org.jboss.seam.afterTransactionSuccess.BookingReview")
      public void refreshAveRating() {
           getEntityManager().refresh(getInstance());
           getInstance().calculateAveRating();
      }
      



      Again, this does not work.


      If anyone can suggest where I am going wrong or a better way of achieving my desired result I would be most grateful as I have spent a huge amount of time on this seemingly trivial problem.  For completeness I have included extracts from my entity classes below showing the relationship between my entities.


      Thanks in advance,


      mark


      Vendor:


      @OneToMany(mappedBy = "vendor", cascade = CascadeType.ALL)
      public List<Booking> getBookings() {
           return bookings;
      }
      



      Booking:


      @ManyToOne
      @JoinColumn(name = "vendor_id", nullable = false)
      @NotNull
      public Vendor getVendor() {
           return vendor;
      }
      
      @OneToOne(mappedBy = "booking", optional = true, cascade = CascadeType.ALL)
      public BookingReview getReview() {
           return review;
      }
      



      BookingReview:


      @OneToOne(optional = false, cascade = CascadeType.REFRESH)
      @JoinColumn(name = "booking_id", nullable = false, unique = true)
      public Booking getBooking() {
           return booking;
      }