OrmLite一対多

3755 ワード

プロジェクトでは1対多を使うので、少し理解して、テストに成功したコードをテストして、記録します.
1つのUserは1つのDepartmentに対応し、1つの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;
	}
}