This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 67162 - Generate Code to Automate Session Bean access to Entity Bean
Summary: Generate Code to Automate Session Bean access to Entity Bean
Status: RESOLVED WONTFIX
Alias: None
Product: javaee
Classification: Unclassified
Component: EJB (show other bugs)
Version: 5.x
Hardware: All All
: P3 blocker (vote)
Assignee: martin_adamek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-10-20 08:01 UTC by agyepong
Modified: 2016-07-07 08:53 UTC (History)
0 users

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description agyepong 2005-10-20 08:01:57 UTC
I have developed a tool that generates code for entity and session beans much
like what NetBeans does. What is missing from Net Beans, however, is the ability
to generate session bean methods that accesses the entity bean from the session
bean.

By adding the following methods to NetBeans' generated Entity Bean and Session
Bean classes we can automate access (create, update, delete and get) of our
entity beans from the Session Bean. The following is just a simple example taken
from a lookup table for a make of a car. I have generated more complex code with
the tool.

Adding a detail class simplifies the ejbCreate method call.

NetBeans already has the pieces in place to generate this code. It will take a
day or so to add this feature but the benefit will be a boom to EJB developers.

Entity Bean Methods

    public makeDetail getMake();
    public void updateMake( makeDetail detail );
    public void copyEjbToDetail( makeDetail detail );
    public String ejbCreate( makeDetail detail );

Session Bean Methods

    public String createMake( makeDetail detail ) throws atException;
    public void deleteMake( String key ) throws atException;
    public makeDetail getMake( String key ) throws atException;
    public java.util.Collection getMake() throws atException;
    public void updateMake( makeDetail detail ) throws atException


Detail Class Code Example

makeDetail.java

package com.akantech.gcvtrack.common;

/*
 * makeDetail.java
 * 
 * Created on 5/12/2005 18:39:5
 */
import java.beans.*;

/**
 * 
 * @author agyepong
 */

public class makeDetail extends Object implements java.io.Serializable, Cloneable {

    /** Holds value of property key. */
    private String key;

    /** Holds value of property item. */
    private String item;


    /** Creates a new instance of makeDetail */
    public makeDetail() {
      super();
    }

    public makeDetail( String key,
                       String item
     ) {
      setKey( key );
      setItem( item );
    }    

    public Object clone() {
      try {
        makeDetail detail = (makeDetail)super.clone();
        return detail;
      } catch (CloneNotSupportedException e) {
        return null;
      }
    }

    /** Getter for property key.
     *  @return Value of property key.
     */
    public String getKey() {
      return this.key;
    }

    /** Setter for property key.
     *  @param key New value of property key.
     */
    public void setKey( String key ) {
      this.key = key;
    }

    /** Getter for property item.
     *  @return Value of property item.
     */
    public String getItem() {
      return this.item;
    }

    /** Setter for property item.
     *  @param item New value of property item.
     */
    public void setItem( String item ) {
      this.item = item;
    }

    public String toString() {
      StringBuffer buf = new StringBuffer(1000);

      buf.append( getKey() ); buf.append(" ");
      buf.append( getItem() ); buf.append(" ");

      return buf.toString();
    }
}


Entity Bean Code Example
 
MakeBean.java

package com.akantech.gcvtrack.ejb;

import javax.ejb.*;

import com.akantech.gcvtrack.common.*;

/**
 * This is the bean class for the MakeBean enterprise bean.
 * Created Oct 14, 2005 2:31:04 PM
 * @author agyepong
 */
