Scala 的 @BeanProperty 用于选项

发布于 2025-01-05 18:22:11 字数 358 浏览 6 评论 0原文

是否可以为可选变量实现 BeanProperty ?与 JPA 配合使用会很有用。

那就太好了:

@BeanProperty var status: Option[String]

如果将以下方法添加到类中,

def setStatus(s: String) { status = Some(s) }
def getStatus: String = status.get

Is it possible to implement BeanProperty for Optional variables? It would be useful with JPA.

It would be great if:

@BeanProperty var status: Option[String]

would add the following methods to the class:

def setStatus(s: String) { status = Some(s) }
def getStatus: String = status.get

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

冬天旳寂寞 2025-01-12 18:22:11

不幸的是不是,但有一个简单的解决方法可以两全其美:

@BeanProperty
var status: String

def statusOption = Option(status)

请注意,JPA 不理解 Option[T]。顺便说一句,如果您使用字段访问而不是 JPA 中的 getter/setter 访问,则甚至不需要@BeanProperty - JPA 提供程序将扫描 Java 字段。

Unfortunately not, but there is a simple workaround that brings best of both worlds:

@BeanProperty
var status: String

def statusOption = Option(status)

Note that JPA does not understand Option[T]. BTW if you use field-access as opposed to getter/setter access in JPA, @BeanProperty isn't even needed - the JPA provider will scan Java fields instead.

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