Làm cách nào để triển khai nhiều quan hệ với nhiều thư viện Android Room Persistence Library?Làm cách nào để thực hiện quan hệ nhiều với nhiều thư viện Android Room Persistence?
Một người dùng có thể có một hoặc nhiều thiết bị & Một thiết bị có thể được sở hữu bởi một hoặc nhiều người dùng.
@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
}
Mã của bạn dường như thực hiện quan hệ M: N, với 'UserDevice' biểu thị mối quan hệ. – CommonsWare