java - 实体构造函数给出“不兼容的类型”错误
问题描述
因此,为了提供一些背景信息,我目前正在开发一个带有数据库的应用程序。通过研究,很明显 Google 的 Room(使用 SQLite)是可行的方法。所以我已经完全设置了我的数据库,带有外键,还有@Transaction
's (代表一对多或一对一的关系)。
目前,我一整天都被困在某件事上:每当我尝试为实体(表)创建构造函数时,如果除了 String 作为参数之外还有其他东西,它会给我incompatible types: <null> cannot be converted to int
错误。(或浮动或双重等)。
我只是无法弄清楚为什么会发生这种情况,还尝试删除所有外键并尝试使用尽可能少的代码,但它不起作用。
仅供参考:错误发生在生成的类中。此类是从您的 DAO 接口 (DatabaseAccessInterface) 生成的,您可以在其中列出所有查询函数,包括您的关系。生成的类比调用相关实体的构造函数:_tmpFan = new Fan(null,null,null,null,null,null,_tmpCreatedAt);
像这样,比说incompatible types: <null> cannot be converted to int
以前有没有人遇到过这个错误,或者你只需要坚持使用字符串?如果您需要了解我的代码的一部分,只需询问,我会提供。(在一篇文章中发布所有代码将是相当多的代码,但我将在下面发布一些片段)
接口内的事务/关系:
@Transaction
@Query("SELECT * FROM measurement_session WHERE fan_id = :fanID")
public List<FansWithMeasureSessions> getMeasureSessionsOfFan(int fanID);
粉丝类:
@Entity (tableName = "fan",
foreignKeys = {
@ForeignKey(entity = MeasurementSession.class,
parentColumns = "fan_id",
childColumns = "fan_id",
onDelete = ForeignKey.CASCADE)
},
indices = {
@Index(value = "customer_id", unique = true)
}
)
public class Fan {
@ColumnInfo(name = "customer_id")
public int customerID;
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "fan_id")
public int fanID;
@ColumnInfo(name = "fan_name")
public String fanName;
@ColumnInfo(name = "fan_type")
public String fanType;
@ColumnInfo(name = "diameter")
public float diameter;
@ColumnInfo(name = "length")
public float length;
@ColumnInfo(name = "width")
public float width;
@ColumnInfo(name = "created_at")
@TypeConverters(DateConverter.class)
public Date createdAt;
public Fan(int customerID, String fanName, String fanType, float diameter, float length, float width, Date createdAt) {
this.customerID = customerID;
this.fanName = fanName;
this.fanType = fanType;
this.diameter = diameter;
this.length = length;
this.width = width;
this.createdAt = createdAt;
}
}
FansWithMeasureSessions 类:
public class FansWithMeasureSessions {
@Embedded public Fan fan;
@Relation(
entity = MeasurementSession.class,
parentColumn = "fan_id",
entityColumn = "fan_id"
)
public List<MeasurementSession> measurementSessions;
}
注释掉了我的大部分其他代码,同时仍然保持错误,所以应该这样做!
解决方案
解决方案:
float
对,int
等使用可空类型double
。
这意味着您应该改用Float
, Integer
,Double
数据类型(可以为空)
推荐阅读
- python - Selenium 似乎被列入黑名单
- tomcat - windows10上的Tomcat vs ubuntu url参数数组= 400错误请求
- vue.js - 注册 Vuex 模块的问题,要么找不到命名空间,要么 getters.default 为空
- java - 设计基于 Spring 的 kafka 监听器/生产者的类结构
- sql - SQL 查询中的连接问题
- c# - 从子类值设置 DataGridView 单元格值
- javascript - 自定义环回模型
- sql - SQL 自联合命名?
- c# - 存储库模式的更新概念
- python - 如何为 Python 安装 SHAP (Shapley)