如何多次在对象框的框中添加相同的对象?
how to add same object in objectbox's box multiple times?
我有如下所示的 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 找到了这个,
据此,我什至覆盖了 hashcode
并输入了一些不同的值,但它仍然删除了以前的值,而且在我的用例中,一切都可以相同。
那么如何将相同的对象放入对象框中?
问题是我不需要在产品模型中添加 final pdf = ToOne<Pdf>();
并从 Pdf
模型中删除 @Backlink()
。它将数据放在不同的列表中,并且不会从以前的列表中删除。如果您放置重复的对象,它只会更新它。
虽然我仍然有一个问题它只存储默认值这意味着一些值可能是 null
但我认为这个问题与问题无关所以它很好并把上面作为答案。
我有如下所示的 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 找到了这个,
据此,我什至覆盖了 hashcode
并输入了一些不同的值,但它仍然删除了以前的值,而且在我的用例中,一切都可以相同。
那么如何将相同的对象放入对象框中?
问题是我不需要在产品模型中添加 final pdf = ToOne<Pdf>();
并从 Pdf
模型中删除 @Backlink()
。它将数据放在不同的列表中,并且不会从以前的列表中删除。如果您放置重复的对象,它只会更新它。
虽然我仍然有一个问题它只存储默认值这意味着一些值可能是 null
但我认为这个问题与问题无关所以它很好并把上面作为答案。