Hi,
I'm using Hibernate 3.2.6. I have a search form in a web app which has lots of fields, they're all optional so you can search for something in many ways. Behind this, I have a method with lots of if statements that say things like "if this field isn't empty then add this criterion". I sometimes need to use Criteria Aliases for joins but someone might fill in two fields that both need the same alias to be created. In this case I would get a duplicate Alias exception. The best I have come up with so far was from someone somewhere on the internet suggesting you create a list and keep track of the Aliases in there. So I have created the following method to help with this:
Code:
private void createAlias(ArrayList list, Criteria criteria, String propertyName, String aliasName) {
if (!list.contains(propertyName)) {
criteria.createAlias(propertyName, aliasName);
list.add(propertyName);
}
}
This isn't great though, you have to keep track of the List and which Criteria it is associated with. Surely there must be a much better way of doing this. Is there no way I can check the criteria to see if the alias has been created instead of using my own list or, even better, is there something built into Hibernate that creates an alias if it doesn't exist?