同じ構造をもつ別々の2つのテーブルをプログラム上区別なく扱いたい

同じ構造を持つ確定版のデータとシミュレーション用のデータを別々のテーブルに持つってよくあること?たとえば販売予測の確定データとシミュレーションデータとか。そういう場合、ビジネスロジックの上では違いを意識したくなかったりする。DaoとエンティティそれぞれにインタフェースをかぶせればOKだと思う。

CREATE TABLE FORECASTFINAL
(PRODUCT VARCHAR(20) NOT NULL PRIMARY KEY,
 QTY NUMERIC(7));
CREATE TABLE FORECASTSIMULATION
(PRODUCT VARCHAR(20) NOT NULL PRIMARY KEY,
 QTY NUMERIC(7));
INSERT INTO FORECASTFINAL VALUES('foo',20);
INSERT INTO FORECASTFINAL VALUES('hoge',10);
INSERT INTO FORECASTSIMULATION VALUES('bar',30);
public interface Forecast {

    public String getProduct();
    
    public void setProduct(String product);
    
    public int getQty();
    
    public void setQty(int qty);

}
public class ForecastFinal implements Forecast {
    
    private String product;
    private int qty;

    public ForecastFinal() {
    }
    
    public String getProduct() {
        return product;
    }
    public void setProduct(String product) {
        this.product = product;
    }
    public int getQty() {
        return qty;
    }
    public void setQty(int qty) {
        this.qty = qty;
    }     
    
    public String toString() {
        return "final: " 
                + "product=" + product + ", "
                + "qty=" + qty;
    }
}
public class ForecastSimulation implements Forecast {

    private String product;
    private int qty;
    
    public ForecastSimulation() {
    }
    
    public String getProduct() {
        return product;
    }
    public void setProduct(String product) {
        this.product = product;
    }
    public int getQty() {
        return qty;
    }
    public void setQty(int qty) {
        this.qty = qty;
    }
    
    public String toString() {
        return "simulation: " 
                + "product=" + product + ", "
                + "qty=" + qty;
    }
}
public interface ForecastDao {
    
    public List getAllForecasts();
        
    public int update(Forecast forecast);
}
public interface ForecastFinalDao extends ForecastDao {
    public Class BEAN = ForecastFinal.class;
}
public interface ForecastSimulationDao extends ForecastDao {
    public Class BEAN = ForecastSimulation.class;
}
public interface ForecastDaoFactory {
    public ForecastDao getForecastDao(String name);
}
public class ForecastDaoFactoryImpl implements ForecastDaoFactory {
    
    private Map daos = new HashMap();
    
    public ForecastDao getForecastDao(String name) {
        return (ForecastDao)daos.get(name);
    }
    
    public void put(String name, ForecastDao dao) {
        daos.put(name, dao);
    }
}
<?xml version="1.0" encoding="Shift_JIS"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container//EN"
"http://www.seasar.org/dtd/components.dtd">

  
  
    dao.interceptor
  
  
    dao.interceptor
  
  
    
      "final"
      final
    
    
      "simulation"
      simulation
    
  

public class ForecastDaoClient {

  private static final String PATH = "study/ForecastDao.dicon";

  public static void main(String[] args) {
    S2Container container = S2ContainerFactory.create(PATH);
    container.init();
    try {
      ForecastDaoFactory factory = (ForecastDaoFactory) container
          .getComponent(ForecastDaoFactory.class);
            
            System.out.println(" ** final **");
      ForecastDao dao1 = factory.getForecastDao("final");
            List finals = dao1.getAllForecasts();
            for(Iterator it = finals.iterator(); it.hasNext();) {
                Forecast each = (Forecast)it.next();
                System.out.println(each);
            }
      
            System.out.println(" ** simulation **");
            ForecastDao dao2 = factory.getForecastDao("simulation");            
            List simulations = dao2.getAllForecasts();
            for(Iterator it = simulations.iterator(); it.hasNext();) {
                Forecast each = (Forecast)it.next();
                System.out.println(each);
            }
            
        } finally {
      container.destroy();
    }

  }
}

実行結果

 ** final **
final: product=foo, qty=20
final: product=hoge, qty=10
 ** simulation **
simulation: product=bar, qty=30