如何在对象框的框中多次添加相同的对象?
我有 ToMany 关系,如下所示,
@Entity()
class Pdf {
@Id()
int id;
Uint8List pdfData;
final String customerName;
@Property(type: PropertyType.date)
final DateTime dateTime;
@Backlink()
var products = ToMany<Product>();
double totalAmount;
PaymentStatus paymentStatus;
Pdf({
this.id = 0,
required this.pdfData,
required this.dateTime,
required this.customerName,
required this.totalAmount,
this.paymentStatus = PaymentStatus.unPaid,
});
int get status => paymentStatus.index;
set status(int value) {
paymentStatus = PaymentStatus.values[value];
}
}
@Entity()
class Product {
@Id()
int id;
String name;
String category;
int categoryId;
WeightType weightType;
double? price;
double weight;
bool isRefunded;
final pdf = ToOne<Pdf>();
Product({
this.id = 0,
required this.name,
required this.category,
required this.categoryId,
this.weightType = WeightType.kg,
this.price,
this.weight = 0,
this.isRefunded = false,
});
Product copy() => Product(
name: name,
category: category,
id: id,
price: price,
weight: weight,
weightType: weightType,
categoryId: categoryId,
isRefunded: isRefunded,
);
}
@override
int get hashCode => hashValues(
name.hashCode,
price.hashCode,
id.hashCode,
category.hashCode,
categoryId.hashCode,
weightType.hashCode,
weight.hashCode,
isRefunded.hashCode,
);
@override
bool operator ==(Object other) =>
other is Product &&
other.name == name &&
price == other.price &&
category == other.category &&
categoryId == other.categoryId &&
other.weight == weight &&
other.weightType == weightType &&
other.id == id;
}
这就是我将其放入框中的方式,
void add(){
final billPdf = Pdf(
pdfData: pdf,
dateTime: DateTime.now(),
customerName: bill.customerName,
totalAmount: bill.totalAmount,
paymentStatus: bill.paymentStatus,
);
billPdf.products.addAll(bill.products);
DBService.pdfBox.put(billPdf,mode: obj.PutMode.put);
}
现在当我尝试在 box
中添加相同的产品时,它会从列表(db)中删除以前的数据并且只添加最新的,因此它只是更新产品。如果我将 PutMode
更改为 insert
,则会出现异常。像这样,
object put failed
我为 kotlin 找到了这个, 无法在 Android (Kotlin) 中使用 ObjectBox 保存相同数据< /a>
由此,我什至覆盖了 hashcode
并输入了一些不同的值,但它仍然删除了以前的值,而且在我的用例中,一切都可以相同。
那么如何将相同的对象放入对象框中呢?
I have ToMany relation which looks like this,
@Entity()
class Pdf {
@Id()
int id;
Uint8List pdfData;
final String customerName;
@Property(type: PropertyType.date)
final DateTime dateTime;
@Backlink()
var products = ToMany<Product>();
double totalAmount;
PaymentStatus paymentStatus;
Pdf({
this.id = 0,
required this.pdfData,
required this.dateTime,
required this.customerName,
required this.totalAmount,
this.paymentStatus = PaymentStatus.unPaid,
});
int get status => paymentStatus.index;
set status(int value) {
paymentStatus = PaymentStatus.values[value];
}
}
@Entity()
class Product {
@Id()
int id;
String name;
String category;
int categoryId;
WeightType weightType;
double? price;
double weight;
bool isRefunded;
final pdf = ToOne<Pdf>();
Product({
this.id = 0,
required this.name,
required this.category,
required this.categoryId,
this.weightType = WeightType.kg,
this.price,
this.weight = 0,
this.isRefunded = false,
});
Product copy() => Product(
name: name,
category: category,
id: id,
price: price,
weight: weight,
weightType: weightType,
categoryId: categoryId,
isRefunded: isRefunded,
);
}
@override
int get hashCode => hashValues(
name.hashCode,
price.hashCode,
id.hashCode,
category.hashCode,
categoryId.hashCode,
weightType.hashCode,
weight.hashCode,
isRefunded.hashCode,
);
@override
bool operator ==(Object other) =>
other is Product &&
other.name == name &&
price == other.price &&
category == other.category &&
categoryId == other.categoryId &&
other.weight == weight &&
other.weightType == weightType &&
other.id == id;
}
this is how I'm putting it in the box,
void add(){
final billPdf = Pdf(
pdfData: pdf,
dateTime: DateTime.now(),
customerName: bill.customerName,
totalAmount: bill.totalAmount,
paymentStatus: bill.paymentStatus,
);
billPdf.products.addAll(bill.products);
DBService.pdfBox.put(billPdf,mode: obj.PutMode.put);
}
now when I try add same product in the box
, it removes the previous data from the list(db) and only adds the latest one so it is just updating the product. And if I change the PutMode
to insert
, it gives me exception. like this,
object put failed
I found this for kotlin,
Can't save same data using ObjectBox with Android (Kotlin)
From this I even overrided the hashcode
and put some different value but it still removes previous values and also in my use case everything can be same.
So How can I put same objects in object-box?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是我不需要在产品模型中添加
final pdf = ToOne();
并从Pdf 中删除
@Backlink()
模型。它将数据放入不同的列表中,并且不会从以前的列表中删除。如果您放置重复的对象,它只会更新它。虽然我仍然有一个问题,它只存储默认值,这意味着某些值可能为 null,但我认为这个问题与问题无关,所以没关系,将上面作为答案。
The problem was that I didn't need to add
final pdf = ToOne<Pdf>();
in product's Model and remove the@Backlink()
fromPdf
model. And it put data in different list and doesn't remove from previous list. And if you put duplicate object it just updates it.Although I still have one problem it only stores default values which means some values might be
null
but I think this problem is not related to question so it's fine and putting above as answer.