user1436773
user1436773

Reputation: 23

How to acces nested model's attributes in Play 2 templates

I've got those two models in a Play 2 project :

@Entity
public class Site extends Model{

    @Id
    public String adId;

    @Required
    public String name;

    @Required
    @OneToMany (cascade = CascadeType.ALL)
    public List<Room> rooms;
...
}//end of class

and

@Entity
public class Room extends Model {

    @Id
    @Required
    public String adId;

    @Required 
    public String name;

    public List<Reservation> reservations = new ArrayList<Reservation>();

...
}

When I use this code in my view.scala.html

            var treeData = [                            
                    @for(site <- sites) {
                        {title: "@site.name", tooltip: "@site.adId",
                            children: [
                                @for(room <- site.rooms) {
                                    {title: "@room.name", tooltip: "@room.adId"},
                                }
                            ]
                        },
                    }
                ];

I can't acces site.rooms If I test it on the server side, the data exist.

Another strange thing is that I do get site.adId, but site.name stays empty...

If anyone knows what I'm doing wrong...

(Sorry for the poor English, it's not my mother language).

Upvotes: 2

Views: 704

Answers (2)

Joerg Viola
Joerg Viola

Reputation: 246

Or fetch the data during the query: finder.fetch("rooms")query().findList()

Upvotes: 0

msung
msung

Reputation: 3742

You need getters for the data to be accessible in the templates / by scala. At least that what fixed it for me. I think I found that information on the mailinglist:

This issue has been discussed before. You must use private field and public accessor methods if you want to use Ebean lazy loading (or any other Java magic) from Scala code.

Upvotes: 2

Related Questions