1 Reply Latest reply on May 9, 2007 8:10 AM by pmuir

    Seam Hibernate Q

    tony.herstell1

      Background
      After a lot of poking around with log4j I can now see the hibernate debug comments (as the exceptions are telling me very little other than Advert was invalid!)

      After trapping the validation failure exception in Eclipse and looking at the message (as it really wasnt being put out by seam) it turned out that I had a
      @Length 30
      for an ENUM which it (Seam) really didn't like!
      Its quite exciting to see what Seam persists in the Database for the Enum!

       public enum AdvertType {
       Lineage ("label_ad_type_lineage"),
       BannerAd ("label_ad_type_banner_ad");
       private final String label;
       AdvertType(String label) {
       this.label = label;
       }
       public String getLabel() { return label; }
       }
      


      Bug
      Anyhow, this got me futher to persisting my object... which brings me to:

      I have a servies of things I expose to the client; that they can attatch to the object tree of my new object.
      These have been read in before into a session scoped seam bean so I assume they are detached!
      How do I manage to persist my object with these "hangers on"...

       /**
       * Utiltiy method to actually persist the Campaign.
       * TODO More error handling...
       */
       @TransactionAttribute(TransactionAttributeType.REQUIRED)
       private void save() {
       User currentUser = (User)em.createQuery("from User u where u.username = :username")
       .setParameter("username", Identity.instance().getUsername())
       .getSingleResult();
       advertisingCampaign.setAdvertiser(currentUser);
       // The targeting categoires are detached, I need to re-attatch them.
       em.merge(advertisingCampaign);
       em.persist(advertisingCampaign);
       }
      


      I am guessing merge and then persist but Hiberante does not like this.

      When I enter the merge I have (In a LinkedHashSet):
      [Category (Id => 0, Version => 0, Name Key (inl8) => category_everything, MaintenanceDates --> MaintenanceDates, Date Effective From => 9/05/2007, Date Effective To => null), Adminsitration --> Administration, Creation Date => 9/05/2007))]

      But by the time the exception happens this has gone to null.

      Any suggestions? I think I am doing this completely wrong.

        • 1. Re: Seam Hibernate Q
          pmuir

          You need to get managed references to the object - e.g. use entityManager.find() or entityManager.getReference(). Then add these manged references to what you want to persist.