case insensitive hibernate criteria on composite key
I am having a hibernate entity with composite key like the below sample.
class A{
B id; //this is the composite key for this class
int property1;
int property2;
int property3;
//getters and setters
}
class B{
String prop1;
String prop2;
String prop3;
}
In the above example A entity has the B as a composite key . Now I wrote the below criteria to get the A object by passing composite key to crieteria. Sample code is below.
B id=new B("Prop1Val","Prop2Val","Prop3Val");
(A) sessionFactory.getCurrentSession().get(A.class,id)
My questions are :
Is it right way to create a criteria like below to solve the issue?
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(A.class); criteria.add(Restrictions.eq("id.prop1",id.getProp1()).ignoreCase()); criteria.add(Restrictions.eq("id.prop2",id.getProp2()).ignoreCase()); criteria.add(Restrictions.eq("id.prop3",id.getProp3()).ignoreCase());
Are there any simple way to configure it hbm file to use case insensitive by default?
Please help me in this.
Your criteria looks fine, but your request to do a case-insensitive get by default does not make much sense. What would it do of several rows in the database have PKs differing only in case?
I would just make sure that your key properties are always in lower-case, either by throwing exceptions if upper-case values are passed to the B constructor, or by automatically transforming them to lower-case.
You could do the same using checked constraints in the database, in case some other app inserts data in the database.
链接地址: http://www.djcxy.com/p/37066.html