项目中要用到一对多,所以就稍作了解了一下,测试成功的代码,记录一下。
一个User对应一个Department,一个Department对应多个User。
package com.example.aandroidtest; import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.table.DatabaseTable; @DatabaseTable public class User { @DatabaseField(generatedId=true)//自增长的主键 private int id; @DatabaseField private String userName; @DatabaseField private String password; /** * 这个是一个普通的字段@DatabaseField,只是在后面加了个foreign * 在数据库中的名称默认为 depa_id * 查询到的User对象中可以直接获取到对应的Department * 删除User,并不会同时删除Department */ @DatabaseField(foreign=true,foreignAutoRefresh=true) private Department depa; //必须有一个无参的构造函数 public User() { super(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Department getDepa() { return depa; } public void setDepa(Department depa) { this.depa = depa; } }
package com.example.aandroidtest; import com.j256.ormlite.dao.ForeignCollection; import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.field.ForeignCollectionField; import com.j256.ormlite.table.DatabaseTable; /** * 部门 * @author LinZhiquan * */ @DatabaseTable public class Department { @DatabaseField(generatedId=true) private int id; @DatabaseField private String name; /** * 在数据库中不会生成对应的字段 * 获取到Department的时候同时会获取Department下面的User * * **要注意的是:删除Department的时候,并不会自动删除对应的User * 这个还没搞清楚哪里出的问题,待究 */ @ForeignCollectionField private ForeignCollection<User> users; public Department() { super(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public ForeignCollection<User> getUsers() { return users; } public void setUsers(ForeignCollection<User> users) { this.users = users; } }
package com.example.aandroidtest; import java.sql.SQLException; import android.content.Context; import android.database.sqlite.SQLiteDatabase; 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 DBHelper extends OrmLiteSqliteOpenHelper { /*一张表对应一个dao*/ private Dao<User, Integer> userDao; private Dao<Department, Integer> departmentDao; private final static int DB_VERSION = 1; private static final String DB_NAME = "orm"; /**构造函数,貌似是必须有的 * @param context */ public DBHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) { try { TableUtils.createTable(arg1, User.class); TableUtils.createTable(arg1, Department.class); } catch (SQLException e) { e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2, int arg3) { try { TableUtils.dropTable(arg1, User.class, true); TableUtils.dropTable(arg1, Department.class, true); this.onCreate(arg0, arg1); } catch (SQLException e) { e.printStackTrace(); } } public Dao<User, Integer> getUserDao() throws SQLException { if(userDao == null) { userDao = getDao(User.class); } return userDao; } public Dao<Department, Integer> getDepartmentDao() throws SQLException { if(departmentDao == null) { departmentDao = getDao(Department.class); } return departmentDao; } }