开发者

How to stop writing Java property setters for BlazeDS & JPA that I don't want?

开发者 https://www.devze.com 2023-01-28 23:03 出处:网络
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 p

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 objec开发者_StackOverflow中文版ts implement....but they are still there nonetheless.


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.


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/.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号