将db字段映射到特殊类型的POJO

时间:2017-02-06 10:36:38

标签: java sql jpa ebean

我有一个带有FoodItems的数据库,每个数据库都有以克为单位的蛋白质,以千卡为单位的能量等数据。这些数据仅存储为浮点数。

我想将这些映射到一个名为FoodData的特殊POJO对象,该对象只有浮点数,然后是一个表示单位的枚举,如GRAMS,KCALS,MILLIGRAMS等。

我该怎么做?

这就是我现在所拥有的:

FoodItem.java

@Column(name = "energy_kcal") public Float energyKcal;
@Column(name = "energy_kj") public Float energyKj;
@Column(name = "carbohydrates_g") public Float carbohydrates;
@Column(name = "protein_g") public Float protein;
@Column(name = "fibre_g") public Float fibre;
@Column(name = "whole_grain_g") public Float wholeGrain;
@Column(name = "cholesterol_mg") public Float cholesterol;
@Column(name = "water_g") public Float water;
@Column(name = "alcohol_g") public Float alcohol;
@Column(name = "ash_g") public Float ash;
@Column(name = "waste_procent") public Float waste;

FoodData.java

public class FoodData {

    private float data;
    private Unit unit;

    public Enum Unit {
         GRAMS, KCAL, MILLIGRAMS
    }
}

我想做的是这样的事情:

@Column(name = "energy_kcal") public FoodData energyKcal = new FoodData(data_from_column, Unit.KCAL);

但那不是你怎么做的,你是怎么做到的?

0 个答案:

没有答案