6 Replies Latest reply on May 24, 2007 11:05 AM by kingcu

    Need some clarification about seam-gen-ed EntityHome class

    kingcu

      Hi,

      I used "seam generate-entities" to generate a complete CRUD application from an existing database schema. Everything works great! I just need some clarification/education to better understand EntityHome.

      Here is the background description: there is a bidirectional one-to-many relation between two entities, Widget (1) <---> Feed (n), entity Widget on the one side and entity Feed on the many side. Below are the seam generated classes extending EntityHome.

      So, my questions are:

      1. Why isWired() always returns true? In other words, is there any situation that this method needs to return false?

      2. wire() seems to be the method to "wire up" the correct entity relation, but it's empty in WidgetHome class, even though this is a bidirectional relation and both sides need to be wired, why?

      3. the wire() method is only referenced in page.xml as a page action (I can't find a complete description about page.xml anywhere, but I guess the purpose of action element is to call some method on page load, right?), the creation/refreshing of the home component seems unclear to me.

      Thanks.

      WidgetHome.java

      package com.kfi.was.entity;
      
      import java.math.BigDecimal;
      import java.util.ArrayList;
      import java.util.List;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.framework.EntityHome;
      
      @Name("widgetHome")
      public class WidgetHome extends EntityHome<Widget> {
      
       public void setWidgetId(BigDecimal id) {
       setId(id);
       }
      
       public BigDecimal getWidgetId() {
       return (BigDecimal) getId();
       }
      
       @Override
       protected Widget createInstance() {
       Widget widget = new Widget();
       return widget;
       }
      
       public void wire() {
       }
      
       public boolean isWired() {
       return true;
       }
      
       public Widget getDefinedInstance() {
       return isIdDefined() ? getInstance() : null;
       }
      
       public List<Feed> getFeeds() {
       return getInstance() == null ? null : new ArrayList<Feed>(getInstance()
       .getFeeds());
       }
      
      }
      


      FeedHome.java
      package com.kfi.was.entity;
      
      import java.math.BigDecimal;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.framework.EntityHome;
      
      @Name("feedHome")
      public class FeedHome extends EntityHome<Feed> {
      
       @In(create = true)
       WidgetHome widgetHome;
      
       public void setFeedId(BigDecimal id) {
       setId(id);
       }
      
       public BigDecimal getFeedId() {
       return (BigDecimal) getId();
       }
      
       @Override
       protected Feed createInstance() {
       Feed feed = new Feed();
       return feed;
       }
      
       public void wire() {
       Widget widget = widgetHome.getDefinedInstance();
       if (widget != null) {
       getInstance().setWidget(widget);
       }
       }
      
       public boolean isWired() {
       return true;
       }
      
       public Feed getDefinedInstance() {
       return isIdDefined() ? getInstance() : null;
       }
      
      }
      


        • 1. Re: Need some clarification about seam-gen-ed EntityHome cla
          gavin.king

          You only "wire" things from the many end of a one-to-many association.

          If there are no -to-one associations originating at the entity, isWired() always returns true, and wire() is empty.

          • 2. Re: Need some clarification about seam-gen-ed EntityHome cla
            kingcu

             

            "gavin.king@jboss.com" wrote:
            You only "wire" things from the many end of a one-to-many association.

            If there are no -to-one associations originating at the entity, isWired() always returns true, and wire() is empty.


            But if there are -to-one associations originating at the entity, does isWired() still always return true? In my above situation, the Feed entity has a many-to-one to Widget entity, but FeedHome.isWired() simply returns true. So it seems to me that isWired() always returns true no matter what association the entity has. Can you clarify? Thanks.


            • 3. Re: Need some clarification about seam-gen-ed EntityHome cla
              gavin.king

              No true, it looks to me like Widget has many Feeds.

              • 4. Re: Need some clarification about seam-gen-ed EntityHome cla
                kingcu

                Yes, one Widget has many Feeds. But, both WidgetHome and FeedHome's isWired() (generated by seam-gen) simply return true, that's where I am confused. To me, FeedHome.isWired() should be something like:

                return getInstance().getWidget() != null;


                • 5. Re: Need some clarification about seam-gen-ed EntityHome cla
                  gavin.king

                  Depends upon whether the attribute is optional or not. If the FK column is nullable, an entity can be saved without a value for the FK.

                  You can see all this stuff by looking at the template:

                  public void wire()
                   {
                  <#foreach property in pojo.allPropertiesIterator>
                  <#if c2h.isManyToOne(property)>
                  <#assign parentPojo = c2j.getPOJOClass(cfg.getClassMapping(property.value.referencedEntityName))>
                  <#if parentPojo.shortName!=pojo.shortName>
                  <#assign parentHomeName = util.lower(parentPojo.shortName) + "Home">
                  <#assign setter = "set" + pojo.getPropertyName(property)>
                   ${parentPojo.shortName} ${property.name}=${parentHomeName}.getDefinedInstance();
                   if ( ${property.name}!=null )
                   {
                   getInstance().${setter}(${property.name});
                   }
                  </#if>
                  </#if>
                  </#foreach>
                   }
                  
                   public boolean isWired()
                   {
                  <#foreach property in pojo.allPropertiesIterator>
                  <#if (c2h.isManyToOne(property) && !property.optional)>
                  <#assign getter = pojo.getGetterSignature(property)>
                   if ( getInstance().${getter}()==null ) return false;
                  </#if>
                  </#foreach>
                   return true;
                   }


                  • 6. Re: Need some clarification about seam-gen-ed EntityHome cla
                    kingcu

                    Thanks a lot, Gavin. That really helps.

                    BTW, could you please comment on my other post about using EntityHome for nested entities?

                    http://www.jboss.com/index.html?module=bb&op=viewtopic&t=109328