JSF SelectOneMenu コンポーネントから Customer エンティティを取得しようとしています: 私が持っているクラスは次のとおりです:
顧客エンティティ
@Entity
@Table(name="CUSTOMERS")
@NamedQuery(name="Customer.findAll", query="SELECT c FROM Customer c")
public class Customer implements Serializable {
@Id
@GeneratedValue
@Column(name="CUSTOMER_ID")
private Long customerId;
@Column(name="FIRST_NAME")
private String firstName;
@Column(name="LAST_NAME")
private String lastName;
private String email;
public Long getCustomerId() {
return customerId;
}
public void setCustomerId(Long customerId) {
this.customerId = customerId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
Long localCustomerId = customerId;
String localFirstName = firstName;
String localLastName = lastName;
String localEmail = email;
if (localCustomerId == null) {
localCustomerId = 0L;
}
if (localEmail == null) {
localEmail = "";
}
if (localFirstName == null) {
localFirstName = "";
}
if (localLastName == null) {
localLastName = "";
}
String toString = "customerId = " + localCustomerId + "\n";
toString += "firstName = " + localFirstName + "\n";
toString += "lastName = " + localLastName + "\n";
toString += "email = " + localEmail;
return toString;
}
}
カスタマーコントローラー
@ManagedBean
@RequestScoped
public class CustomerController implements Serializable {
private static final long serialVersionUID = 1L;
@Resource(name = "jdbc/__CustomerDBPool")
private DataSource dataSource;
@PersistenceUnit(unitName = "customerPersistenceUnit")
private EntityManagerFactory emf;
@Resource
private UserTransaction userTransaction;
@ManagedProperty(value = "#{customer}")
private Customer customer;
public DataSource getDataSource() {
return dataSource;
}
public EntityManagerFactory getEmf() {
return emf;
}
public void saveCustomer() {
EntityManager entityManager = emf.createEntityManager();
try {
userTransaction.begin();
entityManager.persist(customer);
userTransaction.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void setEmf(EntityManagerFactory emf) {
this.emf = emf;
}
public List<Customer> getCustomerList() {
List<Customer> list = null;
EntityManager em = emf.createEntityManager();
try {
Query q = em.createNamedQuery("Customer.findAll");
int m = q.getResultList().size();
list = (List<Customer>) q.getResultList();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public Customer getCustomerById(Long id) {
EntityManager entityManager = emf.createEntityManager();
Query q = entityManager.createQuery("Select c FROM Customer c WHERE c.id = :id");
q.setParameter("id", id);
Customer c = (Customer) q.getResultList().get(q.getFirstResult());
return c;
}
public String getCustomerDetails() {
if (customer == null)
return "NULL";
else
return customer.toString();
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
//System.out.println(customer);
}
}
カスタマーコンバーター
@ManagedBean
@RequestScoped
@FacesConverter(value = "CustomerConverter")
public class CustomerConverter implements Converter, Serializable {
@ManagedProperty(value = "#{cController}")
private CustomerController cc;
@Override
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
Long pk = Long.parseLong(value);
Customer c = null;
List<Customer> list = (List<Customer>) cc.getCustomerList();
for (Customer k : list) {
if (k.getCustomerId().equals(pk))
c = k;
}
return c;
}
@Override
public String getAsString(FacesContext context, UIComponent component,
Object value) {
Long id = (Long) value;
return id.toString();
}
public CustomerController getCc() {
return cc;
}
public void setCc(CustomerController cc) {
this.cc = cc;
}
}
そして最後に .xhtml ファイル
<h:head>
<title>Save Customer</title>
</h:head>
<h:body>
<h:panelGrid columns="1">
<h:form>
<h:messages id="messages"></h:messages>
<p/>
<h:selectOneMenu id="select1" value="#{customerController.customer}" converter="CustomerConverter">
<f:selectItems
value="#{customerController.customerList}" var="c"
itemLabel="#{c.customerId} #{c.firstName} #{c.lastName}"
itemValue="#{c.customerId}"/>
</h:selectOneMenu>
<p/>
<h:commandButton value="Output" update="output2"></h:commandButton>
<p/>
<h:outputText id="output2" value="#{customerController.customerDetails}"/>
</h:form>
</h:panelGrid>
</h:body>
</html>
基本的に、CustomerConverter Bean で CustomerController Bean をインスタンス化するのに問題があるようです。私が間違っていることはありますか?