com.avaje.ebean
Class MapBean

java.lang.Object
  extended by com.avaje.ebean.MapBean
All Implemented Interfaces:
EntityBean, java.io.Serializable, java.lang.Cloneable, java.util.Map

public class MapBean
extends java.lang.Object

    
implements EntityBean, java.lang.Cloneable, java.io.Serializable, java.util.Map

    

Map based EntityBean implementation. Used to dynamically create beans based on a table or nativeSql.

Note that MapBeans based on a table can be persisted via save(), insert() and delete(). MapBeans based on a nativeSql query are not persistable until the table to persist to has been set.

There are conveniance methods such as getInteger(), getBigDecimal() etc. The reason for these methods is that the values put into this map often come straight from the JDBC resultSet. Depending on the JDBC driver it may put a different type into a given property. For example an Integer, BigDecimal, Double could all be put into a property depending on the JDBC driver used. These conveniance methods automatically convert the value as required returning the type you expect.


       
        // A "Relational mode" query...
        
        // Get row from oe_order table where primary key = 10
        
        FindByUid find = new FindByUid();
        find.setTableName("oe_order");
        find.setUid("10");
       
        MapBean mapBean = (MapBean)Ebean.find(find);
       
        // get some field data from order 10
        Integer orderId = mapBean.getInteger("id");
        Integer custId  = mapBean.getInteger("cust_id");
        ...
        
        // update the order status and save
        mapBean.setString("status_code","SHIPPED");
        
        Ebean.save(mapBean);
        
 

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Constructor Summary
MapBean()
          Create a new MapBean based on a LinkedHashMap with default initialCapacity (of 16).
MapBean(int initialCapacity, float loadFactor)
          Create with an initialCapacity and loadFactor.
MapBean(java.util.Map map)
          Create with a specific Map implementation.
 
Method Summary
 void clear()
          Clear the map.
 java.lang.Object clone()
          Return a clone of this MapBean.
 boolean containsKey(java.lang.Object key)
          Returns true if the map contains the property.
 boolean containsValue(java.lang.Object value)
          Returns true if the map contains the value.
 java.util.Set entrySet()
          Returns the entrySet of the map.
 boolean equals(java.lang.Object o)
          Equal is based on the id property.
 java.lang.Object get(java.lang.Object name)
          Return a property value by its name.
 java.math.BigDecimal getBigDecimal(java.lang.Object name)
          Return a property value as a BigDecimal.
 java.sql.Date getDate(java.lang.Object name)
          Return the property as a sql date.
 java.lang.Double getDouble(java.lang.Object name)
          Return the property value as a Double.
 EntityBeanIntercept getEbeanIntercept()
          Return the EntityBeanIntercept that controls the method interception and old values creation.
 java.lang.Object getId()
          Return the Id value based on the ID property name.
 java.lang.String getIdPropertyName()
          Return the ID property name.
 java.lang.Integer getInteger(java.lang.Object name)
          Return a property as an Integer.
 java.lang.Long getLong(java.lang.Object name)
          Return a property value as a Long.
 java.lang.String getString(java.lang.Object name)
          Return a property as a String.
 java.lang.String getTableName()
          Return the table name for this bean.
 java.sql.Timestamp getTimestamp(java.lang.Object name)
         &nF="../../../com/avaje/ebean/MapBean.html#put(java.lang.Object, java.lang.Object)">put(java.lang.Object name, java.lang.Object value)
          Set a value to a property.
 void putAll(java.util.Map t)
          Put all the values from t into this map.
 java.lang.Object remove(java.lang.Object name)
          Remove a property from the map.
 void resetOldValues()
          Resets the oldValues back to null and ebeanIntercept 'loaded' to false.
 java.lang.Object set(java.lang.Object name, java.lang.Object value)
          Exactly the same as the put method.
 void setEbeanIntercept(EntityBeanIntercept ebi)
          Not allowed for MapBeans.
 void setIdPropertyName(java.lang.String idPropertyName)
          Set the ID property name.
 void setTableName(java.lang.String tableName)
          Set the table name.
 void setTypeConverter(com.avaje.ebean.util.TypeConverter typeConverter)
          Set the utility object use to provide type conversion.
 int size()
          Return the size of the map.
 java.lang.String toString()
          String description of the underlying map.
 java.util.Collection values()
          Return the values from this map.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MapBean

public MapBean(java.util.Map map)
Create with a specific Map implementation.

The default Map implementation is LinkedHashMap.


MapBean

public MapBean()
Create a new MapBean based on a LinkedHashMap with default initialCapacity (of 16).


MapBean

public MapBean(int initialCapacity,
               float loadFactor)
