具有多列的 ORMLite 自定义数据持久化器

发布于 2024-12-11 12:23:14 字数 599 浏览 0 评论 0原文

假设我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安静 2024-12-18 12:23:14

实际上,可序列化对象的完美解决方法可能是:

@DatabaseField (dataType = DataType.SERIALIZABLE) 
public Person person;

因此,Person 像通常的原始变量一样存储在单个列中。

Actually a perfect workaround for Serializable objects this could be:

@DatabaseField (dataType = DataType.SERIALIZABLE) 
public Person person;

As a result Person stored in a single column as usual primitive variable.

冰葑 2024-12-18 12:23:14

您所要求的称为嵌入式对象。子对象中的所有字段都存储在父对象中。

抱歉,但现在 (12/2012) ORMLite 不支持嵌入对象。它在 TODO 列表中,但没有计划阻碍它。正如@user999717提到的,我们确实支持可以自动刷新的外来对象。这允许您将 House 对象存储在另一个表中,并且当您提取 Person 时,ORMLite 将查询它。这是它的文档:

http://ormlite.com/docs/foreign

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 a Person. Here are the docs for it:

http://ormlite.com/docs/foreign

揽月 2024-12-18 12:23:14

您可以使用这个:

@DatabaseField (useGetSet = true)
private String personStr; // Dummy

public Person person;
public String getPersonStr(){ person.toString(); }
public void setPersonStr(String personstr){ person = Person(personstr); }

太糟糕了,以下不起作用,您需要额外的虚拟字段:

@DatabaseField (dataType = DataType.String, useGetSet = true) 
public Person person;
public String getPerson(){ person.toString(); }
public void setPerson(String personstr){ person = Person(personstr); }

您也可以使用 dataType = DataType.SERIALIZABLE,如其他答案中所述,但使用自定义序列化,这意味着实际实现 Serialized 并处理所有可能的序列化阅读时的版本。
不应该使用默认序列化,因为你最终会得到混乱的数据库。

You could use this:

@DatabaseField (useGetSet = true)
private String personStr; // Dummy

public Person person;
public String getPersonStr(){ person.toString(); }
public void setPersonStr(String personstr){ person = Person(personstr); }

Too bad the following doesn't work and you need the extra dummy field:

@DatabaseField (dataType = DataType.String, useGetSet = true) 
public Person person;
public String getPerson(){ person.toString(); }
public void setPerson(String personstr){ person = Person(personstr); }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文