-1
我需要幫助。 我想要得到的是如何在DB中找到對象名稱,並且還能夠編輯並保存對象。按名稱查找對象,使用JPA編輯對象
業務邏輯學
更新員工
public void edit(Long id, Customer editCustomer) {
try {
entityTransaction = em.getTransaction();
em.getTransaction().begin();
em.merge(editCustomer);
Customer customer = em.find(Customer.class, id);
customer.setId(id);
customer.setFirstName(editCustomer.getFirstName());
customer.setLastName(editCustomer.getLastName());
customer.setAddress(editCustomer.getAddress());
customer.setId(editCustomer.getId());
em.getTransaction().commit();
} catch (Exception e) {
if (em.isOpen())
em.getTransaction().rollback();
e.printStackTrace();
}
}
FindByName
公衆客戶getCustomerByName(字符串的firstName){
TypedQuery查詢= em.createQuery(「選擇C FROM Customer c WHERE
c.firstName =:firstName「,Customer.class);
return query.setParameter(「firstName」,firstName).getSingleResult();
}
JSF ManagedBean
公共字符串findByName(){
BookstoreManager.getInstance().getCustomerByName(firstName);
System.out.println("Hello " + customer.getFirstName());
return "Customer";
}
**Result**
2017-07-02T16:43:13.927 + 0200 |信息:你好null
XHTML