对于库模块中定义的POJO的@NonNull带注释的构造函数参数,Android Room @Embedded注释编译失败 [英] Android Room @Embedded annotation compilation fails for @NonNull annotated constructor parameters of a POJO defined in a library module
问题描述
我有一个嵌入到房间实体中的POJO;请注意, POJO是在库模块中定义的;
I have a POJO which I am embedding in a Room Entity; Please note that the POJO is defined in a library module;
@Entity
public class Person {
@PrimaryKey
@NonNull
private String uuid;
@Embedded
@NonNull
private Address address;
public Person(@NonNull String uuid, @NonNull Address address) {
this.uuid = uuid;
this.address = address;
}
@NonNull
public String getUuid() {
return uuid;
}
@NonNull
public Address getAddress() {
return address;
}
}
public class Address {
@NonNull
private String street;
@NonNull
private String city;
public Address(@NonNull String street, @NonNull String city) {
this.street = street;
this.city = city;
}
@NonNull
public String getStreet() {
return street;
}
@NonNull
public String getCity() {
return city;
}
}
@Dao
public interface PersonDao {
@Query("SELECT * FROM Person")
List<Person> getPersons();
}
@TypeConverters(DateConverter.class)
@Database(entities = {Person.class}, version = 1, exportSchema = false)
public abstract class PersonDb extends RoomDatabase {
private static volatile PersonDb INSTANCE;
public abstract PersonDao getPersonDao();
}
编译失败,显示
错误:实体和Pojos必须具有可用的公共构造函数.您 可以有一个空的构造函数或参数匹配的构造函数 字段(按名称和类型)." 错误:找不到字段的设置器.
"Error:Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type)." Error:Cannot find setter for field.
如果我从构造函数的参数中删除@NonNull
批注到Address POJO,则代码可以正常编译.
另外,如果相同的POJO位于app模块中,则代码将编译.
If I remove @NonNull
annotation from the constructors parameters to Address POJO, the code compiles fine.
Also, if the same POJO is in the app module, code compiles.
可以看出,Address
类确实具有公共构造函数.
As can be seen, Address
class does have a public constructor.
我在这里想念什么?从Room的角度来看,usable constructor
的定义是什么?还是房间有问题?
What am I missing here? What is the definition of usable constructor
in Room's perspective? Or it is an issue with Room?
推荐答案
文档说: "要保留字段,Room必须有权访问它.可以公开一个字段,也可以为其提供一个getter和setter.如果使用getter和setter方法,请记住它们基于Room中的JavaBeans约定."
uuid
和address
声明为private
,您可以使用这些方法的getter方法,但没有设置方法.
因此,您可以创建setter或将其声明为public
.
uuid
and address
are declared as private
in Person
, you have getters for those but no setters.
So you can create setters or declare them as public
.
这篇关于对于库模块中定义的POJO的@NonNull带注释的构造函数参数,Android Room @Embedded注释编译失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!