I’m experimenting with ORMLite for android.
I have a pre built SQLite db that i am loading on the android device. I have stored position information as LocationX, LocationY columns:
CREATE TABLE graphicsinstances(Name STRING , LocationX FLOAT , LocationY FLOAT , SizeX FLOAT , SizeY FLOAT );
Catch is, at runtime i’d like this to look something like:
public class Vector2
{
public float X;
public float Y;
}
@DatabaseTable(tableName = "graphicsinstances")
public class graphicsinstance
{
public GraphicalEntityInstance()
{
mName = null;
mPosition = Vector2.zero();
mSize = Vector2.zero();
}
@DatabaseField(columnName = "Name", canBeNull = false)
public String mName;
//Olympic standard optimism
//@DatabaseField(columnName = "Location", canBeNull = false)
//public Vector2 mPosition;
// current (unsatisfactory) method:
@DatabaseField(canBeNull = false, useGetSet=true)
private float LocationX;
public float getLocationX() { return mPosition.X;}
public void setLocationX(float x) { mPosition.X = x;}
// repeat for Y, SizeX & SizeY!
}
Have I missed something obvious or am I completely barking up the wrong tree? can anyone suggest an alternative/prettier method?
If I understand, you want to map a table into a class with sub-classes. In your example I guess you want to load the
LocationXandLocationYfields into thempositionwhich I guess is aVector2?I think it is ok to have a
getMPosition()andsetMPosition(mpos)methods that do the mapping between theLocationXandLocationYfields but you are not going to be able to do that mapping auto-magically with ORMLite.From a purity standpoint I always recommend using what database circles call an “entity class”. This class mirrors your database structure exactly. It should have a one-to-one correspondence between fields in the table and the class. It can have methods like
hashCode(),equals(), and the standard getter/setters but very little else.You could create a wrapping object that takes a
GraphicsInstanceobject in its constructor and does theVector2mapping and allows the other operations. When you are ready to persist it when you would need to go back to theGraphicsInstanceobject.