播放2.0 ManyToMany和ManyToOne

时间:2012-05-20 17:49:47

标签: java playframework

我尝试保存并与play webframework 2.0显示多对一的关系......

我有这些模特:

@Entity
public class Project extends Model{

    @Id
    public Long id;

    @Required
    public String name; 

    @ManyToMany
    public List<Worker> workers = 
          new ArrayList<Worker>();

    @ManyToOne
    public Worker head;
...

}


@Entity
public class Worker extends Model{  
    @Id
    public Long id;

    @Required
    public String name;

   ...

    public static Map<String,String> options() {
        LinkedHashMap<String,String> options = new LinkedHashMap<String,String>();
        for(Worker w: Worker.getAll()) {
            options.put(w.id.toString(), w.name);
        }
        return options;
    }       

}

我有这个观点:

@(projects: List[Project], taskForm: Form[Project])

@import helper._


    <h1>@projects.size() Projects(s)</h1>

    <ul>
        @for(project <- projects) {
            <li>
                @project.name
                <b> - head: </b>
                @project.head.name

                <ul>
                @for(worker <- project.workers) {
                    <li>
                        @worker.name
                    </li>
                }
                </ul>

                @form(routes.ProjectController.deleteProject(project.id)) {
                    <input type="submit" value="Delete">
                }

            </li>
        }
    </ul>

    <h2>Add a new task</h2>

    @form(routes.ProjectController.newProject()) {        

        @inputText(taskForm("name"))

        @inputText(taskForm("description"))               

        @select(
            taskForm("head.id"), 
            options(Worker.options), 
            '_label -> "Worker"         
        )       


        <input type="submit" value="Create">

    }   

但是当我保存项目时,我没有相关的工人 而且我没有找到任何多个例子.. 任何人都可以告诉我如何管理游戏中的关系2.0或给我一个解释它的链接?

感谢所有回复

1 个答案:

答案 0 :(得分:0)

我使用getAll方法获取所有项目并将它们传递给视图... 我看到我需要做“获取('工人')”让工人附加到项目......

我在控制器中的getAll之后错过了获取方法...问题解决了......