Homes

BeanHome (com.javelin.beans.BeanHome)

It is the responsibility of the BeanHome interface to find, create, update and remove individual beans from their persistent store; whilst it is the responsibility of the session to coordinate that persistence behavior.

The BeanHome should (generally) not be used directly by the application programmer.

The BeanHome has a few of methods to allow the Session to be aware of its context,

The key represents the current key (User) on the system, this set on the Home before the home is got from the ClassContext. This key could be a Thread or User name and can be used by the Home to retrieve objects bound in its own context.

public Object getKey();
public void setKey( Object key );

The ClassContext represents the current ClassContext for the Home.

Public ClassContext getClassContext();
public void setClassContext( ClassContext classContext );

The BeanHome has a set of 'abstract' persistence methods that allow the BeanSession to coordinate persistence of dependent Beans. It is the responsibility of implementation of the BeanHome class to implement these methods.

These methods are as follows: -

public Bean findBeanByPrimaryKey( Object primaryKey ) throws FinderException, ValidationException;
public void load( Bean bean ) throws LoadException, ValidationException;
public Bean create( Bean bean ) throws CreateException, ValidationException;
public void store( Bean bean ) throws StoreException, ValidationException;
public void remove( Bean bean ) throws RemoveException, ValidationException;

AbstractBeanHome (com.javelin.beans.AbstractBeanHome)

The AbstractBeanHome is a helper class that specific BeanHomes extend. The AbstractBeanHome has the following functionality: -

  • The BeanHome key and context properties.

HomeInterfaceWriter (com.javeln.generator.beans.HomeInterfaceWriter)

The HomeInterfaceWriter is responsible for writing out the BeanHome interfaces for every table.

Instantiating Bean Homes

BeanHomes are instantiated by the ClassContext. This is done to hide the implementation of the BeanHomes.

BeanHomes are instantiated using the following syntax: -

<ClassName>Home home = <ContextName>Context.getDefault().new<ClassName>Home();

<ClassName>Home home = <ContextName>Context.getDefault().get<ClassName>Home();

<ClassName>Home home = <ContextName>Context.getDefault().get<ClassName>Home(Object key);

To get a new AccountHome from the NorthwindContext, all you have to do is call : -

AccountHome accountHome = NorthwindContext.getDefault().newAccountHome();

Finding Beans

The BeanSession is required to find beans using the BeanHome interface. This is because of caching constraints.

All the findBy methods on the BeanSession are replicated on the BeanHome.

Counting Beans

Counting Beans, using the countBy methods is not a requirement on homes. It is is the responsibility of the Session to count beans and there is no requirement to use a BeanHome instance to do this.

Loading Beans

The BeanHome is responsible for loading beans. Every BeanHome will have the following syntax: -

public void load( <ClassName> <className ) throws LoadException, ValidationException;

The BeanHome will only load the bean that is has been requested to. It is the responsibility of the BeanSession to coordinate calling the load methods on all the BeansHomes for all the dependent beans when a Bean is loaded on a BeanSession.

Creating Beans

The BeanHome is responsible for creating beans. Every BeanHome will have the following syntax: -

public void create( <ClassName> <className ) throws CreateException, ValidationException;

The BeanHome will only create the bean that is has been requested to. It is the responsibility of the BeanSession to coordinate calling the create and store methods on all the BeansHomes for all the dependent beans when a Bean is saved on a BeanSession.

Storing Beans

The BeanHome is responsible for storing beans. Every BeanHome will have the following syntax: -

public void store( <ClassName> <className ) throws StoreException, ValidationException;

The BeanHome will only store the bean that is has been requested to. It is the responsibility of the BeanSession to coordinate calling the create and store methods on all the BeansHomes for all the dependent beans when a Bean is saved on a BeanSession.

Removing Beans

The BeanHome is responsible for removing beans. Every BeanHome will have the following syntax: -

public void store( <ClassName> <className ) throws StoreException, ValidationException;

The BeanHome will only remove the bean that is has been request to. It is the responsibility of the BeanSession to coordinate calling the remove methods on all the BeansHomes for all the dependent beans when a Bean is remove on a BeanSession.

Example Home

Here is an example of a (simple) Home Interfaces for Addresses to give a big picture of what is generated.

/*
* Copyright Javelinsoft Ltd, All rights reserved.
*/

package com.javelin.jcommerce;

import java.util.*;
import java.sql.*;
import com.javelin.jcommerce.*;
import com.javelin.jcommerce.*;
import com.javelin.beans.*;

/**
* AddressHome.java
*
* @version JCommerce - 1.0
* @author Javelin Software
* @see com.javelin.jcommerce.Address
* @see com.javelin.jcommerce.AddressSession
* @see com.javelin.jcommerce.AddressValidator
*/

public interface AddressHome extends BeanHome
{

/**
* Find All the 'available' beans.
* @return Iterator
* @exception FinderException If there was a problem finding any of the beans.
* @exception ValidationException If there was a problem validating any of the beans.
*/
public Iterator findByAll() throws FinderException, ValidationException;

/**
* Find the Address By Location
* @param locationKey
* @return Address
* @exception FinderException If there was a problem finding any of the beans.
* @exception ValidationException If there was a problem validating any of the beans.
*/
public Address findByLocation( Object locationKey ) throws FinderException, ValidationException;

/**
* Find the Address By PrimaryKey
* @param addressKey
* @return Address
* @exception FinderException If there was a problem finding any of the beans.
* @exception ValidationException If there was a problem validating any of the beans.
*/
public Address findByPrimaryKey( Object addressKey ) throws FinderException, ValidationException;

/**
* Find the Beans using a String.
* @param query A Query String, usually a SQL query.
* @return Iterator
* @exception FinderException If there was a problem finding any of the beans.
* @exception ValidationException If there was a problem validating any of the beans.
* @expert
*/
public Iterator findByQuery( String query ) throws FinderException, ValidationException;

/**
* Find the Beans using a Query String, with a maximum return row count.
* @param query A Query String
* @param maxRows The maximum number of rows to return.
* @return Iterator
* @exception FinderException If there was a problem finding any of the beans.
* @exception ValidationException If there was a problem validating any of the beans.
* @expert
*/
public Iterator findByQuery( String query, int maxRows ) throws FinderException, ValidationException;

/**
* Count the Beans using a String.
* @param query A Query String
* @return int the number of matching beans
* @exception FinderException If there was a problem finding any of the beans.
* @exception ValidationException If there was a problem validating any of the beans.
* @expert
*/
public int countByQuery( String query ) throws FinderException, ValidationException;

/**
* Reload the Address based on it's primaryKey.
* @param Address the bean to reload.
* @exception LoadException If there was a problem loading the Bean.
* @exception ValidationException If there was a problem validating the Bean.
*/
public void load( Address address ) throws LoadException, ValidationException;

/**
* Create just this Address in the store.
* @param Address the bean to create.
* @exception CreateException If there was a problem creating the Bean.
* @exception ValidationException If there was a problem validating the Bean.
*/
public Address create( Address address ) throws CreateException, ValidationException;

/**
* Store just this Address in the store.
* @param Address the bean to create.
* @exception StoreException If there was a problem storing the Bean.
* @exception ValidationException If there was a problem validating the Bean.
*/
public void store( Address address ) throws StoreException, ValidationException;

/**
* Remove just this Address in the store.
* @param Address the bean to create.
* @exception RemoveException If there was a problem removing the Bean.
* @exception ValidationException If there was a problem validating the Bean.
*/
public void remove( Address addressKey ) throws RemoveException, ValidationException;

}