Upgrade your dao to Full Object-Relational Mapping

Relational database is ... relational. The relations between tables (entities) are build on key columns.

Relations in Daobab are represented on interfaces, providing common logic regarding some column.
Those interfaces are separated on two groups:

  • entity interfaces
  • dao interfaces
  • Entities:

    @Entity @Table(name="PIZZA")
    public class Pizza implements RelatedToPizza
    @Entity @Table(name="PIZZA_SIZE")
    public class PizzaSize implements RelatedToPizzaSize
    @Entity @Table(name="PIZZA_PRICE")
    public class PizzaPrice implements RelatedToPizza,RelatedToPizzaSize

    Those interfaces relate to key field:

    @FunctionalInterface
    public interface RelatedToPizza {
    	public Integer getPizzaId();
    }
    @FunctionalInterface
    public interface RelatedToPizzaSize {
    	public Integer getPizzaSizeId();
    }

    Daos:

    Having related Entities, we can implement relation on Daos:

    public interface IDaoPizza implements IDaoRelatedToPizza<Pizza>
    public interface IDaoPizzaSize implements IDaoRelatedToPizzaSize<PizzaSize>
    public interface IDaoPizzaPrice implements IDaoRelatedToPizzaPrice<PizzaPrice>

    And into those interfaces we can put common logic, related to key field:

    public interface IDaoRelatedToPizza<E extends RelatedToPizza> extends IDao<E>{
    	
    	@Column(name="PIZZA_ID")
    	public default DaoColumn<E,Integer> colPizzaId(){
    		return define("pizzaId");
    	};
    		
    	@Column(name="PIZZA_ID")
    	public default DaoRelation<E,Integer,RelatedToPizza> relPizza(){
    		return new DaoRelation<>(colPizzaId(),r->r.getPizzaId());
    	};
    
    }
    public interface IDaoRelatedToPizzaSize<E extends RelatedToPizzaSize> extends IDao<E>{
    	
    	@Column(name="PIZZA_SIZE_ID")
    	public default DaoColumn<E,Integer> colPizzaSizeId(){
    		return define("pizzaSizeId");
    	};
    		
    	@Column(name="PIZZA_SIZE_ID")
    	public default DaoRelation<E,Integer,RelatedToPizzaSize> relPizzaSize(){
    		return new DaoRelation<>(colPizzaSizeId(),r->r.getPizzaSizeId());
    	};
    
    }

    Now, we have a relations definied. Take a look, how it helps:

  • if you need common logic regarding to one field - put it into interface, and inherit it.
  • code duplications avoided.
  • safe code into interface, separated from state.
  • possible injection of all tables related.