Eva
Java粉絲募集中
  • 簡介
  • 我們的優勢
  • 最新消息
  • 下載與使用
  • 範例與說明
    • Eva Objects >
      • Bean類別定義
      • Entity型別定義
      • Struct型別定義
      • 建立Entity空白物件
      • 建立Entity修改物件
      • 建立Entity條件物件
      • 建立Struct值物件
    • Eva_ORM >
      • ORM程式主體
      • 使用條件物件執行操作
      • 執行更複雜條件的操作.
      • 分頁取回資料
      • 操作JDBC的參數
      • Array Property的操作
      • Collection Property的操作
      • 更複雜的Collection Property
      • 指定表格名稱
      • 新增資料庫Dialect
    • Eva_IOC >
      • IOC程式主體
      • AOP應用
    • Eva_Struct >
      • Struct範例
      • Bit Strcut範例
      • 指標範例
      • 於Eva_ORM中使用
    • Eva_Sort >
      • 依指定Properties排序
      • 部份排序
      • 優先排序部份
    • Eva_Cache >
      • hashing-based cache
      • 非hashing-based cache
  • API列表
  • 技術支援
    • 協助開發
  • 相關書籍

自訂Property Converter.

若我們定義一個Entity FOO, 與兩個它所使用到的型別X,Y如下 : 
package com.logntw.eva.demo.converter;

public class X
{
  public final int i;
  public X(int i)
  {
    this.i = i;
  }
}
package com.logntw.eva.demo.converter;

public class Y
{
  public final X a;
  public Y(X a)
  {
    this.a = a;
  }
}
package com.logntw.eva.demo.converter;

import com.logntw.eva.anno.orm.Entity;

@Entity
public interface FOO
{
  X getA();
  void setA(X a);
  
  Y getB();
  void setB(Y b);  
}
則這個Entity不能直接被對映到一個資料表, 因為其中X,Y型別的Property不能被儲存在資料庫中, 所以我們會需要再為它們建立下列兩個Converters,  XConverter與YConverter, 透過它們, 將X與Y型別的物件最級轉換為Integer型別的物件, 如此就可能能將它們儲存在資料庫之中. (若該資料庫擁有能對映Integer型別的資料型別的話)
package com.logntw.eva.demo.converter;

import com.logntw.eva.odm.Condition;
import com.logntw.eva.odm.weighted.WeightedConverter;

public class XConverter extends WeightedConverter
{
  @Override
  public Integer to(X data)
  {    
    return data.i;
  }

  @Override
  public Condition getInCondition()
  {    
    return getCondition(X.class);
  }

  @Override
  public Condition getOutCondition()
  {
    return getCondition(Integer.class);
  }

  @Override
  public X from(Integer object)
  {    
    return new X(object);
  }
}
package com.logntw.eva.demo.converter;

import com.logntw.eva.odm.Condition;
import com.logntw.eva.odm.weighted.ConverterAtoB;
import com.logntw.eva.odm.weighted.WeightedConverter;
import com.logntw.eva.odm.weighted.ConverterAtoB.Access;

@ConverterAtoB(Access.BA_PRIVATE)
public class YConverter extends WeightedConverter<Y, X>
{
  @Override
  public X to(Y data)
  {
    return data.a;
  }

  @Override
  public Condition<Y> getInCondition()
  {
    return getCondition(Y.class);
  }

  @Override
  public Condition<X> getOutCondition()
  {
    return getCondition(X.class);
  }

  @Override
  public Y from(X object)
  {
    return new Y(object);
  }
}
Powered by Create your own unique website with customizable templates.