Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
La versión 2.1.0 de la biblioteca de persistencias Room (y versiones posteriores) proporciona compatibilidad con vistas de bases de datos SQLite, lo que te permite encapsular una búsqueda en una clase. Room se refiere a las clases respaldadas por búsquedas como vistas, que se comportan igual que los objetos de datos simples cuando se usan en un DAO.
Cómo crear una vista
Para crear una vista, agrega la anotación @DatabaseView a una clase.
Establece el valor de la anotación para la búsqueda que la clase debe representar.
En el siguiente fragmento de código, se proporciona un ejemplo de vista:
Kotlin
@DatabaseView("SELECT user.id, user.name, user.departmentId,"+"department.name AS departmentName FROM user "+"INNER JOIN department ON user.departmentId = department.id")dataclassUserDetail(valid:Long,valname:String?,valdepartmentId:Long,valdepartmentName:String?)
Java
@DatabaseView("SELECT user.id, user.name, user.departmentId,"+"department.name AS departmentName FROM user "+"INNER JOIN department ON user.departmentId = department.id")publicclassUserDetail{publiclongid;publicStringname;publiclongdepartmentId;publicStringdepartmentName;}
Cómo asociar una vista con tu base de datos
Para incluir la vista como parte de la base de datos de tu app, incluye la propiedad views en la anotación @Database de tu app como se muestra a continuación:
El contenido y las muestras de código que aparecen en esta página están sujetas a las licencias que se describen en la Licencia de Contenido. Java y OpenJDK son marcas registradas de Oracle o sus afiliados.
Última actualización: 2025-07-27 (UTC)
[null,null,["Última actualización: 2025-07-27 (UTC)"],[],[],null,["# Create views into a database\n\nVersion 2.1.0 and higher of the [Room persistence\nlibrary](/training/data-storage/room) provides support for [SQLite database\nviews](https://www.sqlite.org/lang_createview.html), allowing you\nto encapsulate a query into a class. Room refers to these query-backed classes\nas *views* , and they behave the same as simple data objects when used in a\n[DAO](/training/data-storage/room/accessing-data).\n| **Note:** Like [entities](/training/data-storage/room/defining-data), you can run `SELECT` statements against views. However, you cannot run `INSERT`, `UPDATE`, or `DELETE` statements against views.\n\nCreate a view\n-------------\n\nTo create a view, add the\n[`@DatabaseView`](/reference/androidx/room/DatabaseView) annotation to a class.\nSet the annotation's value to the query that the class should represent.\n\nThe following code snippet provides an example of a view: \n\n### Kotlin\n\n```kotlin\n@DatabaseView(\"SELECT user.id, user.name, user.departmentId,\" +\n \"department.name AS departmentName FROM user \" +\n \"INNER JOIN department ON user.departmentId = department.id\")\ndata class UserDetail(\n val id: Long,\n val name: String?,\n val departmentId: Long,\n val departmentName: String?\n)\n```\n\n### Java\n\n```java\n@DatabaseView(\"SELECT user.id, user.name, user.departmentId,\" +\n \"department.name AS departmentName FROM user \" +\n \"INNER JOIN department ON user.departmentId = department.id\")\npublic class UserDetail {\n public long id;\n public String name;\n public long departmentId;\n public String departmentName;\n}\n```\n\nAssociate a view with your database\n-----------------------------------\n\nTo include this view as part of your app's database, include the\n[`views`](/reference/androidx/room/Database#views) property in your app's\n`@Database` annotation: \n\n### Kotlin\n\n```kotlin\n@Database(entities = [User::class],\n views =[UserDetail::class], version = 1)\nabstract class AppDatabase : RoomDatabase() {\n abstract fun userDao(): UserDao\n}\n```\n\n### Java\n\n```java\n@Database(entities = {User.class}, views = {UserDetail.class},\n version = 1)\npublic abstract class AppDatabase extends RoomDatabase {\n public abstract UserDao userDao();\n}\n```"]]