如何使用 Android Room Persistence Library 实现多对多关系?
一位用户可能拥有一台或多台设备,而一台设备可能由一位或多位用户拥有。
@Entity
public class User {
public @PrimaryKey Long id;
public String userName;
}
@Dao
public interface UserDao {
@Query("select * from user") List<User> getAllUsers();
@Query("select * from user where id = :id")
User getUserById(long id);
}
@Entity
public class Device {
public @PrimaryKey Long id;
public String name;
}
@Dao
public interface DeviceDao {
@Query("select * from device")
List<Device> getAllDevices();
}
@Entity
public class UserDevice {
public String userId;
public String deviceId;
}
@Dao
public interface UserDeviceDao {
// List all devices by userId
// List all users by deviceId
}