Skip to content

One to one relationship

Ravi Teja Gudapati edited this page Feb 19, 2019 · 8 revisions
class User {
  @PrimaryKey(length: 50)
  String id;

  @Column(length: 50)
  String name;

  @HasOne(AddressBean)
  Address address;

  User({this.id, this.name, this.address});
}
class Address {
  @PrimaryKey(length: 50)
  String id;

  @Column(length: 150)
  String street;

  @BelongsTo(UserBean)
  String userId;

  Address({this.id, this.street, this.userId});

  String toString() => "Address($id, $userId, $street)";

  bool operator ==(final other) {
    if (other is Address)
      return id == other.id && street == other.street && userId == other.userId;
    return false;
  }
}
@GenBean()
class UserBean extends Bean<User> with _UserBean {
  UserBean(Adapter adapter)
      : addressBean = AddressBean(adapter),
        super(adapter);

  final AddressBean addressBean;

  String get tableName => 'oto_simple_user';
}

@GenBean()
class AddressBean extends Bean<Address> with _AddressBean {
  UserBean _userBean;
  UserBean get userBean => _userBean ??= UserBean(adapter);

  AddressBean(Adapter adapter) : super(adapter);

  String get tableName => 'oto_simple_address';
}
Clone this wiki locally