AClass.java:
import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;
@DatabaseTable(tableName = "classes")
public class AClass {
@DatabaseField(generatedId = true)
public int id;
@DatabaseField(canBeNull = false, dataType = DataType.INTEGER)
public int classId;
@DatabaseField(canBeNull = false, defaultValue = "class", dataType = DataType.STRING)
public String name;
@ForeignCollectionField(eager = false)
public ForeignCollection<Student> students;
@Override
public String toString() {
return "id:" + id + ",classId:" + classId + ",name:" + name;
}
}
Student.java:
import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
@DatabaseTable(tableName = "students")
public class Student {
@DatabaseField(generatedId = true)
public int id;
@DatabaseField(canBeNull = false, dataType = DataType.INTEGER)
public int studentId;
@DatabaseField(canBeNull = false, dataType = DataType.STRING)
public String name;
@DatabaseField(canBeNull = false, foreign = true, foreignAutoRefresh = true)
public AClass aclass;
@Override
public String toString() {
return "id:" + id + ",studentId:" + studentId + ",name:" + name + ",className:" + aclass.name;
}
}
ORMLiteDatabaseHelper.java:
import java.sql.SQLException;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
public class ORMLiteDatabaseHelper extends OrmLiteSqliteOpenHelper {
private static ORMLiteDatabaseHelper mDatabaseHelper = null;
private Dao<AClass, Integer> mClassDao = null;
private Dao<Student, Integer> mStudentDao = null;
private final static String DB_NAME = "school.db";
private final static int DB_VERSION = 1;
public ORMLiteDatabaseHelper(Context context, String databaseName, CursorFactory factory, int databaseVersion) {
super(context, DB_NAME, factory, DB_VERSION);
}
public static ORMLiteDatabaseHelper getInstance(Context context) {
if (mDatabaseHelper == null) {
mDatabaseHelper = new ORMLiteDatabaseHelper(context, DB_NAME, null, DB_VERSION);
}
return mDatabaseHelper;
}
@Override
public void onCreate(SQLiteDatabase arg0, ConnectionSource connectionSource) {
Log.d(this.getClass().getName(), "ORMLite数据库:onCreate");
try {
TableUtils.createTableIfNotExists(connectionSource, AClass.class);
TableUtils.createTableIfNotExists(connectionSource, Student.class);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
Log.i(this.getClass().getName(), "数据库 -> onUpgrade");
try {
// 删除旧的数据库表。
TableUtils.dropTable(connectionSource, AClass.class, true);
TableUtils.dropTable(connectionSource, Student.class, true);
// 重新创建新版的数据库。
onCreate(database, connectionSource);
} catch (SQLException e) {
e.printStackTrace();
}
}
public Dao<Student, Integer> getStudentDao() {
if (mStudentDao == null) {
try {
mStudentDao = getDao(Student.class);
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
return mStudentDao;
}
public Dao<AClass, Integer> getClassDao() {
if (mClassDao == null) {
try {
mClassDao = getDao(AClass.class);
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
return mClassDao;
}
@Override
public void close() {
super.close();
mClassDao = null;
mStudentDao = null;
}
}
MainActivity.java:
import java.sql.SQLException;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.ForeignCollection;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private Dao<AClass, Integer> mClassDao;
private Dao<Student, Integer> mStudentDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ORMLiteDatabaseHelper mORMLiteDatabaseHelper = ORMLiteDatabaseHelper.getInstance(getApplicationContext());
mClassDao = mORMLiteDatabaseHelper.getClassDao();
mStudentDao = mORMLiteDatabaseHelper.getStudentDao();
// 在数据库中存储5个班级。
for (int i = 1; i < 6; i++) {
AClass aclass = new AClass();
aclass.classId = i;
aclass.name = i + "班";
try {
mClassDao.createIfNotExists(aclass);
} catch (SQLException e) {
e.printStackTrace();
}
}
// 找到id=1的1班。
AClass class1 = null;
try {
class1 = mClassDao.queryForId(1);
} catch (SQLException e) {
e.printStackTrace();
}
// 创建10个学生,这10个学生都归属到1班。
for (int i = 0; i < 10; i++) {
Student s = new Student();
s.studentId = i;
s.name = "学生" + i;
// 将新创建的这些学生所在班级指针指向1班。
// 1班有这19个学生,换言之,这19个学生是1班的学生。
s.aclass = class1;
try {
mStudentDao.createIfNotExists(s);
} catch (SQLException e) {
e.printStackTrace();
}
}
ForeignCollection<Student> students = class1.students;
for (Student s : students) {
Log.d("数据库", s.toString());
}
}
}