« - »

Look-up Tables: Entity beans

24 January 2009

The first step in refactoring the Look-up Table service will be to update all of the entities to include the new context property. This will involve changes to the Java object, as well as changes to the Hibernate mapping, Betwixt mapping, and the underlying database table. This will have to be done for all three of the entities involved in the Look-up Table service, the LookupTable, the LookupTableProperty, and the LookupTableEntry.

The most significant change will be to the LookupTable entity. Before the introduction of the context, the table id was unique, which meant that we could use that property for the primary key to the database, which we did. With the addition of the context, we can now potentially have a look-up table with the same id in different contexts, so it is no longer usable as a primary key. To resolve that problem, I added a new, generated primary key as well as the new context property when I updated theĀ  LookupTable:

package org.restafarian.core.beans;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>This entity bean defines a single look-up table.</p>
 */
public class LookupTable extends PersistentBeanBase {
  private static final long serialVersionUID = 1;
  private int id = -1;
  private String context = null;
  private String tableName = null;
  private String displayName = null;
  private String description = null;
  private List<LookupTableProperty> properties = null;

  public void addProperty(LookupTableProperty lookupTableProperty) {
    if (properties == null) {
      properties = new ArrayList<LookupTableProperty>();
    }
    lookupTableProperty.setTableId(id);
    lookupTableProperty.setSequence(properties.size());
    properties.add(lookupTableProperty);
  }

  public LookupTableProperty getProperty(int sequence) {
    LookupTableProperty lookupTableProperty = null;

    if (properties != null && properties.size() > sequence) {
      lookupTableProperty = (LookupTableProperty)
           properties.get(sequence);
    }

    return lookupTableProperty;
  }

  public void setProperty(int sequence, LookupTableProperty
         lookupTableProperty) {
    if (properties != null && properties.size() > sequence) {
      properties.set(sequence, lookupTableProperty);
    }
  }

  /**
   * @return the id
   */
  public int getId() {
    return id;
  }

  /**
   * @param id the id to set
   */
  public void setId(int id) {
    this.id = id;
  }

  /**
   * @return the context
   */
  public String getContext() {
    return context;
  }

  /**
   * @param context the context to set
   */
  public void setContext(String context) {
    this.context = context;
  }

  /**
   * @return the tableName
   */
  public String getTableName() {
    return tableName;
  }

  /**
   * @param tableName the tableName to set
   */
  public void setTableName(String tableName) {
    this.tableName = tableName;
  }

  /**
   * @return the displayName
   */
  public String getDisplayName() {
    return displayName;
  }

  /**
   * @param displayName the displayName to set
   */
  public void setDisplayName(String displayName) {
    this.displayName = displayName;
  }

  /**
   * @return the description
   */
  public String getDescription() {
    return description;
  }

  /**
   * @param description the description to set
   */
  public void setDescription(String description) {
    this.description = description;
  }

  /**
   * @return the properties
   */
  public List<LookupTableProperty> getProperties() {
    return properties;
  }

  /**
   * @param properties the properties to set
   */
  public void setProperties(List<LookupTableProperty> properties) {
    this.properties = properties;
  }
}

This, of course, changed the Hibernate mapping as well, which we could actually get rid of at this point and switch to annotations now that we have made the moved to Java 5, but I was too lazy to work that into the mix at this point, so we’re still stuck with updating the hbm.xml files:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class name="org.restafarian.core.beans.LookupTable"
         table="lookuptable">
    <id name="id" unsaved-value="-1">
      <generator class="native"/>
    </id>
    <property name="context"/>
    <property name="tableName"/>
    <property name="displayName"/>
    <property name="description"/>
    <property name="creationDate"/>
    <property name="createdBy"/>
    <property name="lastUpdate"/>
    <property name="lastUpdateBy"/>
    <list name="properties" cascade="all-delete-orphan" lazy="false">
      <key column="tableId"/>
      <index column="sequence"/>
      <one-to-many
           class="org.restafarian.core.beans.LookupTableProperty"/>
    </list>
  </class>
</hibernate-mapping>

The other item that is impacted by all of this is the Betwixt mapping, which now looks like this:

<?xml version="1.0" encoding="UTF-8" ?>
<info primitiveTypes="element">
  <element name='lookup:table'>
    <attribute name='id' property='id'/>
    <attribute name='context' property='context'/>
    <attribute name='tableName' property='tableName'/>
    <attribute name='xmlns:lookup'
         value='http://www.restafarian.org/lookup'/>
    <attribute name='xmlns:xlink'
         value='http://www.w3.org/1999/xlink'/>
    <element name='displayName' property='displayName'/>
    <element name='description' property = 'description'/>
    <element name='creationDate' property='creationDate'/>
    <element name='createdBy' property='createdBy'/>
    <element name='lastUpdate' property='lastUpdate'/>
    <element name='lastUpdateBy' property='lastUpdateBy'/>
    <element name='properties'>
      <element property='properties' updater='addProperty'/>
    </element>
  </element>
</info>

That pretty much covers the LookupTable, but now we have to make the same kind of changes to the LookupTableProperty and the LookupTableEntry. Those changes don’t involve a key change, though, so they aren’t quite as extensive.


http://blog.restafarian.org/2009/01/look-up-tables-entity-beans/

Leave a reply

You must be logged in to post a comment.