英文:
How to persist JavaFX properties in SQLite using ORMLite?
问题
我正在使用JavaFX创建一个应用程序,并且我需要(这将非常酷)持久化JavaFX属性,比如SimpleStringProperty
或者SimpleObjectProperty
。
我已经看到了这个问题(现在已关闭),它使用了SimpleStringPropertyPersister
类。
但是,我实际上没有这个类。
我尝试使用SerializablePersister
类作为我的持久化类,并且ORMLite没有给我发送一个巨大的错误。但是我不确定所有的JavaFX属性(至少是我想要使用的属性)是否都是可序列化的,因为在javadoc中没有提到。
我正在使用maven中央仓库中的最新版本:
<dependency>
<groupId>com.j256.ormlite</groupId>
<artifactId>ormlite-jdbc</artifactId>
<version>5.1</version>
</dependency>
我该如何实现这一点?
英文:
I am using JavaFX to create an app and I need (it would be very cool) to persist JavaFX properties such as SimpleStringProperty
or SimpleObjectProperty
.
I've seen this issue (now closed) that make use of the SimpleStringPropertyPersister
class.
But, I don't actually have this class.
I've tried using the SerializablePersister
class as my persister class, and ORMLite didn't send me a huge error. But I'm not sure that all JavaFX properties (at least the ones that I want to use) are Serializable, as in the javadoc it is not.
I'm using the last version available at the maven central :
<dependency>
<groupId>com.j256.ormlite</groupId>
<artifactId>ormlite-jdbc</artifactId>
<version>5.1</version>
</dependency>
How can I achieve this ?
答案1
得分: 0
我正在使用JavaFX创建一个应用程序,并且我需要(这将非常酷)持久化JavaFX属性,例如SimpleStringProperty或SimpleObjectProperty。
在谷歌搜索中,第一个关于"ormlite自定义持久化器"的链接将我带到了自定义持久化器的文档页面。令人惊讶的是,我在ORMLite文档上花了很多时间,所以它们应该是有帮助的。还有一个自定义日期持久化器示例的源代码示例。
从文档中引用:
当从头开始定义数据持久化器类时,您可以扩展其他数据类型持久化器或BaseDataType类。您需要定义以下方法。
parseDefaultString
– 将默认字符串转换为适合加载到数据库中的SQL参数。resultToSqlArg
– 使用DatabaseResults对象从结果中获取正确的SQL参数类型。sqlArgToJava
– 将SQL参数转换为关联的Java类。
英文:
> I am using JavaFX to create an app and I need (it would be very cool) to persist JavaFX properties such as SimpleStringProperty or SimpleObjectProperty.
The first link in a search on google for "ormlite custom persister" brought me to the documentation for customer persisters. Shocking. I spent a lot of time on the ORMLite docs so they should be helpful. There also is source code example of a custom date persister.
To quote from the docs:
> When defining a data persister class from scratch, you could extend one of the other data type persisters or the BaseDataType class. You will need to define the following methods.
> * parseDefaultString
– Converts a default string into a SQL argument suitable to be loaded into the database.
> * resultToSqlArg
– Uses the DatabaseResults object to get the right SQL argument type out of the results.
> * sqlArgToJava
– Converts the SQL argument to the associated Java class.
专注分享java语言的经验与见解,让所有开发者获益!
评论