This is a new generation orm for Android.
- Put the JAR in the libs subfolder of your Android project
public class DataBaseHelper extends OrmHelper {
public DataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
protected void onCreate() {
}
@Override
protected void onUpgrade(int oldVersion, int newVersion) {
}
@Override
public void getDefaultValues(Class<? extends OrmEntity> entityClass, List<OrmEntity> valueList) {
}
}
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
try{
OrmFactory.SetHelper(DataBaseHelper.class, getApplicationContext());
}catch (Exception e){
e.printStackTrace();
}
}
}
public class BaseEntity extends OrmEntity {
@Column(primaryKey = true, inherited = true)
private Long id;
}
@Table(name = "car_type", cashedList = true)
public class CarType extends BaseEntity {
@Column
private String code;
public CarType(String code) {
this.code = code;
}
}
@Table(rightJoinTo = {Truck.class})
public class Car extends BaseEntity {
@Column(name = "car_type")
private CarType type;
@Column
private List<Wheel> wheels;
@Column(name = "engine_power")
private int enginePower;
@Column(name = "doors_count")
private int doorsCount;
}
@Table
public class Wheel extends BaseEntity {
@Column(name = "car_id")
private Car car;
@Column
private String manufacturer;
}
@Table(leftJoinTo = Car.class)
public class Truck extends Car {
@Column(name = "is_tipper")
private boolean isTipper;
}
<manifest>
<application android:name=".MyApplication">
...
<meta-data android:name="UO_DB_NAME" android:value="Cars" />
<meta-data android:name="UO_DB_VERSION" android:value="1" />
</application>
</manifest>
Add code to onCreate method in DataBaseHelper
protected void onCreate() {
try {
OrmUtils.CreateTable(CarType.class);
OrmUtils.CreateTable(Car.class);
OrmUtils.CreateTable(Wheel.class);
OrmUtils.CreateTable(Truck.class);
} catch (Exception e) {
e.printStackTrace();
}
}
Add code to getDefaultValues method in DataBaseHelper
public void getDefaultValues(Class<? extends OrmEntity> entityClass, List<OrmEntity> valueList) {
if (entityClass.equals(CarType.class)) {
valueList.add(new CarType("Passenger"));
valueList.add(new CarType("Truck"));
}
}
Add getAllCarTypes method in CarType
public static List<CarType> getAllCarTypes(){
try {
return OrmEntity.getAllEntities(CarType.class);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Car car = new Car();
car.setType(CarType.getAllCarTypes().get(0));
car.setEnginePower(116);
car.setDoorsCount(4);
Wheel whell = new Whell();
whell.setCar(car);
whell.setManufacturer("Michrelli");
car.addWheel(whell);
car.alter();
car.setEnginePower(120);
car.getWheels().get(0).setManufacturer("Pirlin");
And now just
car.alter();
If you have list of entities, you can insert/update it's items in an one transaction.
OrmTransaction.WorkInTransaction(new OrmTransaction.Worker() {
@Override
public boolean work() {
boolean success = true;
for (Car car : cars){
try {
if (!OrmEntity.alterInTransaction(car))
{
success = false;
break;
}
} catch (Exception e) {
e.printStackTrace();
success = false;
}
}
return success;
}
});
Truck truck = new Truck();
truck.setType(CarType.getAllCarTypes().get(1));
truck.setEnginePower(220);
truck.setDoorsCount(2);
Wheel whell = new Whell();
whell.setCar(truck);
whell.setManufacturer("Michrelli");
truck.addWheel(whell);
truck.setTipper(true);
truck.alter();
public static List<Car> getAllCars() {
try {
return OrmEntity.getAllEntities(CarType.class, true);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static List<Car> getCarsWithoutTrucks() {
try {
return OrmEntity.getAllEntities(CarType.class);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Create in Car Where method
public static OrmWhere Where(){
return Where(Car.class);
}
Will select all cars where an engine power is equals 120
List<Car> cars = Car.Where().Equals("engine_power", 120).Select();
Will select all cars where an engine power is equals 120 and a doors number is equals 4
List<Car> cars = Car.Where().Equals("engine_power", 120).And().Equals("doors_count", 4).Select();
Will select one car who has the Pirlin wheel
Car car = Car.Where().FindChild(Wheel.class, new OrmWhere(Wheel.class).Equals("manufacturer", "Pirlin")).SelectFirst();
Add to Car class a new field maxSpeed and remove the field doorsCount
@Table(rightJoinTo = {Truck.class})
public class Car extends BaseEntity {
@Column(name = "car_type")
private CarType type;
@Column
private List<Wheel> wheels;
@Column(name = "engine_power")
private int enginePower;
@Column(name = "max_speed")
private int maxSpeed;
}
Change the database version in the manifest to 2
<manifest>
<application android:name=".MyApplication">
...
<meta-data android:name="UO_DB_NAME" android:value="Cars" />
<meta-data android:name="UO_DB_VERSION" android:value="2" />
</application>
</manifest>
Add code to onUpdate method in DataBaseHelper. You can write only this:
protected void onUpgrade(int oldVersion, int newVersion) {
try {
if (oldVersion < 2) {
OrmUtils.UpdateTable(Car.class).work();
}
} catch (Exception e) {
e.printStackTrace();
}
}
But! You can help orm do his work! And also, if need to add to the new field the default value:
protected void onUpgrade(int oldVersion, int newVersion) {
try {
if (oldVersion < 2) {
OrmUtils.UpdateTable(Car.class).addColumn("max_speed", 100);
/*Or if not need default value, just
OrmUtils.UpdateTable(Car.class).addColumn("max_speed");*/
OrmUtils.UpdateTable(Car.class).removeColumn("doors_count");
}
} catch (Exception e) {
e.printStackTrace();
}
}