How to get full info of non-owing side of relationship after ORM in views in play framework?

I had two classes: Author and Book, defined relations between both, right JPA annotations. I use MVC architecture in Play Framework. In controller class (Application) ORM works, I have complete info about authors (idA and nameA), but in views, field nameA looks like empty string (idA is correct).

Below I'll explain what I mean:

in Author:

public class Autor extends Model {

    @Id
    public Long idA;

    @Required
    public String nameA;

    @ManyToMany(mappedBy = "authors")
    public Set<Book> books;
    }

and in Books:

public class Book extends Model {

    @Id
    public Long idB
    @ManyToMany
    @JoinTable(
              name = "creates",
              joinColumns = @JoinColumn(name = "id_b", referencedColumnName = "id_b"),
              inverseJoinColumns = @JoinColumn(name = "id_a", referencedColumnName = "id_a")
          )
    public Set<Autor> authors;
    }

    public static Finder<Long, Book> find = new Finder<Long, Book>(Long.class, Book.class);

    public static List<Book> all()
    {
    return find.all();
    }

I tried to get book with all authors as effect of GET request. I've added to routes file line:

GET     /books/                 controllers.Application.booksShow()

and created methode in Application class:

public static Result booksShow(Long)
  {
      return  ok(views.html.booksInfo.render(Book.all()));
  }

snippet in views in Scala looks like this:

    @for(book <- Books) {
        <li> 
            @book.title
            @book.authors.size()
            @for(author <- book.authors) {
                    <li>@author.nameA</li>
                }
        </li>
    }

After get request in browser I get something like this:

Title1 2
[empty line]
[empty line]
Title2 1
[empty line]

but It should be:

Title1 2
Author1
Author2
Title2 1
Author2

I tried to change booksShow method for:

  public static Result booksShow(Long id)
  {
      List<Books> b = Books.all();
      System.out.println(b.get(0).title);
      System.out.println(b.get(0).authors.iterator().next().nameA);
      return  ok(views.html.booksInfo.render(Ksiazka.all()));
  }

And In my console appeared:

Title1
 Author1

But on website, Authors are still empty;

When I changed in Scala:

@for(author <- book.authors) {
    <li>@author.nameA</li>
}

for:

@for(author <- book.authors) {
    <li>@author.idA</li>
}

I get right (nonzero, nonblank) ID's.

Any idea? I'm confused, I get idA's and size of Authors collection, but nameA's is blank.

Answers


You have some options for solving this. You could set your relationships to FetchType.EAGER in order to populate all your relationships. That's the easy way. Could end up in performance problems, but will do it for the moment.

For the future, think about what information is needed when and populate the needed fields before your session gets closed by just doing a "get"on them. This will trigger lazy loading and populate the fields for later access.


I found another way to resolve my problem (not exactly resolve, but I'm getting result I want):

Instead of use find.all(), I pass to view result of query with join:

in Book:

public static List<Books> allWithAuthors()
{
    return find.fetch("authors").findList();
}

and in Application:

public static Result booksShow()
{
    return  ok(views.html.booksInfo.render(Book.allWithAuthors()));
}

Need Your Help

how to get all the csv file name under a directory?

php

supposed there is a folder named example, and in it there are some csv file eg(a.csv, b.csv....).

Overlapped tiles for custom image map type in Google Maps

google-maps-api-3

I am implementing a prototype of an online game, whose map tiles are hexagons. I have the PNG files of tiles (which are rectangles of course), can I allow overlap in Google Map so that the tiles ca...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.