Olá pessoal, estou desenvolvendo uma ferramenta usando JPA (Hibernate) para realizar a persitëncia.
Em algumas situações preciso fazer uma busca de determinado registro filtrando por atributos que fazer parte de uma composite id.
Seguem o código das classes:
@Embeddable
public class CustomerPK implements Serializable {
private static final long serialVersionUID = 314150377756575581L;
private long id;
private long dId;
private int wId;
public CustomerPK() {
}
public CustomerPK(long id, long id2, int id3) {
this.id = id;
dId = id2;
wId = id3;
}
@Column(name = "C_ID", nullable = false)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "C_D_ID", nullable = false)
public long getDId() {
return dId;
}
public void setDId(long id) {
dId = id;
}
@Column(name = "C_W_ID", nullable = false)
public int getWId() {
return wId;
}
public void setWId(int id) {
wId = id;
}
}
@Entity
@Table(name = "CUSTOMER")
@NamedQueries({
@NamedQuery(name = "selectAllCustomers", query = "select c from Customer c"),
@NamedQuery(name = "countAllCustomers", query = "select count(*) from Customer c"),
@NamedQuery(name = "deleteAllCustomers", query = "delete from Customer c"),
@NamedQuery(name = "getCustomerByLastNameAndHomeWarehouse", query = "from " +
"Customer as c where c.cPk.dId=:dId and c.cPk.wId=:wId and c.last=:last")
})
public class Customer implements Serializable {
private static final long serialVersionUID = -6640493644418267201L;
private CustomerPK cPk;
...
private String last;
...
@Id
@AttributeOverrides({
@AttributeOverride(name="id", column=@Column(name="C_ID", nullable = false)),
@AttributeOverride(name="dId", column=@Column(name="C_D_ID", nullable = false)),
@AttributeOverride(name="wId", column=@Column(name="C_W_ID", nullable = false))
})
public CustomerPK getCPk() {
return cPk;
}
@Column(name = "C_LAST", length = 16)
public String getLast() {
return last;
}
}
No momento da execução da aplicação, quando tento obter uma EntityManagerFactory, está sendo a lançada a seguinte exception:
ERROR [main] (Log4JLogger.java:119) - Error in named query: getCustomerByLastNameAndHomeWarehouse
org.hibernate.QueryException: could not resolve property: cPk of: br.***.Customer [from br.***.Customer as c where c.cPk.dId=:dId and c.cPk.wId=:wId and c.last=:last]
Os estranho é que na documentação do Hibernate Entity Manager está escrito que é possível construir a NamedQuery como eu fiz
"Properties of composite identifiers may also be used. Suppose Person has a composite identifier consisting of
country and medicareNumber.
select person from bank.Person person
where person.id.country = 'AU'
and person.id.medicareNumber = 123456
select account from bank.Account account
where account.owner.id.country = 'AU'
and account.owner.id.medicareNumber = 123456
Alguém tem idéia do que posso estar fazendo errado?
Toda ajuda será muito bem vinda!!
Obrigado.
Murilo