// User and Book are classes annotated with @Entity.
@Database(version = 1, entities = {User.class, Book.class})
abstract class AppDatabase extends RoomDatabase {
// BookDao is a class annotated with @Dao.
abstract public BookDao bookDao();
// UserDao is a class annotated with @Dao.
abstract public UserDao userDao();
// UserBookDao is a class annotated with @Dao.
abstract public UserBookDao userBookDao();
}
The example above defines a class that has 2 tables and 3 DAO classes that are used to access it.
There is no limit on the number of Entity or Dao classes but they must be unique
within the Database.
Instead of running queries on the database directly, you are highly recommended to create
Dao classes. Using Dao classes will allow you to abstract the database communication in
a more logical layer which will be much easier to mock in tests (compared to running direct
sql queries). It also automatically does the conversion from Cursor to your application
classes so you don't need to deal with lower level database APIs for most of your data access.
Room also verifies all of your queries in Dao classes while the application is being
compiled so that if there is a problem in one of the queries, you will be notified instantly.
The list of entities included in the database. Each entity turns into a table in the
database.
Returns
Class[]
The list of entities in the database.
exportSchema
boolean exportSchema ()
You can set annotation processor argument (room.schemaLocation)
to tell Room to export the schema into a folder. Even though it is not mandatory, it is a
good practice to have version history in your codebase and you should commit that file into
your version control system (but don't ship it with your app!).
When room.schemaLocation is set, Room will check this variable and if it is set to
true, its schema will be exported into the given folder.
exportSchema is true by default but you can disable it for databases when
you don't want to keep history of versions (like an in-memory only database).
Returns
boolean
Whether the schema should be exported to the given folder when the
room.schemaLocation argument is set. Defaults to true.
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,["# Database\n\nSummary: [Methods](#pubmethods) \\| [Inherited Methods](#inhmethods) \n\nDatabase\n========\n\n| The `android.arch` Architecture Components packages are no longer maintained. They have been superseded by the corresponding [androidx.\\*](/jetpack/androidx/migrate) packages. See [androidx.room.Database](/reference/androidx/room/Database) instead.\n\n\n`\npublic\n\n\nabstract\n@interface\nDatabase\n`\n\n\n`\n\n\nimplements\n\nAnnotation\n\n\n`\n\n|----------------------------------------|\n| android.arch.persistence.room.Database |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nMarks a class as a RoomDatabase.\n\n\nThe class should be an abstract class and extend\n[RoomDatabase](/reference/android/arch/persistence/room/RoomDatabase).\n\n\nYou can receive an implementation of the class via\n[Room.databaseBuilder](/reference/android/arch/persistence/room/Room#databaseBuilder(android.content.Context, java.lang.Class\u003cT\u003e, java.lang.String)) or\n[Room.inMemoryDatabaseBuilder](/reference/android/arch/persistence/room/Room#inMemoryDatabaseBuilder(android.content.Context, java.lang.Class\u003cT\u003e)).\n\n\n```\n // User and Book are classes annotated with @Entity.\n @Database(version = 1, entities = {User.class, Book.class})\n abstract class AppDatabase extends RoomDatabase {\n // BookDao is a class annotated with @Dao.\n abstract public BookDao bookDao();\n // UserDao is a class annotated with @Dao.\n abstract public UserDao userDao();\n // UserBookDao is a class annotated with @Dao.\n abstract public UserBookDao userBookDao();\n }\n \n```\nThe example above defines a class that has 2 tables and 3 DAO classes that are used to access it. There is no limit on the number of [Entity](/reference/android/arch/persistence/room/Entity) or [Dao](/reference/android/arch/persistence/room/Dao) classes but they must be unique within the Database.\n\n\nInstead of running queries on the database directly, you are highly recommended to create\n[Dao](/reference/android/arch/persistence/room/Dao) classes. Using Dao classes will allow you to abstract the database communication in\na more logical layer which will be much easier to mock in tests (compared to running direct\nsql queries). It also automatically does the conversion from `Cursor` to your application\nclasses so you don't need to deal with lower level database APIs for most of your data access.\n\n\nRoom also verifies all of your queries in [Dao](/reference/android/arch/persistence/room/Dao) classes while the application is being\ncompiled so that if there is a problem in one of the queries, you will be notified instantly. \n**See also:**\n\n- [Dao](/reference/android/arch/persistence/room/Dao)\n- [Entity](/reference/android/arch/persistence/room/Entity)\n- [RoomDatabase](/reference/android/arch/persistence/room/RoomDatabase)\n\nSummary\n-------\n\n| ### Public methods ||\n|------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` Class[]` | ` `[entities](/reference/android/arch/persistence/room/Database#entities())`() ` The list of entities included in the database. |\n| ` boolean` | ` `[exportSchema](/reference/android/arch/persistence/room/Database#exportSchema())`() ` You can set annotation processor argument (`room.schemaLocation`) to tell Room to export the schema into a folder. |\n| ` int` | ` `[version](/reference/android/arch/persistence/room/Database#version())`() ` The database version. |\n\n| ### Inherited methods |\n|-----------------------|---|\n| From interface ` java.lang.annotation.Annotation ` |-----------------------------------------|-------------------------| | ` abstract Class\u003c? extends Annotation\u003e` | ` annotationType() ` | | ` abstract boolean` | ` equals(Object arg0) ` | | ` abstract int` | ` hashCode() ` | | ` abstract String` | ` toString() ` | ||\n\nPublic methods\n--------------\n\n### entities\n\n```\nClass[] entities ()\n```\n\nThe list of entities included in the database. Each entity turns into a table in the\ndatabase.\n\n\u003cbr /\u003e\n\n| Returns ||\n|-----------|----------------------------------------------|\n| `Class[]` | The list of entities in the database. \u003cbr /\u003e |\n\n### exportSchema\n\n```\nboolean exportSchema ()\n```\n\nYou can set annotation processor argument (`room.schemaLocation`)\nto tell Room to export the schema into a folder. Even though it is not mandatory, it is a\ngood practice to have version history in your codebase and you should commit that file into\nyour version control system (but don't ship it with your app!).\n\n\nWhen `room.schemaLocation` is set, Room will check this variable and if it is set to\n`true`, its schema will be exported into the given folder.\n\n\n`exportSchema` is `true` by default but you can disable it for databases when\nyou don't want to keep history of versions (like an in-memory only database).\n\n\u003cbr /\u003e\n\n| Returns ||\n|-----------|--------------------------------------------------------------------------------------------------------------------------------------|\n| `boolean` | Whether the schema should be exported to the given folder when the `room.schemaLocation` argument is set. Defaults to `true`. \u003cbr /\u003e |\n\n### version\n\n```\nint version ()\n```\n\nThe database version.\n\n\u003cbr /\u003e\n\n| Returns ||\n|-------|------------------------------|\n| `int` | The database version. \u003cbr /\u003e |\n\n-\n\n Annotations\n -----------\n\n - [ColumnInfo](/reference/android/arch/persistence/room/ColumnInfo)\n - [ColumnInfo.Collate](/reference/android/arch/persistence/room/ColumnInfo.Collate)\n - [ColumnInfo.SQLiteTypeAffinity](/reference/android/arch/persistence/room/ColumnInfo.SQLiteTypeAffinity)\n - [Dao](/reference/android/arch/persistence/room/Dao)\n - [Database](/reference/android/arch/persistence/room/Database)\n - [Delete](/reference/android/arch/persistence/room/Delete)\n - [Embedded](/reference/android/arch/persistence/room/Embedded)\n - [Entity](/reference/android/arch/persistence/room/Entity)\n - [ForeignKey](/reference/android/arch/persistence/room/ForeignKey)\n - [ForeignKey.Action](/reference/android/arch/persistence/room/ForeignKey.Action)\n - [Ignore](/reference/android/arch/persistence/room/Ignore)\n - [Index](/reference/android/arch/persistence/room)\n - [Insert](/reference/android/arch/persistence/room/Insert)\n - [OnConflictStrategy](/reference/android/arch/persistence/room/OnConflictStrategy)\n - [PrimaryKey](/reference/android/arch/persistence/room/PrimaryKey)\n - [Query](/reference/android/arch/persistence/room/Query)\n - [RawQuery](/reference/android/arch/persistence/room/RawQuery)\n - [Relation](/reference/android/arch/persistence/room/Relation)\n - [SkipQueryVerification](/reference/android/arch/persistence/room/SkipQueryVerification)\n - [Transaction](/reference/android/arch/persistence/room/Transaction)\n - [TypeConverter](/reference/android/arch/persistence/room/TypeConverter)\n - [TypeConverters](/reference/android/arch/persistence/room/TypeConverters)\n - [Update](/reference/android/arch/persistence/room/Update)\n-\n\n Classes\n -------\n\n - [DatabaseConfiguration](/reference/android/arch/persistence/room/DatabaseConfiguration)\n - [InvalidationTracker](/reference/android/arch/persistence/room/InvalidationTracker)\n - [InvalidationTracker.Observer](/reference/android/arch/persistence/room/InvalidationTracker.Observer)\n - [Room](/reference/android/arch/persistence/room/Room)\n - [RoomDatabase](/reference/android/arch/persistence/room/RoomDatabase)\n - [RoomDatabase.Builder](/reference/android/arch/persistence/room/RoomDatabase.Builder)\n - [RoomDatabase.Callback](/reference/android/arch/persistence/room/RoomDatabase.Callback)\n - [RoomDatabase.MigrationContainer](/reference/android/arch/persistence/room/RoomDatabase.MigrationContainer)\n - [RoomWarnings](/reference/android/arch/persistence/room/RoomWarnings)\n - [RxRoom](/reference/android/arch/persistence/room/RxRoom)\n-\n\n Enums\n -----\n\n - [RoomDatabase.JournalMode](/reference/android/arch/persistence/room/RoomDatabase.JournalMode)\n-\n\n Exceptions\n ----------\n\n - [EmptyResultSetException](/reference/android/arch/persistence/room/EmptyResultSetException)"]]