[2.0] Null value in any field that is not the primary key (@Id), refer to sample project zentasks

232 views
Skip to first unread message

Priyo

unread,
Mar 21, 2012, 1:09:23 PM3/21/12
to play-fr...@googlegroups.com
Hi guys,

I am playing around with Play and have noticed a behaviour that occurs in my application and the sample zentasks, which leads me to believe this is more than a mistake by myself. In zentasks you have the item.scala.html, there is this line of code.

<span class="assignedTo">@task.assignedTo.emal</span>

The tasks object encapsulates a User object which is called assignedTo. In that particular line of code, the email field which is of type @Id (primary key) is returned normally as you would expect. However when i want to retrieve a different field for example, name which is not a primary key, the value returned is null. Now i checked using Java in the controller class if the same is true, however using the Logger, i noticed the returned value is a String with the correct name associated with that user and the email. My question is why can't i access the name field, the reason why i have brought the primary key to your attention is because the task object has another object encapsulated within it, the project object. The same is true for that object too. I can only access the primary key but not any other field.

Kind Regards,
Priyo

Guillaume Bort

unread,
Mar 21, 2012, 3:40:50 PM3/21/12
to play-fr...@googlegroups.com
This issue has been discussed before. It's because the other fields were not fetched during the query. Then Ebean (like hibernate and other magic libraries) use bytecode manipulation to issue a select query in field access. 

Unfortunately this magic is not available when you access a direct field from within scala (because the scala code itself would be modified)

So there are 2 options:

- Rely on magic and use plain old getter and setter to enable it. 
- Don't use lazy loading and fetch everything during the query. 

For my point of view, having lazy loading not working from template is a feature. It avoid to have the template rendering accidentely issuing tens of db queries as it is often the case in java applications. 
--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/g-bmyMxTljIJ.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Priyo

unread,
Mar 21, 2012, 3:47:50 PM3/21/12
to play-fr...@googlegroups.com
Thank you Gullaume,

I didn't quite know what to search for apart from the symptom itself, hence i had to resort to a posting. Thank you for clearing this up.

Regards.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages