无法识别安卓房间类型转换器
2022-09-03 02:45:03
我正在尝试实现一个持久化日期。它非常通用,但我一直得到:DateConverter
无法弄清楚如何将此字段保存到数据库中。您可以考虑为其添加类型转换器。
我仔细检查了一下,并在数据库级别和字段级别定义了注释,但它仍然无法确定为字段定义了类型转换器。
我的成绩文件依赖关系:
compile('android.arch.persistence.room:runtime:1.0.0-alpha3') {
exclude group: 'com.google.code.gson'
}
compile('android.arch.persistence.room:rxjava2:1.0.0-alpha3') {
exclude group: 'com.google.code.gson'
}
testCompile('android.arch.persistence.room:testing:1.0.0-alpha3') {
exclude group: 'com.google.code.gson'
}
annotationProcessor('android.arch.persistence.room:compiler:1.0.0-alpha3') {
exclude group: 'com.google.code.gson'
}
在我的应用程序数据库中:
@Database(entities = {Location.class, Room.class, Amenity.class, UserModel.class, EventModel.class}, version =1, exportSchema = false)
@TypeConverters({DateConverter.class})
public abstract class AppDatabase extends RoomDatabase {
public abstract RoomDao getRoomDao();
public abstract LocationDao getLocationDao();
public abstract AmenityDao getAmenityDao();
public abstract UserDao getUserDao();
public abstract EventDao getEventDao();
}
然后我的类看起来像这样:DateConverter
public class DateConverter {
@TypeConverter
public static Date toDate(Long timestamp) {
return timestamp == null ? null : new Date(timestamp);
}
@TypeConverter
public static long toTimestamp(Date date) {
return date == null ? null : date.getTime();
}
}
我的实体如下所示:
@Entity(tableName = "event")
@TypeConverters({DateConverter.class})
public class EventModel {
@PrimaryKey
private String uuid;
@ColumnInfo(name = "subject")
private String subject;
@TypeConverters({DateConverter.class})
private Date start;
@TypeConverters({DateConverter.class})
private Date end;
@ColumnInfo(name = "confirmed")
private Boolean confirmed;
public String getUuid() {return uuid;}
public void setUuid(String uuid) {this.uuid = uuid;}
public String getSubject() {return subject;}
public void setSubject(String subject) {
this.subject = subject;
}
public Date getStart() {return start;}
public void setStart(Date date) {this.start = date;}
public Date getEnd() {return end;}
public void setEnd (Date end) {
this.end = end;
}
public Boolean getConfirmed() {return confirmed;}
public void setConfirmed(Boolean confirmed) {
this.confirmed = confirmed;
}
}
我还错过了什么吗?谢谢!