tags:

views:

87

answers:

2

I have a DSL Java object, i.e. a POJO which returns this in setters plus the getters/setters have an unusual naming pattern:

public class Demo {
    private long id;
    private String name;
    private Date created;

    public Demo id (long value) { id = value; return this; }
    public String id () { return id; }
    public Demo name (String value) { name = value; return this; }
    public String name () { return name; }
    public Demo created (Date value) { created = value; return this; }
    public Date created () { 
        if (created == null) created = new Date ();

        return created;
    }

}

Is it possible to tell JPA to use "name(String)" and "name()" as the setter/getter method?

[EDIT] My issue is the created field above. For this field, I want JPA to use the "getter" created() so the field will always be non-NULL.

Or is there a way to tell JPA to use CURRENT TIMESTAMP when creating a new object with created == null?

A: 

According to the JPA spec (see JSR-220) chapter 2.1.1 you can tell JPA to use field access instead of property access by annotating the fields for mapping information and not the getter methods.

I don't think you can tell JPA which naming convention to use for getters and setters since it's a basic java beans concept.

rudolfson
Oh, forgot to mention you have to use annotations. Don't know if it works with xml mapping. :-/
rudolfson
And if I don't want to use the property access?
Aaron Digulla
That's what I mentioned. Put the annotations before the fields. Now after you edited your post the problem remain, that "created" could be null. I think you could use the attribute "columDefinition" of the @Column annotation to define a default value for this column (see chapter 9.1.5 of the mentioned spec).
rudolfson
A: 

Could you not simply intialise created when you define in the class, and then use field access.

private Date created = new Date();
toolkit
Because in my case, new Date() is an expensive operation :) I really want to do this lazily.
Aaron Digulla