org.mentabean
Class BeanConfig

java.lang.Object
  extended by org.mentabean.BeanConfig

public class BeanConfig
extends Object

A class representing a bean configuration, like table name, primary keys and fields in the database.

Author:
sergio.oliveira.jr@gmail.com

Constructor Summary
BeanConfig(Class<? extends Object> beanClass, String tableName)
          Creates a configuration for a bean represented by the given class.
 
Method Summary
 BeanConfig defaultToNow(String name)
          Make this bean property default to 'now' when inserting in the database.
 BeanConfig field(String name, DBType type)
          Add a database field for the given property with the given database type.
 BeanConfig field(String name, String dbName, DBType type)
          Add a database field for the given property with the given database type.
 Iterator<DBField> fields()
          Return all DBFields configured for this bean.
 DBField getAutoIncrementField()
          Return an auto-increment field, if one was configured for this bean.
 Class<? extends Object> getBeanClass()
          Return the bean class.
 int getNumberOfFields()
          Return the number of fields configured for this bean.
 DBField getSequenceField()
          Return a sequence field, if one was configured for this bean.
 String getTableName()
          Return the table name where the bean properties are stored.
 boolean hasPK()
          Check whether the primary key was defined.
 BeanConfig pk(String name, DBType type)
          Add a bean property that is the primary key in the database.
 BeanConfig pk(String name, String dbName, DBType type)
          Add a property that is the primary key in the database.
 Iterator<DBField> pks()
          Return an iterator with the DBFields for the PK configured for this bean.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BeanConfig

public BeanConfig(Class<? extends Object> beanClass,
                  String tableName)
Creates a configuration for a bean represented by the given class.

Parameters:
beanClass - The bean klass
tableName - The database table where the bean properties will be stored.
Method Detail

getTableName

public String getTableName()
Return the table name where the bean properties are stored.

Returns:
The database table name.

getBeanClass

public Class<? extends Object> getBeanClass()
Return the bean class.

Returns:
The bean class.

getAutoIncrementField

public DBField getAutoIncrementField()
Return an auto-increment field, if one was configured for this bean. Note: A bean can have only one auto-increment field configured for this bean. Attempting to configure more than one will throw an exception.

Returns:
the auto-increment field configured for this bean or null if it was not defined

getSequenceField

public DBField getSequenceField()
Return a sequence field, if one was configured for this bean. Note: A bean can have only one sequence field configured for this bean. Attempting to configure more than one will throw an exception.

Returns:
the sequence field configured for this bean or null if it was not defined

defaultToNow

public BeanConfig defaultToNow(String name)
Make this bean property default to 'now' when inserting in the database. Note: The bean property must have been previously configured for you to call this method, in other words, a field must have been previously configured with that bean property name.

Parameters:
name - The bean property name that should default to now.
Returns:
This BeanConfig (Fluent API)

field

public BeanConfig field(String name,
                        DBType type)
Add a database field for the given property with the given database type. It assumes that the property name is the SAME as the database column name. If they are different, use the other addField method.

Parameters:
name - The bean property name (same as the database column name)
type - The database type
Returns:
This BeanConfig (Fluent API)

field

public BeanConfig field(String name,
                        String dbName,
                        DBType type)
Add a database field for the given property with the given database type.

Parameters:
name - The bean property name
dbName - The name of the database column holding this property
type - The database type
Returns:
This BeanConfig (Fluent API)

pk

public BeanConfig pk(String name,
                     DBType type)
Add a bean property that is the primary key in the database. The column name is the same as the property bean name. If they are different use the other pk method. All beans must have a primary key and you can call this method multiple times to support composite primary keys.

Parameters:
name - The bean property name
type - The database type
Returns:
This BeanConfig (Fluent API)

pk

public BeanConfig pk(String name,
                     String dbName,
                     DBType type)
Add a property that is the primary key in the database. All beans must have a primary key and you can call this method multiple times to support composite primary keys.

Parameters:
name - The bean property name
dbName - The name of the database column holding this property
type - The database type
Returns:
This BeanConfig (Fluent API)

getNumberOfFields

public int getNumberOfFields()
Return the number of fields configured for this bean. It includes the PK.

Returns:
The number of fields configured for this bean.

toString

public String toString()
Overrides:
toString in class Object

fields

public Iterator<DBField> fields()
Return all DBFields configured for this bean. It includes the PK as well.

Returns:
An Iterator with all DBFields configured for this bean.

hasPK

public boolean hasPK()
Check whether the primary key was defined.

Returns:
true if a primary key was defined.

pks

public Iterator<DBField> pks()
Return an iterator with the DBFields for the PK configured for this bean. Note: A bean can have more than one property as its primary key, in that case it has a composite primary key.

Returns:
An iterator with the DBFields for the PK.


Copyright © 2011. All Rights Reserved.