Home > Software engineering >  Room error - "Cannot figure out how to save this field into database. You can consider adding a
Room error - "Cannot figure out how to save this field into database. You can consider adding a

Time:07-18

I want to store class Reservation that a user makes when going to a cinema to watch a movie and I can't figure out how to convert it so that room can store it in the data base.

Errors appear for members: places and codeQR.

Class Reservation:

@Entity(tableName = "reservations", foreignKeys = @ForeignKey(entity = UtilizatorMADA.class, parentColumns = "id", childColumns = "idUser"))
public class Reservation implements Serializable {
                @PrimaryKey(autoGenerate = true)
                private long idReservation;
                private long idUser;
                @Embedded private Film film;
                private Time startTime;
                private List<Integer> places;
                private Bitmap codeQR;
            
                @Ignore
                public Reservation(){}
            
                public Reservation(long idReservation, long idUser, Film film, Time startTime, List<Integer> places, Bitmap codeQR) {
                    this.idReservation = idReservation;
                    this.idUser = idUser;
                    this.film = film;
                    this.startTime = startTime;
                    this.places = places;
                    this.codeQR = codeQR;
                }
            
                @Ignore
                public Reservation(long idUser, Film film, Time startTime, List<Integer> places, Bitmap codeQR) {
                    this.idUser = idUser;
                    this.film = film;
                    this.startTime = startTime;
                    this.places = places;
                    this.codeQR = codeQR;
                }
            
                @Ignore
                public Reservation(Film film, Time startTime, List<Integer> places, Bitmap codeQR) {
                    this.film = film;
                    this.startTime = startTime;
                    this.places = places;
                    this.codeQR = codeQR;
                }
            
                public long getIdReservation() {
                    return idReservation;
                }
            
                public void setIdReservation(long idReservation) {
                    this.idReservation = idReservation;
                }
            
                public long getIdUser() {
                    return idUser;
                }
            
                public void setIdUser(long idUser) {
                    this.idUser = idUser;
                }
            
                public void setPlaces(List<Integer> places) {
                    this.places = places;
                }
            
                public Film getFilm() {
                    return film;
                }
            
                public void setFilm(Film film) {
                    this.film = film;
                }
            
                public Time getStartTime() {
                    return startTime;
                }
            
                public void setStartTime(Time startTime) {
                    this.startTime = startTime;
                }
            
                public Bitmap getCodeQR() {
                    return codeQR;
                }
            
                public void setCodeQR(Bitmap codeQR) {
                    this.codeQR = codeQR;
                }
            
                public List<Integer> getPlaces() { return places; }
            
                @Override
                public String toString() {
                    return "Reservation{"  
                            "film="   film  
                            ", startTime="   startTime  
                            ", codeQR='"   codeQR   '\''  
                            '}';
                }
            
                @RequiresApi(api = Build.VERSION_CODES.KITKAT)
                @Override
                public boolean equals(Object o) {
                    if (this == o) return true;
          
        
          if (o == null || getClass() != o.getClass()) return false;
                Reservation that = (Reservation) o;
                return Objects.equals(film, that.film) &&
                        Objects.equals(startTime, that.startTime) &&
                        Objects.equals(places, that.places);
            }
        
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public int hashCode() {
                return Objects.hash(film, startTime, places);
            }
        }

Class Film:

@Entity(tableName = "films")
public class Film implements Serializable {
    @PrimaryKey(autoGenerate = true)
    private int idFilm;
    private String title;
    private String genre;
    private String description;
    private double rating;
    private int imagePath;
    private boolean isFavorite;

    public Film(int idFilm, String title, String genre, String description, double rating, int imagePath, boolean isFavorite) {
        this.idFilm = idFilm;
        this.title = title;
        this.genre = genre;
        this.description = description;
        this.rating = rating;
        this.imagePath = imagePath;
        this.isFavorite = isFavorite;
    }

    @Ignore
    public Film(String title, String genre, String description, double rating, int imagePath) {
        this.title = title;
        this.genre = genre;
        this.description = description;
        this.rating = rating;
        this.imagePath = imagePath;
        isFavorite = false;
    }

    @Ignore
    public Film(String title) {
        this.title = title;
    }

    public int getIdFilm() {
        return idFilm;
    }

    public void setIdFilm(int idFilm) {
        this.idFilm = idFilm;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public double getRating() {
        return rating;
    }

    public void setRating(double rating) {
        this.rating = rating;
    }

    public int getImagePath() {
        return imagePath;
    }

    public void setImagePath(int imagePath) {
        this.imagePath = imagePath;
    }

    public boolean isFavorite() {
        return isFavorite;
    }

    public void setFavorite(boolean favorite) {
        isFavorite = favorite;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Film film = (Film) o;
        return Objects.equals(title, film.title);
    }

    @Override
    public int hashCode() {
        return Objects.hash(title);
    }
}

I also get the following error by the ReservationOfMoviesDao, which may or may not be related. The queries affected are: getReservationOfMoviesByUserId(long id), getReservationOfMovies() and getReservationOfMoviesByRservationId(long id);

error: The columns returned by the query does not have the fields [idFilm] in com.example.cinemaapp.model.ReservationMovie even though they are annotated as non-null or primitive. Columns returned by the query: [idReservation,idUser,film,startTime,places,codeQR]

Interface ReservationOfMoviesDao:

@Dao
public interface ReservationOfMoviesDao {
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    void insert(ReservationMovie rm);

    @Transaction
    @Query("SELECT * FROM reservations where idUser = :id")
    List<ReservationMovie> getReservationOfMoviesByUserId(long id);

    @Transaction
    @Query("SELECT * FROM reservations")
    List<ReservationMovie> getReservationOfMovies();

    @Transaction
    @Query("SELECT * FROM reservations where idReservation = :id")
    ReservationMovie getReservationOfMoviesByReservationId(long id);
}

If you could help me out please, I'd appreciate it.

CodePudding user response:

You cant use entities inside other entities (e.g. Film in Reservation). You should either use "relationships between entities" or try "Embedded" annotation. Reffer to this link for more info.

  • Related