# Relation

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

# Relation

| 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.Relation](https://developer.android.com/reference/androidx/room/Relation)instead.

`
public


abstract
@interface
Relation
`  
`


implements

Annotation


`

|----------------------------------------|
| android.arch.persistence.room.Relation |

<br />

*** ** * ** ***

A convenience annotation which can be used in a Pojo to automatically fetch relation entities. When the Pojo is returned from a query, all of its relations are also fetched by Room.  

```
 @Entity
 public class Pet {
     @ PrimaryKey
     int id;
     int userId;
     String name;
     // other fields
 }
 public class UserNameAndAllPets {
   public int id;
   public String name;
   @Relation(parentColumn = "id", entityColumn = "userId")
   public List<Pet> pets;
 }

 @Dao
 public interface UserPetDao {
     @Query("SELECT id, name from User")
     public List<UserNameAndAllPets> loadUserAndPets();
 }
 
```

The type of the field annotated with`Relation`must be a[List](https://developer.android.com/reference/java/util/List)or[Set](https://developer.android.com/reference/java/util/Set). By default, the[Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity)type is inferred from the return type. If you would like to return a different object, you can specify the[entity()](https://developer.android.com/reference/android/arch/persistence/room/Relation#entity())property in the annotation.  

```
 public class User {
     int id;
     // other fields
 }
 public class PetNameAndId {
     int id;
     String name;
 }
 public class UserAllPets {
   @Embedded
   public User user;
   @Relation(parentColumn = "id", entityColumn = "userId", entity = Pet.class)
   public List<PetNameAndId> pets;
 }
 @Dao
 public interface UserPetDao {
     @Query("SELECT * from User")
     public List<UserAllPets> loadUserAndPets();
 }
 
```

In the example above,`PetNameAndId`is a regular Pojo but all of fields are fetched from the`entity`defined in the`@Relation`annotation (*Pet* ).`PetNameAndId`could also define its own relations all of which would also be fetched automatically.

If you would like to specify which columns are fetched from the child[Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity), you can use[projection()](https://developer.android.com/reference/android/arch/persistence/room/Relation#projection())property in the`Relation`annotation.  

```
 public class UserAndAllPets {
   @Embedded
   public User user;
   @Relation(parentColumn = "id", entityColumn = "userId", entity = Pet.class,
           projection = {"name"})
   public List<String> petNames;
 }
 
```

Note that`@Relation`annotation can be used only in Pojo classes, an[Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity)class cannot have relations. This is a design decision to avoid common pitfalls in[Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity)setups. You can read more about it in the main Room documentation. When loading data, you can simply work around this limitation by creating Pojo classes that extend the[Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity).

Note that the`@Relation`annotated field cannot be a constructor parameter, it must be public or have a public setter.

## Summary

|                                                                                                                          ### Public methods                                                                                                                           ||
|-------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` Class`    | ` `[entity](https://developer.android.com/reference/android/arch/persistence/room/Relation#entity())`() ` The entity to fetch the item from.                                                                                                             |
| ` String`   | ` `[entityColumn](https://developer.android.com/reference/android/arch/persistence/room/Relation#entityColumn())`() ` The field path to match in the[entity()](https://developer.android.com/reference/android/arch/persistence/room/Relation#entity()). |
| ` String`   | ` `[parentColumn](https://developer.android.com/reference/android/arch/persistence/room/Relation#parentColumn())`() ` Reference field in the parent Pojo.                                                                                                |
| ` String[]` | ` `[projection](https://developer.android.com/reference/android/arch/persistence/room/Relation#projection())`() ` If sub fields should be fetched from the entity, you can specify them using this field.                                                |

| ### 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

### entity

```
Class entity ()
```

The entity to fetch the item from. You don't need to set this if the entity matches the type argument in the return type.

<br />

|                                        Returns                                        ||
|---------|------------------------------------------------------------------------------|
| `Class` | The entity to fetch from. By default, inherited from the return type. <br /> |

### entityColumn

```
String entityColumn ()
```

The field path to match in the[entity()](https://developer.android.com/reference/android/arch/persistence/room/Relation#entity()). This value will be matched against the value defined in[parentColumn()](https://developer.android.com/reference/android/arch/persistence/room/Relation#parentColumn()).

<br />

|     Returns      ||
|----------|--------|
| `String` | <br /> |

### parentColumn

```
String parentColumn ()
```

Reference field in the parent Pojo.

If you would like to access to a sub item of a[Embedded](https://developer.android.com/reference/android/arch/persistence/room/Embedded)d field, you can use the`.`notation.

For instance, if you have a[Embedded](https://developer.android.com/reference/android/arch/persistence/room/Embedded)d field named`user`with a sub field`id`, you can reference it via`user.id`.

This value will be matched against the value defined in[entityColumn()](https://developer.android.com/reference/android/arch/persistence/room/Relation#entityColumn()).

<br />

|                          Returns                           ||
|----------|--------------------------------------------------|
| `String` | The field reference in the parent object. <br /> |

### projection

```
String[] projection ()
```

If sub fields should be fetched from the entity, you can specify them using this field.

By default, inferred from the the return type.

<br />

|                                                                              Returns                                                                               ||
|------------|--------------------------------------------------------------------------------------------------------------------------------------------------------|
| `String[]` | The list of columns to be selected from the[entity()](https://developer.android.com/reference/android/arch/persistence/room/Relation#entity()). <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)