如何停止为 BlazeDS 和 Java 编写 Java 属性设置器我不想要 JPA?

发布于 2024-10-06 11:19:24 字数 323 浏览 1 评论 0原文

BlazeDS 不会序列化属性,除非它同时具有 getter 和 setter。但是,我的许多 Java 属性都是只读的。因此,我现在必须添加设置器来支持解组过程。如果这些域对象的任何用户开始自己调用这些设置器,则会破坏这些事物的值对象语义,并可能导致各种系统问题。

过去我不得不做很多这样的事情来支持 JPA 的某些方面,但我从来不喜欢它。这是因为我们将 JPA 注释放在属性上而不是私有字段上(以避免另一个问题)。

除了使用Javadoc来警告自己和他人之外,程序员还能做什么?

编辑:我应该补充一点,这些额外的设置器不是这些对象实现的公共接口的一部分......但它们仍然存在。

BlazeDS will not serialize a property unless it has both a getter and a setter. However, many of my Java properties are read-only. Therefore I am now having to add setters to support the Unmarshalling process. If any of the users of these domain objects start calling these setters themselves it'll break the value-object semantics of these things and likely cause all sorts of system problems.

I've had to do this a lot on the past to support certain aspects of JPA and never liked it. This was because we put our JPA annotations on the properties rather than the private fields (to avoid another problem).

Other than use Javadoc to warn myself and others, what's a programmer to do?

Edit: I should add that these extra setters are NOT part of the public interface these objects implement....but they are still there nonetheless.

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

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

发布评论

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

评论(2

鹿港小镇 2024-10-13 11:19:24

您可以尝试使用 @Access 注释。这用于逐列覆盖用于该类的访问类型。它还可以让您在访问数据库时搞乱内容,如下所示:

private String firstName;

@Access(AccessType.PROPERTY) 
@Column(name="FIRST_NAME")
protected String getFirstNameForDatabase() {
    return "Mr. " + this.firstName;
}

此示例不仅覆盖用于该类的“FIELD”访问权限,而且还导致数据库在值上添加前缀“Mr.”每次。这还可以允许您声明满足您其他要求的“虚拟”getter/setter,而不会搞乱 JPA 提交和检索。尝试一下,看看是否可以用来为您的问题制定解决方案。

You could try using the @Access annotation. This is used to override the access type being used for the class on a column-by-column basis. It also lets you mess with stuff when it goes to the database, like this:

private String firstName;

@Access(AccessType.PROPERTY) 
@Column(name="FIRST_NAME")
protected String getFirstNameForDatabase() {
    return "Mr. " + this.firstName;
}

This example not only overrides the 'FIELD' access being used for the class, but it also causes the database to commit with "Mr. " prefixed on the value each time. This may also allow you to declare 'dummy' getters/setters that satisfy your other requirements without screwing up the JPA commits and retrieves. Try it out and see if this can be used to craft a solution for your issue.

像极了他 2024-10-13 11:19:24

您有多种选择:使用您自己的序列化机制或使用 BlazeDS 版本 4。我写了一篇与此相关的小文章,也许它可以帮助您。链接是 http://cornelcreanga.com/2009/09 /blazeds-amf-and-read-only-properties/

You have several options: use your own serialization mechanism or use BlazeDS version 4. I wrote a small article related to that, maybe it can help you. The link is http://cornelcreanga.com/2009/09/blazeds-amf-and-read-only-properties/.

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