简体   繁体   English

休眠复合ID标准

[英]hibernate Criteria on Composite Id

Below is the same code very similar to my code where i am facing the issue 下面是与我遇到问题的代码非常相似的相同代码

//mapped to Table B
class B implements Serializable {
   //Primary key bId
   private Long bId;
   //Getter and setter for bId;
}

//Mapped to table C
class C implements Serializable {
   //Primary key bId
   private Long cId;
   //Getter and setter for cId;
}

//mapped to Table A (Which has composite key fBid and fCid)
class A{
   //Composite primary key
   private B bid;
   private C cid;
   //getter and setter for B and C
  //Other fields

}

If I will create a criteria like below :- 如果我将创建如下条件:-

B b = new B();
b.setBId(1l);

C c = new C();
c.setCId(2l);

Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(A.class,"a");
        criteria.add(Restrictions.eq("a.bid", b));
        criteria.add(Restrictions.eq("a.cid",c));
        A result = (A) criteria.uniqueResult();
        return result;

I am getting below exception 我在例外之下

org.postgresql.util.PSQLException: ERROR: operator does not exist: integer = bytea org.postgresql.util.PSQLException:错误:运算符不存在:integer = bytea

even i created a separate class BC which has object of B and C class and used this in class A as composite key. 甚至我创建了一个单独的BC类,它具有B和C类的对象,并在A类中将其用作组合键。 and then did a 然后做了一个

 Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(A.class,"a");
            criteria.add(Restrictions.eq("a.BC", bc));
            A result = (A) criteria.uniqueResult();
            return result;

Still same error 还是一样的错误

any Help will be appreciated. 任何帮助将不胜感激。

Note :- I am doing hibernate xml maping for all the classes. 注意:-我正在为所有类进行休眠xml映射。



solution that i have found changed the class A like below :- 我发现的解决方案更改了A类,如下所示:-

class A{
private BC bc;
//getter and setter 
}

where BC

class BC{
 private B b;
Private C c;
//getter and setters 
}

Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(A.class,"a");
        criteria.add(Restrictions.eq("bc.b", b));
        criteria.add(Restrictions.eq("bc.c",c));
        A result = (A) criteria.uniqueResult();
        return result;

the error is indicating , that you are trying to compare 2 different things. 错误指示,您正在尝试比较2种不同的事物。 "a.bc" will return the blob object and bc inside java will give you the actual pointer value. “ a.bc”将返回blob对象,而java内部的bc将为您提供实际的指针值。 You can change the restriction with 您可以通过更改限制

criteria.add(Restrictions.eq("a.bId.bId", b.bId));
criteria.add(Restrictions.eq("a.cid.cId",c.cId));

Note that this will work , because you are defining the Criteria Class with createCriteria(A.class,"a") so "a" will be the Alias for the A Class 请注意,这将起作用,因为您正在使用createCriteria(A.class,"a")定义Criteria类,因此“ a”将成为A Class的别名

For More info , check this answer 有关更多信息,请查看此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM