如何在OrmLite中使用内部联接?

时间:2018-06-19 16:26:05

标签: java sqlite ormlite

我有2个表: tbl_employee

CREATE TABLE tbl_employee (
    id        INT (4)      PRIMARY KEY,
    firstname VARCHAR (20),
    lastname  VARCHAR (20),
    email     VARCHAR (40),
    status    INT (1)      REFERENCES tbl_employee_status (code) 
);

tbl_employee_status

CREATE TABLE tbl_employee_status (
    code        INT (1)      PRIMARY KEY,
    description VARCHAR (30) 
);

我想做的是一个内部联接。在SQLite中引用简单:

select firstname, lastname, email, description from tbl_employee inner join tbl_employee_status where tbl_employee_status.code = tbl_employee.status

现在我正试图用OrmLite来做

private final static String DATABASE_URL = "jdbc:sqlite:C://MyDB//db_employee.db";
private static Dao EmployeeDao; 
private static Dao EmployeeStatusDao;

public static void main(String[] args) throws Exception {
    ConnectionSource connectionSource = null;

    connectionSource = new JdbcConnectionSource(DATABASE_URL);
    EmployeeDao = DaoManager.createDao(connectionSource, Employee.class);
    EmployeeStatusDao = DaoManager.createDao(connectionSource, EmployeeStatus.class);

    QueryBuilder qb1 = EmployeeDao.queryBuilder();
    QueryBuilder qb2 = EmployeeStatusDao.queryBuilder();

    List<Employee> employeeList = qb1.join(qb2).query();
}

Employee.java

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "tbl_employee")
public class Employee {

    public static final String FIRSTNAME_FIELD = "firstname";
    public static final String LASTNAME_FIELD = "lastname";
    public static final String EMAIL_FIELD = "email";
    public static final String STATUS_FIELD = "status";

    @DatabaseField(columnName = FIRSTNAME_FIELD)
    private String firstname;

    @DatabaseField(columnName = LASTNAME_FIELD)
    private String lastname;

    @DatabaseField(columnName = EMAIL_FIELD)
    private String email;

    @DatabaseField(columnName = STATUS_FIELD)
    private String status;
}

EmployeeStatus.java

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "tbl_employee_status")
public class EmployeeStatus {

    public static final String CODE_FIELD = "code";
    public static final String DESCRIPTION_FIELD = "description";

    @DatabaseField(columnName = CODE_FIELD, foreign=true, foreignAutoCreate=true, foreignAutoRefresh=true)
    private String code;

    @DatabaseField(columnName = DESCRIPTION_FIELD)
    private String status;    

}

有错误:

  

线程“主”中的异常java.lang.IllegalArgumentException:否   字段在类java.lang.String中具有DatabaseField批注,位于   com.j256.ormlite.table.DatabaseTableConfig.extractFieldTypes(DatabaseTableConfig.java:249)     在   com.j256.ormlite.table.DatabaseTableConfig.fromClass(DatabaseTableConfig.java:176)

猜猜这不是进行内部联接的正确方法。你能给我澄清一下吗?谢谢。

0 个答案:

没有答案