Db4o 对象更新
我将 db4o 用于一个简单的应用程序,带有嵌入式数据库。当我保存一个对象,然后更改该对象时,是否假设 db4o 返回更改后的对象?
代码如下:
[Test]
public void NonReferenceTest()
{
Aim localAim = new Aim("local description", null);
dao.Save(localAim);
// changing the local value should not alter what we put into the dao
localAim.Description = "changed the description";
IQueryable<Aim> aims = dao.FindAll();
var localAim2 = new Aim("local description", null);
Assert.AreEqual(localAim2, aims.First());
}
测试失败。我需要以任何特殊方式设置 db4o 容器吗?将其包装在提交调用中?谢谢
I'm using db4o for a simple app, with an embedded db. When I save an object, and then change the object, is it suppose that db4o returns the changed object?
Here's the code:
[Test]
public void NonReferenceTest()
{
Aim localAim = new Aim("local description", null);
dao.Save(localAim);
// changing the local value should not alter what we put into the dao
localAim.Description = "changed the description";
IQueryable<Aim> aims = dao.FindAll();
var localAim2 = new Aim("local description", null);
Assert.AreEqual(localAim2, aims.First());
}
The test fails. Do I need to setup the db4o container in any special way? wrap it in commit calls? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,它应该是这样工作的。您必须记住,您操作的是对象而不仅仅是数据。
当将对象存储到对象数据库(或从对象数据库查询)时,它会保留存储的数据与内存中的对象之间的链接。当您更新对象并将其存储在数据库中时需要这样做。您实际上不希望存储新对象,但希望更新旧对象。
因此,当检索内存中仍然存在的对象时,您将获得对该对象的引用。
另一个原因是数据完整性。再次查看您的代码,想象它为您提供数据库的数据,而不是对更新对象的引用:
假设 A (localAim2 != localAim) 的问题是您处理存储在数据库中的同一对象有2个不同的内容。
如果没有假设 A(即 localAim2 == localAim),您的数据始终是一致的,因为您只有一个对象(引用两次)。
Actually, it is supposed to work that way. You have to keep in mind that you are manipulating objects not only data.
When storing (or querying) an object to (or from) the object-database, it keeps the link between the stored data and the object in memory. This is needed when you update the object and store it in the database. You actually do not want that a new object is stored but you want the old object to be updated.
So, when retrieving an object that still exists in memory, you will be given a reference to that object.
Another reason is data integrity. Look at your code again, and imagine it gives you the data of the database and not a reference to the updated object:
The problem with Assuption A (localAim2 != localAim) is that you work on the same object that is stored in the database with 2 different contents.
Without Assuption A (i.e., localAim2 == localAim), your data is allways consistent since you have only one object (referenced two times).