我该如何正确使用ORMLite注释继承类? [英] How do I properly annotate inheritance classes using ORMLite?

查看:462
本文介绍了我该如何正确使用ORMLite注释继承类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用继承与ORMLite,如果它是支持的,我不能工作了,或不从看文档和谷歌搜索。

我想要做的是有

 公共抽象类Person {
   公众诠释ID;
   公共字符串名称;
}

公共类学生扩展人{
   公共字符串学校;
    公共字符串一年;
    //其他学生的东西
}

公共班主任扩展人{
   公共字符串称号;
    //其他老师的东西
}
 

我不能工作了(假设它的支持)是如何注释3类ORMLite。

我只需要注释具体的类与 @DatabaseTable(tableName值=学生)或者我需要抽象类也?

我不断收到错误,如:

  

十月4号至24号:18:30.857:E / AndroidRuntime(30495):java.lang.RuntimeException的:值java.sql.SQLException:未知领域的名称从Android sqlite的游标,而不是在:产生的原因[年份,学校]

解决方案

@DatabaseTable 注释只需要在学生教师表,也不会被使用,如果它是在基类。

您需要有什么是对 ID的 @DatabaseField 注释人员字段。例如:

 公共抽象类Person {
    @DatabaseField(generatedId =真)
    公众诠释ID;
    @DatabaseField
    公共字符串名称;
}
 

ORMLite 应该走的类层次结构,并从基类中的任何字段应该包含在学生教师表。如果您编辑您的问题,以显示 @DatabaseField 或其他注释,我可以多加评论。

I'm trying to use inheritance with ORMLite and I can't work out if it is supported or not from looking at the documentation and googling.

What I want to do is have

public abstract class Person{
   public int id;
   public String name;
}

public class Student extends Person{
   public String school;
    public String year;
    //  other student stuff
}

public class Teacher extends Person{
   public String title;
    // other teacher stuff
}

What I can't work out (assuming it's supported) is how to annotate the 3 classes for ORMLite.

Do I only need to annotate the concrete classes with @DatabaseTable(tableName = "Student") or do I need the abstract class also?

I keep getting errors like:

04-24 10:18:30.857: E/AndroidRuntime(30495): Caused by: java.lang.RuntimeException: java.sql.SQLException: Unknown field 'name' from the Android sqlite cursor, not in:[year, school]

解决方案

The @DatabaseTable annotation is only necessary on the Student or Teacher tables and would not be used if it was on the Person base class.

What you need to have is a @DatabaseField annotation on the id and name fields in Person. For example:

public abstract class Person{
    @DatabaseField(generatedId = true)
    public int id;
    @DatabaseField
    public String name;
}

ORMLite should walk the class hierarchy and any fields from the base class should be included in the Student and Teacher tables. If you edit your question to show the @DatabaseField or other annotations, I can comment more.

这篇关于我该如何正确使用ORMLite注释继承类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