An EJB home interface contains methods that return proxies for the component’s remote interface.
The home interface for an entity bean contains finder methods that can be used to obtain instances that represent rows already in the underlying database.
To instantiate a home interface, use a SessionManager::Manager instance to create a SessionManager::Session instance, then call the SessionManager::Session::lookup method, passing the bean’s home interface name. Narrow the returned object to the bean’s home interface.
The example below instantiates the home interface named bookStore/customerMaintenance. In IDL, the home interface is bookStore::custMaintenanceHome:
' Initialize the ORB Dim orbRef As JaguarTypeLibrary.ORB Set orbRef = New JaguarTypeLibrary.ORB orbRef.Init ("") ' Get a SessionManager::Manager proxy Dim manager_ior As String Dim CORBAObj As Object Dim sessManager As SessionManager.Manager manager_ior = Format("iiop://" & host & ":" & port) Set CORBAObj = _ orbRef.string_to_object(manager_ior) Set sessManager = CORBAObj.Narrow_("SessionManager/Manager") ' Get a Session proxy, passing username and password Dim session As SessionManager.session Set CORBAObj = sessManager.createSession( _ userName, password) Set session = CORBAObj.Narrow_("SessionManager/Session") ' Get a proxy for the home interface Dim home As bookStore.custMaintenanceHome Set CORBAObj = session.lookup("bookStore/custMaintenance") Set home = CORBAObj.Narrow_("bookStore/custMaintenanceHome")
Each instance of an entity bean represents a row in an underlying database table. An entity bean’s home interface may contain both finder methods and create methods.
Finder methods Finder methods look return instances that match an existing row in the underlying database.
A home interface may contain several finder methods, each of which accepts parameters to constrain the search for matching database rows. Every entity bean home interface has a findByPrimaryKey method that accepts a structure that represents the primary key for a row to look up.
Finder methods throw CtsComponents::FinderException if no rows match the specified search criteria.
Create methods Create methods insert a row into the underlying database.
When instantiating an entity bean proxy, call a finder method first if you are not sure whether an entity bean’s data is already in the database. Create methods throw a CtsComponents::CreateException exception if you attempt to insert a duplicate database row.
Example: instantiating an entity bean This example instantiates an entity bean that represents a customer credit account. The primary key structure has two members: custName is a string and creditType is also a string. The example looks for a customer named "Morry" using the findByPrimaryKey method. If a user exception is raised, the code assumes that CtsComponents::FinderException was thrown to indicate that the requested entity does not exist. In this case, the example calls a create method to create a new entity.
Dim pKey As bookStore.custCreditKey Dim customerName as String customerName = "Morry" Set pKey = New bookStore.custCreditKey pKey.creditType = "VISA" pKey.custName = customerName Dim balance As Long ' First try to look up the customer as an existing entity ' This fails with CtsComponents::FinderException if the ' entity does not exist. On Error GoTo FinderError Set customer = home.findByPrimaryKey(pKey) GoTo Instantiated FinderError: ' An error 9000 means a user-defined exception was thrown. ' In this case, it must be CtsComponents::FinderException, ' which indicates the requested entity does not exist. Any ’ other error number is unexpected. If Err.Number <> 9000 Then ' This is an unexpected error inError = True Call MsgBox("Error calling findByPrimaryKey", "Error") GoTo CleanupAfterFailure End If ' Create a new entity. Create methods are not overloaded in the ’ IDL home interface, and we must use the full IDL method name. On Error GoTo CleanupAfterFailure balance = 3000 Set customer = home.create__string(customerName, balance) Instantiated: ' Successful instantiation. Code to call business methods goes here. CleanupAfterFailure: ' Unexpected error. Code to clean up forms, display errors, ’ and so forth.
The home interface for a session bean contains only create methods.
The example below instantiates a home interface named HelloWorldHome/HelloWorldHome, then calls the create method that takes no parameters. The IDL home interface type is mde::helloworld::HelloWorldHome and the remote interface is mde::helloworld::HelloWorld.
Dim session as SessionManager.Session ... deleted code that instantiated a valid session ... Dim compHome As mde_helloworld.HelloWorldHome Set CORBAObj = session.lookup("HelloWorldHome/HelloWorldHome") Set compHome = CORBAObj.Narrow_("mde/helloworld/HelloWorldHome") Set comp = compHome.Create().Narrow_("mde/helloworld/HelloWorld")
Copyright © 2005. Sybase Inc. All rights reserved. |