hi ,
thanxs for ur help , well what i feel that there is no problem with the set/unset Entity Context Method .
i am pasting a bean code and let me know if it requries any changes.
Any sort help help be appricated
thanxs
Deepak
***********************************************
/**
* Sets the EntityContext for the EJBean.
*
* @param EntityContext ctx
* @exception java.rmi.RemoteException
*/
public void setEntityContext(EntityContext ctx)
throws RemoteException
{
log("setEntityContext called of GroupUserBean");
this.ctx = ctx;
iGroupUser = new GroupUserData();
jndi_Name = getJNDIname();
}
/**
* Unsets the EntityContext for the EJBean.
* @exception java.rmi.RemoteException
*/
public void unsetEntityContext()
throws RemoteException
{
log("unsetEntityContext called of GroupUserBean");
this.ctx = null;
}
public void ejbActivate()
throws RemoteException
{
log("ejbActivate called of GroupUserBean");
}
public void ejbPassivate()
throws RemoteException
{
log("ejbPassivate called of GroupUserBean");
}
public void ejbLoad()throws RemoteException
{
log("ejbLoad: called of GroupUserBean");
Connection con = null;
PreparedStatement ps = null;
GroupUserPK pk = (GroupUserPK) ctx.getPrimaryKey();
try {
con = getConnection();
ps = con.prepareStatement("select * from TR_GROUP_USER where GROUP_ID = ? and USER_ID = ?");
ps.setLong(1,pk.getGroupsID());
ps.setLong(2,pk.getUserID());
ps.executeQuery();
ResultSet rs = ps.getResultSet();
if (rs.next())
{
iGroupUser.setGroupsID(rs.getLong(1));
iGroupUser.setUserID(rs.getLong(2));
}
else
{
String error = "ejbLoad: GroupUserBean (" + pk.getGroupsID() + pk.getUserID() + ") not found";
log(error);
throw new NoSuchEntityException (error);
}
}
catch (SQLException sqe)
{
log("SQLException: " + sqe);
throw new EJBException(sqe);
}
finally
{
cleanup(con, ps);
}
}
/**
* Stores the EJBean in the persistent storage.
*
* @exception javax.ejb.NoSuchEntityException
* if the bean is not found in the database
* @exception javax.ejb.EJBException
* if there is a communications or systems failure
* @exception java.rmi.RemoteException
*/
public void ejbStore()throws RemoteException
{
log("ejbStore called of GroupUserBean");
Connection con = null;
PreparedStatement ps = null;
try {
GroupUserPK pk = (GroupUserPK) ctx.getPrimaryKey();
con = getConnection();
ps = con.prepareStatement("UPDATE TR_GROUP_USER set GROUP_ID=?,USER_ID=?"
+ "where GROUP_ID = ? and USER_ID = ?");
ps.setLong(1,iGroupUser.groupsID());
ps.setLong(2,iGroupUser.userID());
ps.setLong(3,pk.getGroupsID());
ps.setLong(4,pk.getUserID());
if (!(ps.executeUpdate() > 0))
{
String error = "ejbStore: GroupUserBean (" + pk.getGroupsID() +pk.getUserID() + ") not updated";
log(error);
throw new NoSuchEntityException (error);
}
}
catch(SQLException sqe)
{
log("SQLException: " + sqe);
throw new EJBException (sqe);
}
finally
{
cleanup(con, ps);
}
}
/**
* This method takes the same arguments as the create method.
* The container calls it after ejbCreate() completes.
* Use this method for any special processing needed after
* the bean is created, but before it becomes available
* to the client. Leave empty if no special processing
* is needed.
*
* @param long _groupID
* @param long _userID
*
* @exception java.rmi.RemoteException
*/
public void ejbPostCreate(long _group,long _userID) throws CreateException,RemoteException,NamingException,FinderException
{
log("ejbPostCreate called of GroupUserBean");
}
/**
* This method corresponds to the create method in the home interface
* "GroupUserHome.java".
* The parameter sets of the two methods are identical. When the client calls
* GroupUserHome.create(), the container allocates an instance
* of this bean and calls GroupUserBean.ejbCreate().
*
* For bean-managed persistence, ejbCreate() returns
* a primary key, unlike the case of container-managed
* persistence, where it returns a void.
*
*
* @param long _groupsID
* @param long _userID
*
* @return GroupUserPK
*
* @exception javax.ejb.CreateException
* if there is a problem creating the bean
* @exception javax.ejb.DuplicateKeyException
* if a create is attempted using a Primary Key
* already in the database
* @exception javax.ejb.EJBException
* if there is a communications or systems failure
* @exception java.rmi.RemoteException
*
* @exception javax.ejb.NamingException
*/
public GroupUserPK ejbCreate(long _groupsID,long _userID)
throws CreateException,RemoteException,NamingException,FinderException
{
log("GroupUserBean.ejbCreate()");
Connection con = null;
PreparedStatement ps = null;
try {
con = getConnection();
ps = con.prepareStatement("INSERT INTO TR_GROUP_USER VALUES (?,?)");
ps.setLong(1,_groupsID);
ps.setLong(2,_userID);
if (ps.executeUpdate() != 1)
{
String error = "JDBC did not create any row";
log(error);
throw new CreateException (error);
}
else
{
iGroupUser.setGroupsID(_groupsID);
iGroupUser.setUserID(_userID);
}
return new GroupUserPK(_groupsID,_userID);
}
catch (SQLException sqe)
{
// Check to see if this SQLException is due to a unique constraint
// violation on our database table . If so, throw a
// DuplicateKeyException else throw a CreateException.
try {
ejbFindByPrimaryKey(_groupsID,_userID);
}
catch(ObjectNotFoundException onfe)
{
String error = "SQLException: " + sqe;
log(error);
throw new CreateException (error);
}
String error = "An GroupUserBean already exists in the database with Composite Primary Key " + _groupsID + _userID;
log(error);
throw new DuplicateKeyException(error);
}
finally
{
cleanup(con, ps);
}
}
/**
* Deletes the EJBean from the persistent storage.
*
* @exception javax.ejb.NoSuchEntityException
* if the bean is not found in the database
* @exception javax.ejb.EJBException
* if there is a communications or systems failure
* @exception java.rmi.RemoteException
*
* @exception java.rmi.RemoveException
*/
public void ejbRemove()throws RemoteException,RemoveException
{
log("ejbRemove called of GroupUserBean");
// we need to get the primary key from the Context because
// it is possible to do a remove right after a find, and
// ejbLoad may not have been called.
Connection con = null;
PreparedStatement ps = null;
try
{
con = getConnection();
GroupUserPK pk = (GroupUserPK)ctx.getPrimaryKey();
String message = "GroupUserBean (" + pk.getGroupsID() + pk.getUserID() + " remove ";
log(message);
ps = con.prepareStatement("DELETE FROM TR_GROUP_USER where GROUP_ID = ? AND USER_ID=?");
ps.setLong(1, pk.getGroupsID());
ps.setLong(2, pk.getUserID());
if (!(ps.executeUpdate() > 0))
{
String error = "GroupUserBean (" + pk.getGroupsID() + pk.getUserID() + " not found";
log(error);
throw new NoSuchEntityException (error);
}
}
catch (SQLException sqe)
{
log("SQLException: " + sqe);
throw new EJBException (sqe);
}
finally
{
cleanup(con, ps);
}
}
/**
* Attempts to find the EJBean with a given Primary Key from
* the persistent storage.
*
* @param long _groupsID
* @param long _userID
*
* @return GroupUserPK
* @exception javax.ejb.ObjectNotFoundException
* thrown if the EJBean cannot be found
* @exception javax.ejb.EJBException
* if there is a communications or systems failure
* @exception java.rmi.RemoteException
*/
public GroupUserPK ejbFindByPrimaryKey(long _groupsID,long _userID)throws RemoteException,FinderException
{
log("ejbFindByPrimaryKey (" + _groupsID + "," + _userID + ")");
Connection con = null;
PreparedStatement ps = null;
try {
con = getConnection();
ps = con.prepareStatement("SELECT * FROM TR_GROUP_USER WHERE GROUP_ID = ? AND USER_ID = ? ");
ps.setLong(1, _groupsID);
ps.setLong(2, _userID);
ps.executeQuery();
ResultSet rs = ps.getResultSet();
if (rs.next())
{
iGroupUser.setGroupsID(rs.getLong(1));
iGroupUser.setUserID(rs.getLong(2));
}
else
{
String error = "ejbFindByPrimaryKey: GroupUserBean (" + _groupsID + _userID + ") not found";
log(error);
}
}
catch (SQLException sqe)
{
log("SQLException: " + sqe);
throw new EJBException (sqe);
}
finally
{
cleanup(con, ps);
}
log("ejbFindByPrimaryKey (" + _groupsID + ","+ _userID + ") found");
return new GroupUserPK(_groupsID ,_userID);
}
/**
* Attempts to find a Long groupID for a given userID
*
* @param long _userID
*
* @return Groups
* @exception javax.ejb.FinderException
* if there is an error finding the bean
* @exception java.rmi.RemoteException
* if there is a communications or systems failure
*/
public GroupUserPK ejbFindByUserGroupID(long _userID)
throws FinderException, RemoteException
{
log("ejbFindByUserGroupID("+ _userID + ")called of GroupUserBean");
log("test");
Connection con = null;
PreparedStatement ps = null;
long groupID = 0;
try {
con = getConnection();
ps = con.prepareStatement("select Group_ID from TR_GROUP_USER where User_ID = ?");
ps.setLong(1,_userID);
ps.executeQuery();
ResultSet rs = ps.getResultSet();
if (rs.next())
{
groupID = rs.getLong(1);
log(groupID + " GroupID");
}
else
{
String error = "ejbFindByGroupUserID: GroupUserBean (" + _userID + ") not found";
log(error);
}
} catch (SQLException sqe)
{
log("SQLException: " + sqe);
throw new EJBException (sqe);
}
finally
{
cleanup(con, ps);
}
log("ejbFindByUserGroupID found");
return new GroupUserPK(groupID,_userID);
}
/**
* Attempts to find the all users in group.
*
* @param long _groupID
*
* @return Collection
* @exception javax.ejb.FinderException
* if there is an error finding the bean
* @exception java.rmi.RemoteException
* if there is a communications or systems failure
*/
public Collection ejbFindByGroupUserID(long _groupID)throws FinderException, RemoteException
{
log("ejbFindByGroupUserID(" + _groupID + ") called of GroupUserBean");
Connection con = null;
PreparedStatement ps = null;
Vector user = new Vector();
try {
con = getConnection();
ps = con.prepareStatement("SELECT USER_ID FROM TR_GROUP_USER WHERE GROUP_ID =?");
ps.setLong(1,_groupID);
ps.executeQuery();
ResultSet rs = ps.getResultSet();
while(rs.next())
{
log("User Id is"+ rs.getLong(1));
user.addElement( new GroupUserPK(_groupID,rs.getLong(1)));
}
} catch (SQLException sqe)
{
log("SQLException: " + sqe);
throw new EJBException (sqe);
}
finally
{
cleanup(con, ps);
}
log("ejbFindByGroupUserID found");
return user;
}
/**
* For printing the output on the console.
*/
private void log(String s)
{
if (VERBOSE) System.out.println(s);
}
/**
* Gets current connection to the connection pool.
*
* @return Connection
* @exception javax.ejb.EJBException
* if there is a communications or systems failure
* @exception java.sql.SQLException
* if there is a SQL query Exception
*/
private Connection getConnection()
throws SQLException
{
InitialContext initCtx = null;
try {
initCtx = new InitialContext();
DataSource ds= (javax.sql.DataSource)initCtx.lookup("java:/jdbc/TimeReportDB");
return ds.getConnection();
}
catch(NamingException ne)
{
log("Failed to lookup JDBC Datasource. Please double check that");
throw new EJBException(ne);
}
finally
{
try {
if(initCtx != null) initCtx.close();
}
catch(NamingException ne)
{
log("Error closing context: " + ne);
throw new EJBException(ne);
}
}
}
/**
* For cleaning up the Connection.
*/
private void cleanup(Connection con, PreparedStatement ps)
{
try {
if (ps != null) ps.close();
} catch (Exception e)
{
log("Error closing PreparedStatement: "+e);
throw new EJBException (e);
}
try {
if (con != null) con.close();
} catch (Exception e)
{
log("Error closing Connection: " + e);
throw new EJBException (e);
}
}
/**
* This Function helps to look the jndi-path from the config file
*
* @param String invokingFile
* @param String key
*
* @return String
*
*/
private String getJNDIname()
{
try
{
Context ctxJndi = new InitialContext();
Context envoirnment = (Context)ctxJndi.lookup("java:/comp/env");
return (String)envoirnment.lookup("JNDI PATH");
}
catch(Exception e)
{
log(e.getMessage() + "Resource is missing");
return null;
}
}
}