0 Replies Latest reply on Jun 11, 2009 4:46 AM by Jeremy Hahn

    EntityHome Update Ignored for EmbeddedId

    Jeremy Hahn Newbie

      Hello,


      I have a many-to-many relationship between users and roles in my project. I can create and delete fine, however, my update call is being ignored (verified by watching  INFO STDOUT Hibernate: in log).


      Here are my entities --


      @Embeddable
      public class AppstoreUserRoleMapId implements java.io.Serializable {
      
              private long userId;
              private long roleId;
      
              public AppstoreUserRoleMapId() {
              }
      
              public AppstoreUserRoleMapId(long userId, long roleId) {
                      this.userId = userId;
                      this.roleId = roleId;
              }
      
              @Column(name = "userId", nullable = false)
              public long getUserId() {
                      return this.userId;
              }
      
              public void setUserId(long userId) {
                      this.userId = userId;
              }
      
              @Column(name = "roleId", nullable = false)
              public long getRoleId() {
                      return this.roleId;
              }
      
              public void setRoleId(long roleId) {
                      this.roleId = roleId;
              }
      
              public boolean equals(Object other) {
                      if ((this == other))
                              return true;
                      if ((other == null))
                              return false;
                      if (!(other instanceof AppstoreUserRoleMapId))
                              return false;
                      AppstoreUserRoleMapId castOther = (AppstoreUserRoleMapId) other;
      
                      return (this.getUserId() == castOther.getUserId())
                                      && (this.getRoleId() == castOther.getRoleId());
              }
      
              public int hashCode() {
                      int result = 17;
      
                      result = 37 * result + (int) this.getUserId();
                      result = 37 * result + (int) this.getRoleId();
                      return result;
              }
      
      }
      
      @Name("appstoreUserRoleMapHome")
      public class AppstoreUserRoleMapHome extends EntityHome<AppstoreUserRoleMap> {
      
              private static final long serialVersionUID = 2498650140430778232L;
              private static boolean roleSelected = false;
              private static boolean userSelected = false;
      
              @In(create = true)
              AppstoreUserHome appstoreUserHome;
      
              @In(create = true)
              AppstoreUserRoleHome appstoreUserRoleHome;
      
              public void setAppstoreUserRoleMapId(AppstoreUserRoleMapId id) {
                      setId(id);
              }
      
              public AppstoreUserRoleMapId getAppstoreUserRoleMapId() {
                      return (AppstoreUserRoleMapId) getId();
              }
      
              public AppstoreUserRoleMapHome() {
                      setAppstoreUserRoleMapId(new AppstoreUserRoleMapId());
              }
      
              @Override
              public boolean isIdDefined() {
                      if (getAppstoreUserRoleMapId().getRoleId() == 0)
                              return false;
                      if (getAppstoreUserRoleMapId().getUserId() == 0)
                              return false;
                      return true;
              }
      
              @Override
              protected AppstoreUserRoleMap createInstance() {
                      AppstoreUserRoleMap appstoreUserRoleMap = new AppstoreUserRoleMap();
                      appstoreUserRoleMap.setId(new AppstoreUserRoleMapId());
                      return appstoreUserRoleMap;
              }
      
              public void load() {
                      if (isIdDefined()) {
                              wire();
                      }
              }
      
              public void wire() {
                      getInstance();
                      AppstoreUser appstoreUser = appstoreUserHome.getDefinedInstance();
                      if (appstoreUser != null) {
                              getInstance().setAppstoreUser(appstoreUser);
                      }
                      AppstoreUserRole appstoreUserRole = appstoreUserRoleHome.getDefinedInstance();
                      if (appstoreUserRole != null) {
                              getInstance().setAppstoreUserRole(appstoreUserRole);
                      }
              }
      
              public boolean isWired() {
                      if (getInstance().getAppstoreUser() == null)
                              return false;
                      if (getInstance().getAppstoreUserRole() == null)
                              return false;
                      
                      if( !roleSelected || !userSelected ) return false;
      
                      return true;
              }
      
              public AppstoreUserRoleMap getDefinedInstance() {
                      return isIdDefined() ? getInstance() : null;
              }
      
              public void ValueChangeListener( ValueChangeEvent e ) {
      
                         String componentId = e.getComponent().getId();
                         Long id = Long.parseLong( e.getNewValue().toString() );
      
                         if( id.intValue() == 0 ) {
      
                                 if( componentId.equals( "roleId" ) )
                                         roleSelected = false;
                                 else if( componentId.equals( "userId" ) )
                                         userSelected = false;
      
                                 return;
                         }
                         
                         // Update event
                         if( this.isManaged() ) {
      
                                 getEntityManager().flush();
      
                                 if( componentId.equals( "roleId" ) ) {
      
                                         
                                         instance.getId().setRoleId( id );
                                         appstoreUserRoleHome.setId( Long.parseLong( e.getNewValue().toString() ) );
                                         roleSelected = true;
                                 }
                                 else if( componentId.equals( "userId" ) ) {
      
                                          
                                          instance.getId().setUserId( id );
                                          appstoreUserHome.setId( id );
                                          userSelected = true;
                                 }
                         }
      
                         // Add event
                         if( componentId.equals( "roleId" ) ) {
      
                                 instance.getId().setRoleId( id );
                                 appstoreUserRoleHome.setId( Long.parseLong( e.getNewValue().toString() ) );
                                 roleSelected = true;
                         }
                         else if( componentId.equals( "userId" ) ) {
      
                                  instance.getId().setUserId( id );
                                  appstoreUserHome.setId( id );
                                  userSelected = true;
                         }
      
                         System.out.println( "Set " + componentId + " to " + e.getNewValue().toString() );
              }
      }
      
      @Name("appstoreUserRoleMapList")
      public class AppstoreUserRoleMapList extends EntityQuery<AppstoreUserRoleMap> {
      
              private static final String EJBQL = "select appstoreUserRoleMap from AppstoreUserRoleMap appstoreUserRoleMap";
      
              private static final String[] RESTRICTIONS = {};
      
              private AppstoreUserRoleMap appstoreUserRoleMap;
              private AppstoreUser appstoreUser;
              private AppstoreUserRole appstoreUserRole;
      
              public AppstoreUserRoleMapList() {
      
                      appstoreUserRoleMap = new AppstoreUserRoleMap();
                      appstoreUserRoleMap.setId( new AppstoreUserRoleMapId() );
                      setEjbql(EJBQL);
                      setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
                      setMaxResults(25);
      
                      appstoreUser = new AppstoreUser();
                      appstoreUser.setId( appstoreUserRoleMap.getId().getUserId() );
      
              appstoreUserRole = new AppstoreUserRole();
                      appstoreUserRole.setId( appstoreUserRoleMap.getId().getRoleId() );
              }
      
              public AppstoreUserRoleMap getAppstoreUserRoleMap() {
                      return appstoreUserRoleMap;
              }
      
              public List<SelectItem> getUsers() {
      
                         setEjbql( EJBQL );
      
                         List<SelectItem> items = new ArrayList<SelectItem>();
                        
                         for( AppstoreUserRoleMap urm : getResultList() ) {
      
                                  urm.getAppstoreUser().setId( urm.getId().getUserId() );
      
                                  SelectItem item = new SelectItem();
                                  item.setValue( urm.getId().getUserId() );
                                  item.setLabel( urm.getAppstoreUser().getUsername() );
      
                                  items.add( item );
                         }
      
                         return items;
              }
      }
      



      And here is my edit page.xml and xhtml files...




      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
                                   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                      xmlns:s="http://jboss.com/products/seam/taglib"
                      xmlns:ui="http://java.sun.com/jsf/facelets"
                      xmlns:f="http://java.sun.com/jsf/core"
                      xmlns:h="http://java.sun.com/jsf/html"
                      xmlns:ice="http://www.icesoft.com/icefaces/component" 
                      template="layout/template.xhtml">
                             
      <ui:define name="body">
          
          <ice:form id="appstoreUserRoleMap" styleClass="edit">
           <ice:panelGroup  id="editappstoreUserRoleMapGroupId" styleClass="formBorderHighlight">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td class="iceDatTblColHdr2">
                                  <ice:outputText id="editTextBoxId" value="#{appstoreUserRoleMapHome.managed ? 'Edit' : 'Add'} Role Assignment"/>
                        </td>
                    </tr>
                </table>
      
              <ice:panelGroup id="editPanelGroupId" styleClass="edit">
      
                  <s:decorate id="roleIdField" template="layout/edit.xhtml">
                      <ui:define name="label">Role</ui:define>
                      <ice:selectOneMenu id="roleId" value="#{appstoreUserRoleMapHome.appstoreUserRoleMapId.roleId}"
                                         partialSubmit="true" valueChangeListener="#{appstoreUserRoleMapHome.ValueChangeListener}">
                         <f:selectItems value="#{appstoreUserRoleList.roles}"/>
                      </ice:selectOneMenu>
                  </s:decorate>
      
                  <s:decorate id="userIdField" template="layout/edit.xhtml">
                      <ui:define name="label">User</ui:define>
                      <ice:selectOneMenu id="userId" value="#{appstoreUserRoleMapHome.appstoreUserRoleMapId.userId}" partialSubmit="true"
                                         valueChangeListener="#{appstoreUserRoleMapHome.ValueChangeListener}">
                          <f:selectItems value="#{appstoreUserList.users}"/>
                      </ice:selectOneMenu>
                   </s:decorate>
                </ice:panelGroup>
             </ice:panelGroup>
                        
             <div class="actionButtons">
              
                  <ice:commandButton id="save" value="Save" action="#{appstoreUserRoleMapHome.persist}" disabled="#{!appstoreUserRoleMapHome.wired}"
                                         rendered="#{!appstoreUserRoleMapHome.managed}"/>  
      
                  <ice:commandButton id="update" value="Update" action="#{appstoreUserRoleMapHome.update}" rendered="#{appstoreUserRoleMapHome.managed}"/>
      
                  <ice:commandButton id="delete" value="Delete" action="#{appstoreUserRoleMapHome.remove}" immediate="true"
                                     rendered="#{appstoreUserRoleMapHome.managed}"/>
                          
                  <s:button id="cancelEdit" styleClass="iceCmdBtn" value="Cancel" propagation="end" view="/AppstoreUserRoleMap.xhtml"
                                rendered="#{appstoreUserRoleMapHome.managed}"/>
                      
                  <s:button id="cancelAddappstoreUserRoleMapHome" styleClass="iceCmdBtn" value="Cancel" propagation="end"
                            view="/#{empty appstoreUserRoleMapFrom ? 'AppstoreUserRoleMapList' : appstoreUserRoleMapFrom}.xhtml"
                                rendered="#{!appstoreUserRoleMapHome.managed}"/>
              </div>
              
          </ice:form>
      
       <ice:form id="form2appstoreUserRoleMapHome">  
      
         <ice:panelTabSet id="editPanelTabappstoreUserRoleMapHomeId" styleClass="componentPanelTabSetLayout" style="margin-bottom:5px;margin-top:10px;">
      
              <ice:panelTab id="editTabappstoreUserId" label="User Details" >
      
                      <div class="association" id="appstoreUserParent">
      
                      <h:outputText id="editappstoreUserTextId" value="The user has not been defined. Please select a user from the drop down menu." 
                                        rendered="#{appstoreUserRoleMapHome.instance.appstoreUser == null}"/>
      
              <ice:dataTable var="appstoreUser" 
                                 value="#{appstoreUserRoleMapHome.instance.appstoreUser}" 
                                 rendered="#{appstoreUserRoleMapHome.instance.appstoreUser != null}"
                                 rowClasses="rvgRowOne,rvgRowTwo"
                                                 columnClasses="allCols"
                             id="editappstoreUserTableId">
      
                  <ice:column id="$editColumnusernameId">
                      <f:facet name="header">Username</f:facet>
                      #{appstoreUser.username}
                  </ice:column>
      
                  <ice:column id="$editColumnpasswordId">
                      <f:facet name="header">Password</f:facet>
                      #{appstoreUser.password}
                  </ice:column>
      
                  <ice:column id="$editColumncreatedId">
                      <f:facet name="header">Created</f:facet>
                      #{appstoreUser.created}
                  </ice:column>
      
                  <ice:column id="$editColumnlastLoginId">
                      <f:facet name="header">Last login</f:facet>
                      #{appstoreUser.lastLogin}
                  </ice:column>
      
                  <ice:column id="$editColumnapiKeyId">
                      <f:facet name="header">API Key</f:facet>
                      #{appstoreUser.apiKey}
                  </ice:column>
      
                  <ice:column id="$editColumnapiEnabledId">
                      <f:facet name="header">API Enabled</f:facet>
                      #{appstoreUser.apiEnabled}
                  </ice:column>
      
                  <ice:column id="editColumnappstoreUserLinkId">
                      <f:facet name="header">Action</f:facet>
                      <s:link view="/AppstoreUser.xhtml" id="viewappstoreUser" value="View" propagation="none">
                          <f:param name="appstoreUserId" value="#{appstoreUser.id}"/>
                      </s:link>
                  </ice:column>
            </ice:dataTable>        
          </div>
          </ice:panelTab>
         
              <ice:panelTab id="editTabappstoreUserRoleId" label="Role Details" >
                      <div class="association" id="appstoreUserRoleParent">
          
              <h:outputText id="editappstoreUserRoleTextId" value="The role has not been defined. Please select a role from the drop down menu." 
                            rendered="#{!appstoreUserRoleMapHome.wired}"/>
      
              <ice:dataTable var="appstoreUserRole"
                             value="#{appstoreUserRoleMapHome.instance.appstoreUserRole}" 
                             rendered="#{appstoreUserRoleMapHome.wired or appstoreUserRoleMapHome.managed}"
                             rowClasses="rvgRowOne,rvgRowTwo"
                                     columnClasses="allCols"
                             id="editappstoreUserRoleTableId">
      
                  <ice:column id="$editColumnnameId">
                      <f:facet name="header">Name</f:facet>
                      #{appstoreUserRole.name}
                  </ice:column>
      
                  <ice:column id="$editColumndescriptionId">
                      <f:facet name="header">Description</f:facet>
                      #{appstoreUserRole.description}
                  </ice:column>
      
                  <ice:column id="editColumnappstoreUserRoleLinkId">
                      <f:facet name="header">Action</f:facet>
                      <s:link view="/AppstoreUserRole.xhtml" id="viewappstoreUserRole" value="View" propagation="none">
                         <f:param name="appstoreUserRoleId" value="#{appstoreUserRole.id}"/>
                      </s:link>
                  </ice:column>
            </ice:dataTable>
           </div>
          </ice:panelTab>
         </ice:panelTabSet>
        </ice:form>
       </ui:define>
      </ui:composition>
      
      
      
      
      <?xml version="1.0" encoding="UTF-8"?>
      
      <page xmlns="http://jboss.com/products/seam/pages"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.1.xsd"
            no-conversation-view-id="/AppstoreUserRoleMapList.xhtml"
            login-required="true">
         
         <begin-conversation join="true" flush-mode="MANUAL"/>
         
         <action execute="#{appstoreUserRoleMapHome.wire}"/>
         
         <param name="appstoreUserRoleMapFrom"/>
         <param name="appstoreUserRoleMapRoleId" value="#{appstoreUserRoleMapHome.appstoreUserRoleMapId.roleId}"/>
         <param name="appstoreUserRoleMapUserId" value="#{appstoreUserRoleMapHome.appstoreUserRoleMapId.userId}"/>
         <param name="appstoreUserFrom"/>
         <param name="appstoreUserId" value="#{appstoreUserHome.appstoreUserId}"/>
      
         <param name="appstoreUserRoleFrom"/>
         <param name="appstoreUserRoleId" value="#{appstoreUserRoleHome.appstoreUserRoleId}"/>
      
         <navigation from-action="#{appstoreUserRoleMapHome.persist}">
             <rule if-outcome="persisted">
                <end-conversation/>
                <redirect view-id="/AppstoreUserRoleMap.xhtml"/>
             </rule>
         </navigation>
         
         <navigation from-action="#{appstoreUserRoleMapHome.update}">
             <rule if-outcome="updated">
                <end-conversation/>
                <redirect view-id="/AppstoreUserRoleMap.xhtml"/>
             </rule>
         </navigation>
         
         <navigation from-action="#{appstoreUserRoleMapHome.remove}">
             <rule if-outcome="removed">
                <end-conversation/>
                <redirect view-id="/AppstoreUserRoleMapList.xhtml"/>
             </rule>
         </navigation>
         
      </page>
      







      Any ideas? Thanks!