#android #kotlin #android-room #android-jetpack
#Android #kotlin #android-room #android-реактивный ранец
Вопрос:
У меня есть объект базы данных Room, называемый Conversation, и я хотел бы androidx.room.@Ignore
messages
использовать переменную-член. Однако, когда я пытаюсь запустить приложение, я получаю эту ошибку.
/db/entity/Conversation.java:10: 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).
public final class Conversation {
^
Tried the following constructors but they failed to match:
Conversation(int,java.util.List<com.example.app.db.entity.Message>) -> [param:id -> matched field:id, param:messages -> matched field:unmatched]/db/entity/Conversation.java:12: error: Cannot find setter for field.
private final int id = 0;
Вот код сущности.
/**
* Entity used to model the conversation SQLite table in the database.
*/
@Entity(tableName = "conversation")
data class Conversation(
@PrimaryKey val id: Int,
@Ignore val messages: List<Message>
)
Когда я комментирую @Ignore val messages...
строку, приложение компилируется.
Я пробовал обе версии базы данных Room 2.2.5
2.3.0-alpha03
, и в обеих были обнаружены одинаковые ошибки.
Ответ №1:
Я смог устранить эту проблему, преобразовав сущность в класс, не содержащий данных:
/**
* Entity used to model the conversation SQLite table in the database.
*/
@Entity(tableName = "conversation")
class Conversation constructor(@PrimaryKey val id: Int) {
@Ignore
val messages = mutableListOf<Message>()
}