Create with an initialCapacity and loadFactor.

The defaults of these are 16 and 0.75.

Note that the Map will rehash the contents when the number of keys in this map reaches its threshold (initialCapacity * loadFactor).

Method Detail

setTypeConverter

public void setTypeConverter(com.avaje.ebean.util.TypeConverter typeConverter)
Set the utility object use to provide type conversion.

This is used for getInteger, getBigDecimal etc.


setEbeanIntercept

public void setEbeanIntercept(EntityBeanIntercept ebi)
Not allowed for MapBeans.

Specified by:
setEbeanIntercept in interface EntityBean

getEbeanIntercept

public EntityBeanIntercept getEbeanIntercept()
Return the EntityBeanIntercept that controls the method interception and old values creation.

Specified by:
getEbeanIntercept in interface EntityBean

resetOldValues

public void resetOldValues()
Resets the oldValues back to null and ebeanIntercept 'loaded' to false.

Use this to force an insert or if you want to set additional data to the bean without creating the old values.

Note that you will need to call ebeanSetLoaded(true) after this for old values to be created.


getTableName

public java.lang.String getTableName()
Return the table name for this bean. Used to automatically persist the bean back to a table using jdbc meta data on the table.


setTableName

public void setTableName(java.lang.String tableName)
Set the table name. You need to do this if you want to use this bean to insert into a table. Fetched MapBeans will already have set this.


setIdPropertyName

public void setIdPropertyName(java.lang.String idPropertyName)
Set the ID property name.

This property name is used by equals to try to do equality based on the value of the unique id.

This is not set for tables with concatinated primary keys.


getIdPropertyName

public java.lang.String getIdPropertyName()
Return the ID property name.


getId

public java.lang.Object getId()
Return the Id value based on the ID property name.


equals

public boolean equals(java.lang.Object o)
Equal is based on the id property.

If the id is null then this uses an object and equality becomes == (same instance).

Specified by:
equals in interface java.util.Map
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Return a hashCode for the

Specified by:
hashCode in interface java.util.Map
Overrides:
hashCode in class java.lang.Object

clone

public java.lang.Object clone()
Return a clone of this MapBean.

Overrides:
clone in class java.lang.Object

keys

public java.util.Iterator keys()
Return the property names (String).

Internally this uses LinkedHashMap and so the order of the property names should be predicatable and ordered by the use of LinkedHashMap.


remove

public java.lang.Object remove(java.lang.Object name)
Remove a property from the map. Returns the value of the removed property.

Specified by:
remove in interface java.util.Map

get

public java.lang.Object get(java.lang.Object name)
Return a property value by its name.

Specified by:
get in interface java.util.Map

put

public java.lang.Object put(java.lang.Object name,
                            java.lang.Object value)
Set a value to a property.

Specified by:
put in interface java.util.Map

set

public java.lang.Object set(java.lang.Object name,
                            java.lang.Object value)
Exactly the same as the put method.

I added this method because it seems more bean like to have get and set methods.


getInteger

public java.lang.Integer getInteger(java.lang.Object name)
Return a property as an Integer.


getBigDecimal

public java.math.BigDecimal getBigDecimal(java.lang.Object name)
Return a property value as a BigDecimal.


getLong

public java.lang.Long getLong(java.lang.Object name)
Return a property value as a Long.


getDouble

public java.lang.Double getDouble(java.lang.Object name)
Return the property value as a Double.


getString

public java.lang.String getString(java.lang.Object name)
Return a property as a String.


getUtilDate

public java.util.Date getUtilDate(java.lang.Object name)
Return the property as a java.util.Date.


getDate

public java.sql.Date getDate(java.lang.Object name)
Return the property as a sql date.


getTimestamp

public java.sql.Timestamp getTimestamp(java.lang.Object name)
Return the property as a sql timestamp.


toString

public java.lang.String toString()
String description of the underlying map.

Overrides:
toString in class java.lang.Object

clear

public void clear()
Clear the map.

Specified by:
clear in interface java.util.Map

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if the map contains the property.

Specified by:
containsKey in interface java.util.Map

containsValue

public boolean containsValue(java.lang.Object value)
Returns true if the map contains the value.

Specified by:
containsValue in interface java.util.Map

keySet

public java.util.Set keySet()
Returns the key set of the map.

Specified by:
keySet in interface java.util.Map

putAll

public void putAll(java.util.Map t)
Put all the values from t into this map.

Specified by:
putAll in interface java.util.Map

size

public int size()
Return the size of the map.

Specified by:
size in interface java.util.Map

values

public java.util.Collection values()
Return the values from this map.

Specified by:
values in interface java.util.Map