javadatabaseandroid-studioandroid-roomandroid-room-relation

Why my Databaseview name appears as non found in my Dao?


I dont know why but Android Studiodoesnt find my DatabaseView. Even if i install others projects

This is the DatabaseView. Its created in a class called LibroDatos.

package com.example.pruebaexamenroom;

import androidx.room.DatabaseView;


@DatabaseView(value = "SELECT libro.id, libro.titulo, libro.autor," +
        "libro.idCliente,cliente.name as nombreCliente FROM libro " +
        "INNER JOIN Cliente ON libro.idCliente = cliente.idHumano",
            viewName = "v_LibroDatos")

public class LibroDatos {
    public int id;
    public String Titulo;
    public String Autor;
    public int idCliente;
    public String nombreCliente;

}

This is the Dao. Here is where its said that doesnt exist

package com.example.pruebaexamenroom;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Transaction;

import java.util.List;
@Dao
public interface DaoLibro {
        @Transaction
        @Query("SELECT * FROM v_LibroDatos")
        List<LibroDatos> obtenerLibros();

        @Insert
        void InsertarLibro(Libro...libros);

        @Query("UPDATE libro SET titulo =:titulo,autor=:autor WHERE id =:id")
        void updateLibro(int id,String titulo,String autor);

        @Query("DELETE FROM libro")
        void borrarLibro();

        
}

This is the AppDatabase. Its all declared in here.

package com.example.pruebaexamenroom;

import androidx.room.Database;
import androidx.room.RoomDatabase;

@Database(
        entities = {Libro.class, Cliente.class},
        views = {LibroDatos.class},
        version = 1
)
public abstract class AppDatabase extends RoomDatabase {
    public abstract DaoCliente daoCliente();
    public abstract DaoLibro daoLibro();
}

Solution

  • I believe that you can just ignore the error and that the project will compile successfully.

    If while coding the obtenerLibros Query you type in v for the view you will see that it does know as it prompts to complete e.g.

    enter image description here

    but then still flags it as an error. However it compiles successfully.

    Note if you look at the build log, then it has warnings:-

    warning: The query returns some columns [titulo, autor] which are not used by a.a.so75347005javaroomviewnotfound.LibroDatos. You can use @ColumnInfo annotation on the fields to specify the mapping. You can annotate the method with @RewriteQueriesToDropUnusedColumns to direct Room to rewrite your query to avoid fetching unused columns. 
    a.a.so75347005javaroomviewnotfound.LibroDatos has some fields [Titulo, Autor] which are not returned by the query. If they are not supposed to be read from the result, you can mark them with @Ignore annotation. You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: id, titulo, autor, idCliente, nombreCliente. - a.a.so75347005javaroomviewnotfound.DaoLibro.obtenerLibros()
    

    So you may wish to

    1. Include the @RewriteQueriesToDropUnusedColumns annotation
    2. Rename the member variables in the LibroDatos class to be lower case.

    for 1 :-

    @Transaction
    @RewriteQueriesToDropUnusedColumns //<<<<<<<<<< ADDED
    @Query("SELECT * FROM v_LibroDatos")
    List<LibroDatos> obtenerLibros();
    

    for 2 :-

    public class LibroDatos {
       public int id;
       public String titulo; //<<<<<<<<<< was Titulo
       public String autor; //<<<<<<<<< was Autor
       public int idCliente;
       public String nombreCliente;
    
    }