I want to implement android ROOM to my project. But I can not solve one-to-one relation between Order(customer_id) and Customer(id).
Is it possible to fill customer (@Ignore protected Customer customer) field in OrderDao interface?
I can solve only java codes. Is there any other methods?
// Customer.java
@Entity (tableName = "t_customer")
public class Customer {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
protected String mId;
@ColumnInfo(name = "name")
protected String mName;
}
// Order.java
@Entity(tableName = "t_order",
foreignKeys = {
@ForeignKey(entity = Customer.class,
parentColumns = "id",
childColumns = "customer_id",
onDelete = SET_NULL) }
public class Order {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
protected String mId;
@ColumnInfo(name = "description")
protected String mDescription;
@ColumnInfo(name = "customer_id")
protected String mCustomerId;
@Ignore
**protected Customer customer;**
//....
}
//OrderDao.java
@Dao
public interface OrderDao {
@Query("SELECT * from t_order ORDER BY id DESC")
LiveData<List<Order>> listByIds();
@Query("SELECT * from t_order WHERE id = :id LIMIT 1")
LiveData<Order> readById(long id);
//....
}
As of right now there is no way to fill Customer automatically. You'll need to create another POJO named OrderWithCustomer and it will probably looks as shown below:
public class OrderWithCustomer {
@Embedded
private Order order;
@ColumnInfo(name = "name")
private String customerName;
}
And in your DAO:
@Dao
interface OrderDao {
@Query("SELECT t_order.* name FROM t_order LEFT JOIN t_customer ON t_order.customer_id = t_customer.id")
public List<OrderWithCustomer> getOrderWithCustomer();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With