public abstract class MakeBean implements javax.ejb.EntityBean,
com.akantech.gcvtrack.ejb.MakeLocalBusiness {
    private javax.ejb.EntityContext context;
    
    // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods.
Click on the + sign on the left to edit the code.">
    // TODO Consider creating Transfer Object to encapsulate data
    // TODO Review finder methods
    /**
     * @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
     */
    public void setEntityContext(javax.ejb.EntityContext aContext) {
        context = aContext;
    }
    
    /**
     * @see javax.ejb.EntityBean#ejbActivate()
     */
    public void ejbActivate() {
        
    }
    
    /**
     * @see javax.ejb.EntityBean#ejbPassivate()
     */
    public void ejbPassivate() {
        
    }
    
    /**
     * @see javax.ejb.EntityBean#ejbRemove()
     */
    public void ejbRemove() {
        
    }
    
    /**
     * @see javax.ejb.EntityBean#unsetEntityContext()
     */
    public void unsetEntityContext() {
        context = null;
    }
    
    /**
     * @see javax.ejb.EntityBean#ejbLoad()
     */
    public void ejbLoad() {
        
    }
    
    /**
     * @see javax.ejb.EntityBean#ejbStore()
     */
    public void ejbStore() {
        
    }
    // </editor-fold>
    
    
    public abstract java.lang.String getKey();
    public abstract void setKey(java.lang.String key);
    
    public abstract java.lang.String getItem();
    public abstract void setItem(java.lang.String item);
    
    public abstract java.util.Collection getGcvtrackBean();
    public abstract void setGcvtrackBean(java.util.Collection gcvtrackBean);
    
    public String ejbCreate( makeDetail detail ) throws CreateException {

      setKey( detail.getKey() );
      updateMake( detail );
      return null;
    }
    
    public void ejbPostCreate( makeDetail detail ) {
        // TODO populate relationships here if appropriate
    }
    
    public java.lang.String ejbCreate(java.lang.String key, java.lang.String
item)  throws javax.ejb.CreateException {
        if (key == null) {
            throw new javax.ejb.CreateException("The field \"key\" must not be
null");
        }
        if (item == null) {
            throw new javax.ejb.CreateException("The field \"item\" must not be
null");
        }
        
        // TODO add additional validation code, throw CreateException if data is
not valid
        setKey(key);
        setItem(item);
        
        return null;
    }
    
    public void ejbPostCreate(java.lang.String key, java.lang.String item) {
        // TODO populate relationships here if appropriate
    }
    
    // Business methods

    public makeDetail getMake() {
      makeDetail detail = new makeDetail();
      copyEjbToDetail( detail );

      return detail;
    }

    public void copyEjbToDetail( makeDetail detail ) {
      detail.setKey( getKey() );
      detail.setItem( getItem() );
    }

    public void updateMake( makeDetail detail ) {
      //setKey( detail.getKey() );  Cannot update primary key field(s)
      setItem( detail.getItem() );
    }
    
}


MakeLocal.java

package com.akantech.gcvtrack.ejb;

import com.akantech.gcvtrack.common.*;

/**
 * This is the local interface for Make enterprise bean.
 */
public interface MakeLocal extends javax.ejb.EJBLocalObject,
com.akantech.gcvtrack.ejb.MakeLocalBusiness {
    public makeDetail getMake();
    public void updateMake( makeDetail detail );
    public void copyEjbToDetail( makeDetail detail );
    
}


MakeLocalHome.java

package com.akantech.gcvtrack.ejb;

import java.util.Collection;

import com.akantech.gcvtrack.common.*;

/**
 * This is the local-home interface for Make enterprise bean.
 */
public interface MakeLocalHome extends javax.ejb.EJBLocalHome {
    
    /**
     */
    com.akantech.gcvtrack.ejb.MakeLocal findByPrimaryKey(java.lang.String key) 
throws javax.ejb.FinderException;

    public MakeLocal create( makeDetail detail ) throws javax.ejb.CreateException
    public com.akantech.gcvtrack.ejb.MakeLocal create(java.lang.String key,
java.lang.String item) throws javax.ejb.CreateException;;

    Collection findAll() throws javax.ejb.FinderException;
}


Seesion Bean Code

public class gcvtrackfacadeBean implements javax.ejb.SessionBean {
   private MakeLocalHome makeHome;

   public String createMake( makeDetail detail ) throws atException {
        if ( detail == null ) {
            throw new atException( 10, "Incomplete Make request" );
        }
        try {
            MakeLocal home = makeHome.create( detail );
            
            return detail.getKey();
        } catch (CreateException e) {
            throw new atException( 10,"Create make failed: " + e.getMessage() );
        } catch (EJBException e) {
            throw new atException( 10,"General exception: " + e.getMessage() );
        } catch (Exception e) {
            throw new atException( 10,"Create remote exception: " +
e.getMessage() );
        }
    }

    public void updateMake( makeDetail detail ) throws atException {
        try {
            MakeLocal home = makeHome.findByPrimaryKey( detail.getKey() );
            home.updateMake( detail );
            
        } catch (FinderException e) {
            throw new atException( 10, "Record does not exist: " + e.getMessage());
        } catch (EJBException e) {
            throw new atException( 10, "EJBException during Update: " +
e.getMessage());
        } catch (Exception e) {
            throw new atException( 10,"Update remote exception: " +
e.getMessage() );
        }
    }

    public makeDetail getMake( String key ) throws atException {
        try {
            MakeLocal home = makeHome.findByPrimaryKey( key );
            makeDetail detail = (makeDetail)home.getMake();
            
            return detail;
        } catch (FinderException e) {
            throw new atException( 10, "Record does not exist: " + e.getMessage() );
        } catch (EJBException e) {
            throw new atException( 10, "General exception: " + e.getMessage() );
        } catch (Exception e) {
            throw new atException( 10,"Get remote exception: " + e.getMessage() );
        }
    }

    public java.util.Collection getMake() throws atException {
        try {
            Collection items = makeHome.findAll();
            
            return copyMakeToDetails( items );
        } catch (FinderException e) {
            System.out.println("getMake() FinderException");
            throw new atException( 10, "Records do not exist: " + e.getMessage() );
        } catch (EJBException e) {
            System.out.println("getMake() EJBException");
            throw new atException( 10, "General exception: " + e.getMessage() );
        } catch (Exception e) {
            System.out.println("getMake() Exception");
            throw new atException( 10,"Get collection remote exception: " +
e.getMessage() );
        }
    }

    public void deleteMake( String key ) throws atException {
        try {
            MakeLocal home = makeHome.findByPrimaryKey( key );
            home.remove();
            
        } catch (FinderException e) {
            throw new atException( 10, "Record does not exist: " + e.getMessage() );
        } catch (EJBException e) {
            throw new atException( 10, "General exception: " + e.getMessage() );
        } catch (RemoveException e) {
            throw new atException( 10, "Delete failed: " + e.getMessage() );
        } catch (Exception e) {
            throw new atException( 10,"Delete remote exception: " +
e.getMessage() );
        }
    }

    private ArrayList copyMakeToDetails( Collection ejBeans ) {
        ArrayList detailsList = new ArrayList();
        Iterator i = ejBeans.iterator();
        try {
            while ( i.hasNext() ) {
                MakeLocal ejbInst = (MakeLocal) i.next();
                makeDetail details = new makeDetail();
                ejbInst.copyEjbToDetail( details );
                detailsList.add( details );
            }
        } catch (Exception e) {
            System.out.println( "copyMakeToDetails: error: " + e.getMessage() );
        }
        
        return detailsList;
    }
}

Local Interface

public interface gcvtrackfacadeLocal extends javax.ejb.EJBLocalObject {
    public String createMake( makeDetail detail ) throws atException;
    public void deleteMake( String key ) throws atException;
    public makeDetail getMake( String key ) throws atException;
    public java.util.Collection getMake() throws atException;
    public void updateMake( makeDetail detail ) throws atException;
}

Remote Interface

public interface gcvtrackfacadeRemote extends javax.ejb.EJBObject {
    public String createMake( makeDetail detail ) throws atException,
RemoteException;
    public void deleteMake( String key ) throws atException, RemoteException;
    public makeDetail getMake( String key ) throws atException, RemoteException;
    public java.util.Collection getMake() throws atException, RemoteException;
    public void updateMake( makeDetail detail ) throws atException, RemoteException;
}
Comment 1 Martin Balin 2016-07-07 08:53:25 UTC
This old bug may not be relevant anymore. If you can still reproduce it in 8.2 development builds please reopen this issue.

Thanks for your cooperation,
NetBeans IDE 8.2 Release Boss