我想写这让我回通过cardId
的customerId
的方法,但是这是不工作时,Eclipse说customerId
不能被解析为一个变量JPA的EntityManager find方法不起作用
public CustomerMapping getCustomerMapping(String cardId) {
this.getEntityManager();
return em.find(CustomerMapping.class, customerId);
}
@Stateless
public class CustomerMappingEJB {
private EntityManager em;
private void getEntityManager() {
em = MultiTenantEntityManagerFactory.getEntityManager();
}
private void beginTransaction() {
em.getTransaction().begin();
}
private void commitTransaction() {
em.getTransaction().commit();
}
private void insert(CustomerMapping customerMapping) {
em.persist(customerMapping);
}
public CustomerMapping getCustomerMapping(long id) {
this.getEntityManager();
return em.find(CustomerMapping.class, id);
}
public List<CustomerMapping> getCustomerMappings() {
TypedQuery<CustomerMapping> query = null;
this.getEntityManager();
query = em.createNamedQuery("AllCustomerIds", CustomerMapping.class);
return query.getResultList();
}
public CustomerMappingHelper getCustomerMappingHelper(String cardId) {
this.getEntityManager();
return em.find(CustomerMappingHelper.class, cardId);
}
public CustomerMapping addNew(CustomerMapping customerMapping) {
this.getEntityManager();
this.beginTransaction();
this.insert(customerMapping);
this.commitTransaction();
return customerMapping;
}
}
我没有把MultiTenantEntityManagerFactory
这个类放在这里,因为我猜你不会需要这个类,那么当你调用它时,我有CustomerMapping
类与“一堆列表”,并且只有一些设置和获取id的方法, cardId和customerId我没有复制,因为这将是非常大,如果你也想我也会做
public class CustomerMapping {
@Id
@GeneratedValue
public Long id;
@Basic
private String customerId;
@Basic
private String cardId;
@Basic
private String hashKey;
}
您没有在该方法中定义'customerId',并且'customerId'没有作为参数传递,它没有声明为变量,所以您会得到该错误。 – Unknown