Thanks a lot for looking into this, I really, really do appreciate this.
I prepared an example to clarify what I want to achieve.
There are departments which have employees.
In the edit page for a department I'd like to have a multi-select for the employees
that work in the department.
(A selectManyShuffle form trinidad ultimately, for now I am testing with a standard
selectManyListbox.)
I started with the seampay example and made the following additions.
Then directly opened
http://127.0.0.1:8080/seam-pay/departments.seam
Currently edits never seem to make it to the DB.
My "hack" that manually sets the parent's id at the child entities when the
list setter is called is no good either, because it does not remove deselected entities
from the selection.
I played with different cascade options, but that also didn't make any difference.
Any idea what I need to change to make this work?
Added to import.sql
insert into Department(id, name) values(1, 'Accounting');
| insert into Department(id, name) values(2, 'Marketing');
| insert into Department(id, name) values(3, 'R&D');
|
| insert into Employee(id, department_id, name) values(1, 1, 'Bean Counter');
| insert into Employee(id, department_id, name) values(2, 1, 'Mister Money');
| insert into Employee(id, department_id, name) values(3, 1, 'Calc U. Lator');
|
| insert into Employee(id, department_id, name) values(4, 2, 'Power Pointy');
|
| insert into Employee(id, department_id, name) values(5, 3, 'Heinz Hacker');
| insert into Employee(id, department_id, name) values(6, 3, 'Sam Seam');
|
Added to components.xml
<framework:entity-query name="departments"
| ejbql="from Department"
| order="name"
| max-results="20"
| entity-manager="#{entityManager}" />
|
| <framework:entity-query name="employees"
| ejbql="from Employee"
| order="name"
| max-results="20"
| entity-manager="#{entityManager}" />
|
| <framework:entity-home name="departmentHome"
|
entity-class="org.jboss.seam.example.seampay.Department"
| entity-manager="#{entityManager}" />
|
| <factory name="selectedDepartment"
value="#{departmentHome.instance}"/>
|
New: departments.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
| <html
xmlns="http://www.w3.org/1999/xhtml"
|
xmlns:ui="http://java.sun.com/jsf/facelets"
|
xmlns:h="http://java.sun.com/jsf/html"
|
xmlns:f="http://java.sun.com/jsf/core"
|
xmlns:s="http://jboss.com/products/seam/taglib"
| template="template.xhtml">
| <head>
| <meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
| <title>Seam Pay</title>
| <link href="screen.css" rel="stylesheet"
type="text/css" />
| <link href="date.css" rel="stylesheet"
type="text/css" />
| </head>
| <body>
|
| <div class="body">
| <h1>Departments</h1>
|
| <h:messages styleClass="message"/>
|
|
| <!-- search results -->
| <table class="results">
| <tr>
| <th>Id</th>
| <th>Name</th>
| </tr>
| <ui:repeat value="#{departments.resultList}"
var="department">
| <tr>
| <td>
| <s:link view="/departments.xhtml"
value="#{department.id}">
| <f:param name="departmentId"
value="#{department.id}"/>
| </s:link>
| </td>
| <td>#{department.name}</td>
| </tr>
| </ui:repeat>
| </table>
| <h:outputText value="No departments found" rendered="#{empty
departments.resultList}" styleClass="message"/>
|
|
| <f:subview id="departmentDetails"
rendered="#{departmentHome.idDefined}">
| <h2>Department Details</h2>
|
| <h:form>
| <s:validateAll>
| <table>
| <tr>
| <td>Name:</td>
| <td>
| <h:inputText id="name"
value="#{selectedDepartment.name}" required="true"/>
| </td>
| </tr>
| </table>
|
| <h:selectManyListbox
value="#{selectedDepartment.employees}" size="8">
| <s:selectItems value="#{employees.resultList}"
var="employee" label="#{employee.name}" />
| <s:convertEntity />
| </h:selectManyListbox>
| </s:validateAll>
|
| <h:commandButton value="Update"
action="#{departmentHome.update}" />
| </h:form>
| </f:subview>
|
| </div>
|
| </body>
| </html>
|
New: Department.java
package org.jboss.seam.example.seampay;
|
| import javax.persistence.*;
| import java.io.Serializable;
| import java.util.List;
|
| @Entity
| public class Department implements Serializable {
| @Id @GeneratedValue
| private Long id;
|
| String name;
|
| @OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
| private List<Employee> employees;
|
| public Long getId() {
| return id;
| }
| public void setId(Long id) {
| this.id = id;
| }
|
| public String getName() {
| return name;
| }
|
| public void setName(String name) {
| this.name = name;
| }
|
| public List<Employee> getEmployees() {
| return employees;
| }
|
| public void setEmployees(List<Employee> employees) {
| this.employees = employees;
| }
| }
New: Employee.java
package org.jboss.seam.example.seampay;
|
| import javax.persistence.Entity;
| import javax.persistence.GeneratedValue;
| import javax.persistence.Id;
| import javax.persistence.ManyToOne;
| import java.io.Serializable;
|
| @Entity
| public class Employee implements Serializable {
| @Id @GeneratedValue
| private Long id;
|
| String name;
|
| @ManyToOne
| private Department department;
|
| public Long getId() {
| return id;
| }
| public void setId(Long id) {
| this.id = id;
| }
|
| public String getName() {
| return name;
| }
|
| public void setName(String name) {
| this.name = name;
| }
|
| public Department getDepartment() {
| return department;
| }
|
| public void setDepartment(Department department) {
| this.department = department;
| }
| }
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4069777#...
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&a...