# RawQuery

Summary:[Methods](https://developer.android.com/reference/android/arch/persistence/room/RawQuery#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/arch/persistence/room/RawQuery#inhmethods)  

# RawQuery

| The`android.arch`Architecture Components packages are no longer maintained. They have been superseded by the corresponding[androidx.\*](https://developer.android.com/jetpack/androidx/migrate)packages. See[androidx.room.RawQuery](https://developer.android.com/reference/androidx/room/RawQuery)instead.

`
public


abstract
@interface
RawQuery
`  
`


implements

Annotation


`

|----------------------------------------|
| android.arch.persistence.room.RawQuery |

<br />

*** ** * ** ***

Marks a method in a[Dao](https://developer.android.com/reference/android/arch/persistence/room/Dao)annotated class as a raw query method where you can pass the query as a[SupportSQLiteQuery](https://developer.android.com/reference/android/arch/persistence/db/SupportSQLiteQuery).  

```
 @Dao
 interface RawDao {
     @RawQuery
     User getUserViaQuery(SupportSQLiteQuery query);
 }
 SimpleSQLiteQuery query = new SimpleSQLiteQuery("SELECT * FROM User WHERE id = ? LIMIT 1",
         new Object[]{userId});
 User user2 = rawDao.getUserViaQuery(query);
 
```

Room will generate the code based on the return type of the function and failure to pass a proper query will result in a runtime failure or an undefined result.

If you know the query at compile time, you should always prefer[Query](https://developer.android.com/reference/android/arch/persistence/room/Query)since it validates the query at compile time and also generates more efficient code since Room can compute the query result at compile time (e.g. it does not need to account for possibly missing columns in the response).

On the other hand,`RawQuery`serves as an escape hatch where you can build your own SQL query at runtime but still use Room to convert it into objects.

`RawQuery`methods must return a non-void type. If you want to execute a raw query that does not return any value, use[RoomDatabase#query](https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase#query(android.arch.persistence.db.SupportSQLiteQuery))methods.

RawQuery methods can only be used for read queries. For write queries, use[RoomDatabase.getOpenHelper().getWritableDatabase()](https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase#getOpenHelper()).

**Observable Queries:**

`RawQuery`methods can return observable types but you need to specify which tables are accessed in the query using the[observedEntities()](https://developer.android.com/reference/android/arch/persistence/room/RawQuery#observedEntities())field in the annotation.  

```
 @Dao
 interface RawDao {
     @RawQuery(observedEntities = User.class)
     LiveData<List<User>> getUsers(SupportSQLiteQuery query);
 }
 LiveData<List<User>> liveUsers = rawDao.getUsers(
     new SimpleSQLiteQuery("SELECT * FROM User ORDER BY name DESC"));
 
```
**Returning Pojos:**

RawQueries can also return plain old java objects, similar to[Query](https://developer.android.com/reference/android/arch/persistence/room/Query)methods.  

```
 public class NameAndLastName {
     public final String name;
     public final String lastName;

     public NameAndLastName(String name, String lastName) {
         this.name = name;
         this.lastName = lastName;
     }
 }

 @Dao
 interface RawDao {
     @RawQuery
     NameAndLastName getNameAndLastName(SupportSQLiteQuery query);
 }
 NameAndLastName result = rawDao.getNameAndLastName(
      new SimpleSQLiteQuery("SELECT * FROM User WHERE id = ?", new Object[]{userId}))
 // or
 NameAndLastName result = rawDao.getNameAndLastName(
      new SimpleSQLiteQuery("SELECT name, lastName FROM User WHERE id = ?",
          new Object[]{userId})))
 
```

**Pojos with Embedded Fields:**

`RawQuery`methods can return pojos that include[Embedded](https://developer.android.com/reference/android/arch/persistence/room/Embedded)fields as well.  

```
 public class UserAndPet {
     @Embedded
     public User user;
     @Embedded
     public Pet pet;
 }

 @Dao
 interface RawDao {
     @RawQuery
     UserAndPet getUserAndPet(SupportSQLiteQuery query);
 }
 UserAndPet received = rawDao.getUserAndPet(
         new SimpleSQLiteQuery("SELECT * FROM User, Pet WHERE User.id = Pet.userId LIMIT 1"))
 
```
**Relations:**

`RawQuery`return types can also be objects with[Relations](https://developer.android.com/reference/android/arch/persistence/room/Relation).  

```
 public class UserAndAllPets {
     @Embedded
     public User user;
     @Relation(parentColumn = "id", entityColumn = "userId")
     public List<Pet> pets;
 }

 @Dao
 interface RawDao {
     @RawQuery
     List<UserAndAllPets> getUsersAndAllPets(SupportSQLiteQuery query);
 }
 List<UserAndAllPets> result = rawDao.getUsersAndAllPets(
      new SimpleSQLiteQuery("SELECT * FROM users"));
 
```

<br />

## Summary

|                                                                                                                                 ### Public methods                                                                                                                                  ||
|------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` Class[]` | ` `[observedEntities](https://developer.android.com/reference/android/arch/persistence/room/RawQuery#observedEntities())`() ` Denotes the list of entities which are accessed in the provided query and should be observed for invalidation if the query is observable. |

| ### Inherited methods |
|-----------------------|---|
| From interface` java.lang.annotation.Annotation ` |-----------------------------------------|-------------------------| | ` abstract Class<? extends Annotation>` | ` annotationType() `    | | ` abstract boolean`                     | ` equals(Object arg0) ` | | ` abstract int`                         | ` hashCode() `          | | ` abstract String`                      | ` toString() `          | ||

## Public methods

### observedEntities

```
Class[] observedEntities ()
```

Denotes the list of entities which are accessed in the provided query and should be observed for invalidation if the query is observable.

The listed classes should either be annotated with[Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity)or they should reference to at least 1 Entity (via[Embedded](https://developer.android.com/reference/android/arch/persistence/room/Embedded)or[Relation](https://developer.android.com/reference/android/arch/persistence/room/Relation)).

Providing this field in a non-observable query has no impact.  

```
 @Dao
 interface RawDao {
     @RawQuery(observedEntities = User.class)
     LiveData<List<User>> getUsers(String query);
 }
 LiveData<List<User>> liveUsers = rawDao.getUsers("select * from User ORDER BY name
 DESC");
 
```

<br />

<br />

|                                     Returns                                     ||
|-----------|----------------------------------------------------------------------|
| `Class[]` | List of entities that should invalidate the query if changed. <br /> |

-

  ## Annotations

  - [ColumnInfo](https://developer.android.com/reference/android/arch/persistence/room/ColumnInfo)
  - [ColumnInfo.Collate](https://developer.android.com/reference/android/arch/persistence/room/ColumnInfo.Collate)
  - [ColumnInfo.SQLiteTypeAffinity](https://developer.android.com/reference/android/arch/persistence/room/ColumnInfo.SQLiteTypeAffinity)
  - [Dao](https://developer.android.com/reference/android/arch/persistence/room/Dao)
  - [Database](https://developer.android.com/reference/android/arch/persistence/room/Database)
  - [Delete](https://developer.android.com/reference/android/arch/persistence/room/Delete)
  - [Embedded](https://developer.android.com/reference/android/arch/persistence/room/Embedded)
  - [Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity)
  - [ForeignKey](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey)
  - [ForeignKey.Action](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey.Action)
  - [Ignore](https://developer.android.com/reference/android/arch/persistence/room/Ignore)
  - [Index](https://developer.android.com/reference/android/arch/persistence/room)
  - [Insert](https://developer.android.com/reference/android/arch/persistence/room/Insert)
  - [OnConflictStrategy](https://developer.android.com/reference/android/arch/persistence/room/OnConflictStrategy)
  - [PrimaryKey](https://developer.android.com/reference/android/arch/persistence/room/PrimaryKey)
  - [Query](https://developer.android.com/reference/android/arch/persistence/room/Query)
  - [RawQuery](https://developer.android.com/reference/android/arch/persistence/room/RawQuery)
  - [Relation](https://developer.android.com/reference/android/arch/persistence/room/Relation)
  - [SkipQueryVerification](https://developer.android.com/reference/android/arch/persistence/room/SkipQueryVerification)
  - [Transaction](https://developer.android.com/reference/android/arch/persistence/room/Transaction)
  - [TypeConverter](https://developer.android.com/reference/android/arch/persistence/room/TypeConverter)
  - [TypeConverters](https://developer.android.com/reference/android/arch/persistence/room/TypeConverters)
  - [Update](https://developer.android.com/reference/android/arch/persistence/room/Update)
-

  ## Classes

  - [DatabaseConfiguration](https://developer.android.com/reference/android/arch/persistence/room/DatabaseConfiguration)
  - [InvalidationTracker](https://developer.android.com/reference/android/arch/persistence/room/InvalidationTracker)
  - [InvalidationTracker.Observer](https://developer.android.com/reference/android/arch/persistence/room/InvalidationTracker.Observer)
  - [Room](https://developer.android.com/reference/android/arch/persistence/room/Room)
  - [RoomDatabase](https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase)
  - [RoomDatabase.Builder](https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase.Builder)
  - [RoomDatabase.Callback](https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase.Callback)
  - [RoomDatabase.MigrationContainer](https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase.MigrationContainer)
  - [RoomWarnings](https://developer.android.com/reference/android/arch/persistence/room/RoomWarnings)
  - [RxRoom](https://developer.android.com/reference/android/arch/persistence/room/RxRoom)
-

  ## Enums

  - [RoomDatabase.JournalMode](https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase.JournalMode)
-

  ## Exceptions

  - [EmptyResultSetException](https://developer.android.com/reference/android/arch/persistence/room/EmptyResultSetException)