Todd
Todd

Reputation: 31

id not automatically available in PlayFramework models?

When I try to access a model in the Application class or in a model it throws the following error. I can create the model and persist it. But if I try and access it, problemo.

Execution exception

[InvalidPropertyException: Invalid property 'id' of bean class [models.CandidateToSalesforce]: No property 'id' found]
In [...]/app/views/syncedRecords.scala.html at line 11.

7   <h3>Results</h3>
8   <ul>
9       @for(r <- records) {
10          <li>
11              @r    [[ this is the line]]
12          </li>
13      }
14  </ul>
15}

This can be fixed by adding the getter as shown below.

@Id
public Long id;

// NOTE: play framework was bugging out without this method even though it's supposed to be automatic
public Long getId() {
    return id;
}

Not a huge deal but is this not wrong? The docs say "Play has been designed to generate getter/setter automatically, to ensure compatibility with libraries that expect them to be available at runtime"

Do I have a known config problem that's going to bite me in other ways?

Upvotes: 2

Views: 1043

Answers (1)

biesior
biesior

Reputation: 55798

You can write your own toString() method in the model:

public String toString(){
     return this.id.toString();
}

although it's probably better idea to return real some string instead of ie.

return this.name+" "+this.somethingElse;

Then if you'll use this is my record: @r in template it will render the toString() return.

You can fetch your id w/out writing custom getter with: @r.id (or r.id in the controller)

Upvotes: 2

Related Questions