views:

151

answers:

2

I'm trying to add a new entity and then after a successful commit, redirect them to the list action.

Here's a summary of what I'm doing:

@Name("offerAction")
@Scope(ScopeType.CONVERSATION)
public class OfferAction implements Serializable {
  @In private EntityManager entityManager;

  private List<OfferSite> offerSites;
  @Factory("offerSites") public void findAllOfferSites() {
    offerSites = entityManager.createQuery("select os from OfferSite os " +
            "inner join fetch os.offer o " +
            "inner join fetch os.site s " +
            "inner join fetch o.store st " +
            "inner join fetch os.offer.offerType ot").getResultList());
  }

  private Offer instance;

  @Begin(join = true, flushMode = FlushModeType.MANUAL)
  public String createOffer() {
    instance = new Offer();
    createSupportMap();
    return EDIT_VIEW;
  }

  @End public String save() {
    persist();
    findAllOfferSites();
    return LIST_VIEW;
  }

  @Transactional void persist() {
    entityManager.persist();
    /* goes through and persists each site and the metadata associated with each offer-site pair. */
    persistOfferSites(); 
    entityManager.flush();
    entityManager.refresh(instance);
  }

}

Here's the part of the offersite entity that I think is giving me the problems:

@Entity
@Table(name = "t_offer_site")
public class OfferSite {
  @Embeddable
  public static class Id implements Serializable { /* Long offerId, Long siteId */ }

  @EmbeddedId
  private Id id = new Id();

  @ManyToOne
  @JoinColumn(name = "offer_id", insertable = false, updatable = false)
  private Offer offer;
  }
/* assume getters/setters */
}

This saves the Offer object (and its OfferSite metadata) correctly, but when I look at the table displayed from the results of findAllOfferSites() it's missing the Offer data from the OfferSite object that was just added. The OfferSite info displays just fine! The data appears after I completely start a new conversation.

#############################################
# Offer Id # Store Name # Start Date # Site #
#############################################
# 1        # Store 1    # 1/1/09     # 1    #
# 1        # Store 1    # 1/2/09     # 2    #
#          #            # 1/1/09     # 1    # <- NEWLY INSERTED ROW
#############################################
A: 

If I understand you correctly, when you call save() the data doesn't show up until you start a new conversation. I'm wondering if you have something going on with the @Transactional that's not ending until you start a new conversation. Does it work without it?

Jim Barrows
It saves the Offer just fine without the @Transactional, but it still doesn't display Offer specific things in the list of OfferSites.
JBristow
@Factory("offerSites") public void findAllOfferSites() I believe (based on the code youv'e shown, I'm correct) this method only gets called once per conversation, so the list of offer sites doesn't get refreshed until the next conversation.
Jim Barrows
A: 

Well, it turns out that when I was creating offerSite objects, I wasn't setting the offer field. So the persistence layer decided that despite the explicit fetch, it wouldn't actually need to hit the database to fill the list.

JBristow