JPA EntityManager查找方法不起作用

时间:2016-05-24 10:48:47

标签: java jpa

我想编写一个方法,通过customerId让我回到cardId,但这不起作用,Eclipse说customerId无法解析为变量< / p>

    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课程,其中包含&#34;列表&#34;正如你所说的那样,只有一些set和get方法的id,cardId和customerId我也没有复制它,因为如果你也想这样做我会做的很大

public class CustomerMapping {

    @Id
    @GeneratedValue
    public Long id;

    @Basic
    private String customerId;

    @Basic
    private String cardId;

    @Basic
    private String hashKey;

}

1 个答案:

答案 0 :(得分:2)

您可以x使用CustomerMapping获取cardId。更优化的解决方案是使用预测。

JPQL