2014-04-24 2 views
-1

나는 엔티티 관리자를 통해 고객 객체를 생성하는 방법으로 클래스를 가졌다. 생성 된 객체 집합을 반환하는 다른 메서드를 추가하고 싶습니다. 내 경우에는 어떻게 할 수 있니?지속 된 개체 집합을 반환하는 방법?

public class DefaultCoreRepository implements CoreRepository { 

private EntityManager entityManager; 

@PersistenceContext(unitName = "crm-db") 
public void setEntityManager(EntityManager entityManager) { 
    this.entityManager = entityManager; 
} 

private <T> T persist(T entity) { 
    entityManager.persist(entity); 
    return entity; 
} 

public void createCustomer(Customer customer) { 
    persist(customer); 
} 

public Set<Customer> getCustomers() { 
    //Code to be written here 
} 
+2

공식 JPA 튜토리얼을 먼저 읽으십시오. h ttp : //docs.oracle.com/javaee/6/tutorial/doc/bnbpz.html –

+0

이 프로젝트를 시작하기 전에이 작업을 수행했습니다. 감사. – Bravo

답변

1

당신은 수를 사용하여 결과 목록을 얻을 수 있도록 엔티티 관리자에서 선택 쿼리를 만들 수 있습니다 될 수있다 : 예를 들어, 다음 코드를 다음과 같은 쿼리를 작성하고 목록을 변환 세트로 변환하십시오 :

public Set<Customer> getCustomers() { 
    return new HashSet<Customer>(createQuery("select c from Customer c", Customer.class).getResultList()); 
} 
+0

어떻게 작동하나요? 즉, 엔티티 관리자는 모든 지속 된 객체를 어디에 저장합니까? – Bravo

+0

귀하의 데이터베이스에서 고객 객체를 가져와 주셔서 감사합니다. 자세한 내용은 http://docs.oracle.com/javaee/6/tutorial/doc/bnbrg.html –

+0

고맙습니다. – Bravo

2

한 가지 가능한 방법은

entityManager.createQuery("SELECT customer FROM Customer customer", 
          Customer.class) 
      .getResultList(); 

당신은 JPA

+0

답변 해 주셔서 대단히 감사합니다. – Bravo

관련 문제