Stay organized with collections
Save and categorize content based on your preferences.
Version 2.1.0 and higher of the Room persistence
library provides support for SQLite database
views, allowing you
to encapsulate a query into a class. Room refers to these query-backed classes
as views, and they behave the same as simple data objects when used in a
DAO.
Create a view
To create a view, add the
@DatabaseView annotation to a class.
Set the annotation's value to the query that the class should represent.
The following code snippet provides an example of a view:
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;}
Associate a view with your database
To include this view as part of your app's database, include the
views property in your app's
@Database annotation:
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 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```"]]