【发布时间】:2016-09-21 12:54:52
【问题描述】:
我想编写一个方法,通过cardId 找回customerId,但这不起作用,Eclipse 说customerId 无法解析为变量
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未作为参数传入,未声明为变量,因此您会收到该错误。