2011-01-20 3 views
1

Netbeans를 사용하여 엔티티 클래스를 생성했습니다. 내 클래스에는 복합 기본 키가 있으므로 Netbeans에서 나를 위해 추가 PK 엔터티 클래스를 생성했습니다. 엔티티 클래스에서 NamedQuery를 사용하려고하지만 NamedQuery 호출이 실패하고 쿼리가 완료되지 않은 것처럼 보이는 경우 명명 된 쿼리에 전달중인 param이 PK 클래스에 포함되어 있습니다.PK 엔티티가있는 JPA NamedQuery

누군가 내 코드가 복합 PK (@EmbeddedId 연결)가있는 NamedQuery를 사용하는 모습을 보여줄 수 있습니까? 다음은 실패

외관 클래스 (일부) :

@Stateless 
public class EMyEntityFacade extends AbstractFacade<EMyEntity> { 

public EMyEntityFacade() { 
    super(EMyEntity.class); 
} 

//does not work 
/* 
private Query queryByComp1Id(int comp1Id) { 
    Query query = this.getEntityManager().createNamedQuery("EMyEntity.findByComp1Id"); 
    query.setParameter("comp1Id", comp1Id); 
    return query; 
} 
*/ 

//any nearer? 
private Query queryByComp1d(int comp1Id) { 
    EMyEntityPK eMyEntityPK = new EMyEntityPK(); 

    Query query = this.getEntityManager().createNamedQuery("EMyEntity.findByComp1Id"); 

    eMyEntityPK.setComp1Id(comp1Id); 

    //how do I pass the eMyEntityPK through to the Entity so the query is well formed? 
    //??? 

    return query; 
} 

public List<EMyEntity> findByComp1Id(int comp1Id) { 
    Query query = queryByComp1Id(comp1Id); 
    return query.getResultList(); 
} 

엔티티 클래스 (일부) :

@Entity 
@Table(name = "my_entity") 
@NamedQueries({ 
    @NamedQuery(name = "EMyEntity.findByComp1Id", 
    query = "SELECT e FROM EMyEntity e WHERE e.eMyEntityPK.comp1Id = :comp1Id"),...)}) 

public class EMyEntity implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @EmbeddedId 
    protected EMyEntityPK eMyEntityPK; 
    @Basic(optional = false) 
    @Column(name = "inherit_from_parent") 
    ... 
    ... 
} 

PK 클래스 (일부) :

@Embeddable 
public class EMyEntityPK implements Serializable { 
    @Basic(optional = false) 
    @Column(name = "comp1_id") 
    private int comp1Id; 
    @Basic(optional = false) 
    @Column(name = "comp2_id") 
    private int comp2Id; 
    ... 
    ... 
} 
+0

무엇이 오류입니까? – Bozho

+0

예외 및 스택 트레이스 표시 – Bozho

+0

오류가 표시되지 않습니다. 명명 된 쿼리는 정상입니다. 어디에서 문제가 있습니까? –

답변

1

당신은 통과해야 누가 하나의 필드가 아닌 기본 키를 나타내는 who 객체. pk의 일부를 쿼리해야하는 경우 where 절에서 참조하십시오. where pk.fld=

+0

감사합니다. Bozho, 예제 코드를 게시 할 수 있습니까? NamedQuery에는 where 절에 'where e.eMyEntityPK.comp1Id = : comp1Id'가 이미 있습니다. 고맙습니다. – JPC

+0

쿼리 및 사용자 엔터티 및 pk 개체 표시 – Bozho

관련 문제