具有多列的 ORMLite 自定义数据持久化器
假设我有一个名为 Person
的类,它通常使用 ORMLite 保存到数据库表中。
现在,我在 Person
类中有一个名为 House
的成员。 House
类包含 3 个属性,并且永远不会存储到数据库中。
我想要的是,每当 Person
被持久化时,我都想将 House
的 3 个字段存储到 Person
表中的 3 个不同的列中。
因此,Person 表将包含:{ person_name, person_contact, house_address, house_type, house_date }
。
最后 3 个字段将来自 House
对象。
我认为我应该在 House
成员变量上使用 DataPersister
,但这是否意味着它将整个 House
对象写入一列?我想将其分成 Person
表内的 3 列。有人可以帮忙吗?
谢谢!
Say I have a class called Person
which is normally persisted into a db table using ORMLite.
Now, I have a member inside that Person
class called House
. The House
class contains 3 properties and is NEVER stored into the database.
What I want is whenever Person
is persisted I want to store the 3 fields of House
into the Person
table into 3 different columns.
So the Person table will have : { person_name, person_contact, house_address, house_type, house_date }
.
The last 3 fields will come in from House
object.
I think I should be using a DataPersister
on the House
member variable but does this mean it will write the entire House
object into ONE column? I want to split it into 3 columns inside the Person
table. Can someone help?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,可序列化对象的完美解决方法可能是:
因此,Person 像通常的原始变量一样存储在单个列中。
Actually a perfect workaround for Serializable objects this could be:
As a result Person stored in a single column as usual primitive variable.
您所要求的称为嵌入式对象。子对象中的所有字段都存储在父对象中。
抱歉,但现在 (12/2012) ORMLite 不支持嵌入对象。它在 TODO 列表中,但没有计划阻碍它。正如@user999717提到的,我们确实支持可以自动刷新的外来对象。这允许您将
House
对象存储在另一个表中,并且当您提取Person
时,ORMLite 将查询它。这是它的文档:What you are asking for is called embedded objects. All of the fields in the sub-object are stored in the parent object.
Sorry, but right now (12/2012) ORMLite does not support embedded objects. It is on the TODO list but no plans are in the way for it. As @user999717 mentioned, we do support foreign objects that can be automatically refreshed. This allows you to store the
House
object in another table and ORMLite will query for it when you pull out aPerson
. Here are the docs for it:您可以使用这个:
太糟糕了,以下不起作用,您需要额外的虚拟字段:
您也可以使用 dataType = DataType.SERIALIZABLE,如其他答案中所述,但使用自定义序列化,这意味着实际实现 Serialized 并处理所有可能的序列化阅读时的版本。
不应该使用默认序列化,因为你最终会得到混乱的数据库。
You could use this:
Too bad the following doesn't work and you need the extra dummy field:
You could also use dataType = DataType.SERIALIZABLE as stated in other answers BUT using custom serialization, which means actually implementing Serializable and handle all possible serialization versions when reading.
Default serialization should NOT be used as u'll end up with messed up DB.