Question

So I'm using GigaSpaces XAP with Hibernate plugin to load my DB table into the their grid cache, unfortunately for querying the grid they do not support directly case insensitive searches.

They offered 2 solutions:
1- Use LIKE query (same as SQL) which is slow (Wont even bother with this)
2- Create a separate property of the fields I want to have case insensitive.

So now my choices are:
1- Create an extra column in DB to have case-insensitive field (Not in this life time)
2- Create a custom data loader plugin for XAP such that the moment the data is loaded the field is stored in a "ToLower" properperty. (Supported but will leave as last resort)

I'm definitely not going with #1 and I will leave the custom data loader as a last result. So my idea is to...

class Person {
    String firstName
    String firstNameLower

    public void setFirstName(String firstName) {
        this.firstName = firstName

        this.firstNameLower = firstName.toLowerCase(...);
    }
}

Will this work with Hibernate? As the GigaSpace dataloader uses Hibernate and my pojo is pretty much an Entity class. I'm using Hibernate XML mapping not annotations. The "ToLower" fields will not be mapped. Does Hibernate call setXXX() on all fields that are mapped or does it do some fancy code replacement under the hood and doesn't call the setXXX() methods.

I'm also thinking an annotation would be good use here but not sure how to implement them or even if it's possible for this case.

Was it helpful?

Solution 2

For anyone interested.

Simply create a "tolower()" method of the field and add the index annotation on that and you can map your pojo to hibernate the usual way.

OTHER TIPS

hibernate does use proxy objects, but the data is still stored in your object and hibernate will use the getter/setter implementations you provide, so your solution should work if you want to set the lowercase version, although I'm not clear why you need a separate instance variable to store the lowercase version.

this article goes over the basics of proxies.

If you configure Hibernate to use the fields accessor method (and not directly access the fields) its default behavior is to use all the setXXX() method.

I think in your case the best solution would be (if Hibernate is only used for GigaSpace) to customise the way Hibernate load the data, by defining a custom type using standrd StringType to load the data then converting it to lower case...

Meanwhile, if your system requires lower case data, can't you make sure all strings entered into the database are lower case ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top