Hi,
I've got a simple JPA2 Criteria query which fails when doing an object equality. I verified this against EclipseLink and over there, it works. I'll Attach some code snippets to help figure it out:
@Entity()
public class Product{
.
.
.
@ManyToOne
private Compamy owningCompany;
}
@Entity()
public class Company{
.
.
.
@Id
private Long id;
}
and the query:
@Override
public T fetch(Company company, Long primaryKey) {
mLogger.info("Fetching type: {} with id {} with company: {}", new Object[]{managedClass, primaryKey, company});
CriteriaBuilder qb = getEntityManager().getCriteriaBuilder();
CriteriaQuery<T> c = qb.createQuery(managedClass);
Root<T> p = c.from(managedClass);
c.select(p);
c.where(
qb.equal(p.get("id"), primaryKey),
qb.and(qb.equal(p.get("owningCompany"), company)));
TypedQuery<T> q = getEntityManager().createQuery(c);
List<T> resultList = q.getResultList();
if(resultList.size() > 0){
return resultList.get(0);
}
return null;
}
The error I get is:
Caused by: com.objectdb.o.UserException: Unsupported query operator '@' at com.objectdb.o.MSG.d(MSG.java:61) at com.objectdb.o.TKR.t(TKR.java:383) at com.objectdb.o.TKR.r(TKR.java:149) at com.objectdb.o.TKI.<init>(TKI.java:53) at com.objectdb.o.QSP.<init>(QSP.java:77) at com.objectdb.o.QPR.H(QPR.java:979) at com.objectdb.o.QPR.<init>(QPR.java:102) at com.objectdb.o.QRC.<init>(QRC.java:116) at com.objectdb.o.QRM.UR(QRM.java:242) at com.objectdb.o.MST.UR(MST.java:878) at com.objectdb.o.WRA.UR(WRA.java:286) at com.objectdb.o.WSM.UR(WSM.java:113) at com.objectdb.o.STC.r(STC.java:418) at com.objectdb.o.SHN.ah(SHN.java:468) at com.objectdb.o.SHN.J(SHN.java:146) at com.objectdb.o.HND.run(HND.java:133) at java.lang.Thread.run(Thread.java:680)
Looking at the log:
[2011-04-28 04:18:07 #1900 query.manager]
<queryRequest query="SELECT $1 FROM Product $1 WHERE ($1.id=8) AND $1.owningCompany=com.x.y.z.Company@540ef9ef" args="null" transactionId="-1" />
So it seems, that it is doing a 'Company.toString()' on the instance instead of resolving the id? Have I misunderstood something again? Under eclipselink, this did work (going to PostgreSQL).
If I change my path expressions to point to basic java types (path expression pointing to id):
@Override
public T fetch(Company company, Long primaryKey) {
mLogger.info("Fetching type: {} with id {} with company: {}", new Object[]{managedClass, primaryKey, company});
CriteriaBuilder qb = getEntityManager().getCriteriaBuilder();
CriteriaQuery<T> c = qb.createQuery(managedClass);
Root<T> p = c.from(managedClass);
c.select(p);
c.where(
qb.equal(p.get("id"), primaryKey),
qb.and(qb.equal(p.get("owningCompany").get("id"), company.getId())));
TypedQuery<T> q = getEntityManager().createQuery(c);
List<T> resultList = q.getResultList();
if(resultList.size() > 0){
return resultList.get(0);
}
return null;
}
I don't know if it's a bug, I just need help :)
Thanks
ObjectDB Version: 2.2.2_04