3

When building my app, I am getting an error with two entity classes. It's basically saying it cannot find the setter for the constructors I am giving. Originally I had vals, I converted these to vars and that seems to fix the issue.

But I don't like this fix... The documentation uses vals in their examples of how to build entities. So why does it not work for certain entities I define? I feel like the coding I'm doing is going to be prone to some sort of error because I am only bandaging the problem rather than actually fixing it.

https://developer.android.com/training/data-storage/room/defining-data

data class DirectMessage(
    @PrimaryKey
    @ColumnInfo(name = "dm_id")  override val objectId: String,

    //Author of message
    @Ignore override val author: User,

    //Recipient of message, usually user of the app
    @Ignore override val recipient: User,

    //Content of message
    override val content: String,
    //Date of creation
    override val timestamp: Long

) : DirectMessage {

    //Place identifier into database instead of Entity
    @ColumnInfo(name = "author") val _author : String = author.uid
    @ColumnInfo(name = "recipient") val _recipient : String = recipient.uid

    /**
     * Get author's user thumbnail
     */
    override fun getThumbnail() {
        TODO("Not yet implemented")
    }

}

@Entity
data class Comment (

    @PrimaryKey
    @ColumnInfo(name = "cid") override val objectId: String,

    //Author of the comment
    @Ignore override val author: User,

    //Moment ID this comment is attached to
    override var momentId: String,

    //Time comment was created
    override val timestamp: Long,

    //Content of the comment
    override var content: String

    ) : Comment {

    //Place identifier into database instead of User entity
    @ColumnInfo(name = "author") val _author = author.uid

    /**
     * Get thumbnail of User object
     */
    override fun getThumbnail() {
        TODO("Not yet implemented")
    }

}

interface Comment : Message {

    val momentId : String
}

public final class Comment implements com.example.barrechat192.data.entities.Comment {
             ^
  Tried the following constructors but they failed to match:
  Comment(java.lang.String,com.example.barrechat192.data.entities.User,java.lang.String,long,java.lang.String) -> [param:objectId -> matched field:objectId, param:author -> matched field:unmatched, param:momentId -> matched field:momentId, param:timestamp -> matched field:timestamp, param:content -> matched field:content]C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\Comment.java:13: error: Cannot find setter for field.
    private final java.lang.String _author = null;
                                   ^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\Comment.java:17: error: Cannot find setter for field.
    private final java.lang.String objectId = null;
                                   ^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\Comment.java:23: error: Cannot find setter for field.
    private final long timestamp = 0L;
                       ^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\DirectMessage.java:7: 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 DirectMessage implements com.example.barrechat192.data.entities.DirectMessage {
             ^
  Tried the following constructors but they failed to match:
  DirectMessage(java.lang.String,com.example.barrechat192.data.entities.User,com.example.barrechat192.data.entities.User,java.lang.String,long) -> [param:objectId -> matched field:objectId, param:author -> matched field:unmatched, param:recipient -> matched field:unmatched, param:content -> matched field:content, param:timestamp -> matched field:timestamp]C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\DirectMessage.java:10: error: Cannot find setter for field.
    private final java.lang.String _author = null;
                                   ^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\DirectMessage.java:13: error: Cannot find setter for field.
    private final java.lang.String _recipient = null;
                                   ^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\DirectMessage.java:17: error: Cannot find setter for field.
    private final java.lang.String objectId = null;
                                   ^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\DirectMessage.java:25: error: Cannot find setter for field.
    private final java.lang.String content = null;
                                   ^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\DirectMessage.java:26: error: Cannot find setter for field.
    private final long timestamp = 0L;
                       ^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\mapobjects\MapObject.java:10: error: The name "map_objects" is used by multiple entities or views: com.example.barrechat192.data.entities.implementations.mapobjects.MapObject, com.example.barrechat192.data.entities.implementations.mapobjects.MapObject
public final class MapObject implements com.example.barrechat192.data.entities.MapObject {
             ^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\AppDatabase.java:8: error: The name "map_objects" is used by multiple entities or views: com.example.barrechat192.data.entities.implementations.mapobjects.MapObject, com.example.barrechat192.data.entities.implementations.mapobjects.MapObject
public abstract class AppDatabase extends androidx.room.RoomDatabase {
                ^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\geocache\GeoTableWithMapObjects.java:12: error: Cannot find the child entity column `geohash` in com.example.barrechat192.data.entities.implementations.mapobjects.MapObject. Options: objectId, timestamp, thumbnailUrl, thumbnailPath, objectType, local, views, viewed, geoHash, latitude, longitude
    private final java.util.List<com.example.barrechat192.data.entities.implementations.mapobjects.MapObject> mapObjects = null;
                                                                                                              ^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\geocache\GeoTableWithMapObjects.java:6: 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).

I also don't understand why I can inherit a val and change it to a var in a child class in Kotlin. That also seems to make the code work, changing the val to var in the entity class, but leaving it as a val in the interface.

chrisdottel
  • 1,053
  • 1
  • 12
  • 21
  • Does this answer your question? [Android Room Database Ignore Problem “Tried the following constructors but they failed to match”](https://stackoverflow.com/questions/64541459/android-room-database-ignore-problem-tried-the-following-constructors-but-they) – sergiy tikhonov Nov 11 '20 at 21:21
  • I am facing the same problem. Do you have a solution? – Dinh Quang Tuan May 30 '23 at 05:25

2 Answers2

1

Hi you have created constructors which is used when it is created by you. You should create constructor passing params all database columns. Room is trying to create an instance but can not find setters for these mapped fields

-2

You Need create setter for your model, the database cannot set the data.

See this: Getter and Setter