Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we check hibernate criteria alias already exists?

A few minute ago get an error "duplicate alias" and I started the think about "Can we check alias was already created? alias was already exists."

Do you know about this? Can we check this?

for example :

Criteria criteria = getSeession().createCriteria(Example.class,"example");
criteria.createAlias("example.test","test");

Now I want this, I'll check if this "test" alias is already created I won't create not I create.

Is that possible? If is that possible how? If Is not possible, Could you offer any other solution?

like image 611
luffy Avatar asked Oct 18 '22 09:10

luffy


1 Answers

We have encountered a similar issue with the complex search criteries. A possible workaround is having an additional criteria building infrastructure to ignore alias duplicates. For an example, using such classes: Aliases, Alias .

    Criteria criteria = getSeession().createCriteria(Example.class, "example");

    Aliases aliases = Aliases.create()
        .add("example.test", "test", JoinType.INNER);
    criteria.add(Restrictions.eq("test", "test_value"));

    aliases.addToCriteria(criteria); // add aliases to the criteria

    List<Example> examples = criteria.list();

To use Aliases class you can just download this library (it doesn't need additional jars): fluent-hibernate.

like image 55
v.ladynev Avatar answered Oct 30 '22 01:10

v.ladynev