Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i concatenate two properties into one property using hibernate criteria query

Tags:

java

hibernate

for example there are 2 properties house number and pincode and i want a single property as address like house number is 10 and pincode is 110064 and combine address property is 10,110064 this is my code

  final Criteria criteria= getDatabaseSession().createCriteria(Application.class, "application");
 final ProjectionList projectionList=Projections.projectionList();
 criteria.setProjection(projectionList);

projectionList.add(Projections.property("address.street"), "street");
 projectionList.add(Projections.property("address.postcode"), "postcode");
 projectionList.add(Projections.property("address.houseNumber"), "houseNumber");

 criteria.createAlias("application.applicationCase", "applicationCase", JoinType.INNER_JOIN);
 criteria.createAlias("applicationCase.property", "property");
 criteria.createAlias("property.address", "address");
 criteria.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
 return (Map<String, Object>) criteria.uniqueResult(); 

and i want to do something like this

   projectionList.add(Projections.property("address.street"+"address.houseNumber"+"address.postcode"),"address");

can somebody help.

like image 491
Amit Anand Avatar asked Feb 18 '16 10:02

Amit Anand


People also ask

How do you write join query in Hibernate using criteria?

Criteria in Hibernate can be used for join queries by joining multiple tables, useful methods for Hibernate criteria join are createAlias(), setFetchMode() and setProjection() Criteria in Hibernate API can be used for fetching results with conditions, useful methods are add() where we can add Restrictions.

How do I concatenate in HQL?

You may create a calculated column in your entity: @Formula(value = " concat(first_name, ' ', last_name) ") private String fullName; And in your HQL you just refer to this field as you would do to any other.

How do you put restrictions in criteria query?

Restrictions with CriteriaCriteria cr = session. createCriteria(Employee. class); Criterion salary = Restrictions.gt("salary", 2000); Criterion name = Restrictions. ilike("firstNname","zara%"); // To get records matching with OR conditions LogicalExpression orExp = Restrictions.or(salary, name); cr.


1 Answers

Using HQL

You can use the concat expression, but it can be used only with HQL

select concat(address.street, address.houseNumber, address.postcode) as fullAddress from ...

Using @Formula

If you want to use Criteria, you can use @Formula. It is need to add additional property to the persistent

@Formula(value = "concat(f_street, f_houseNumber, f_postcode)")
private String fullAddress;

You need to specify column names (not property names), because of Hibernate adds them to the SQL as is. It is not very convenient when you use joins — you need to specify aliases, generated by Hibernate, in the formula.

You can refer to the fullAddress in the Projection

projectionList.add(Projections.property("fullAddress"), "fullAddress");

I have tested it with MySQl. For Oracle you can try to use

@Formula(value = "f_street || f_houseNumber || f_postcode")
private String fullAddress;

Extending Hibernate

I have tried to extend the Projection to add concat function to the Criteria. I test it for the simplest case.

public class ConcatProjection extends SimpleProjection {

    private static final String CONCAT_FUNCTION_NAME = "concat";

    private final String[] properties;

    public ConcatProjection(String... properties) {
        this.properties = properties;
    }

    @Override
    public String toSqlString(Criteria criteria, int loc, CriteriaQuery criteriaQuery)
            throws HibernateException {
        String result = getFunction(criteriaQuery).render(StringType.INSTANCE,
                propertiesToColumns(criteria, criteriaQuery), criteriaQuery.getFactory());
        return result + " as y" + loc + '_';
    }

    private List<String> propertiesToColumns(Criteria criteria, CriteriaQuery criteriaQuery) {
        List<String> result = new ArrayList<String>(properties.length);

        for (String property : properties) {
            result.add(criteriaQuery.getColumn(criteria, property));
        }

        return result;
    }

    private SQLFunction getFunction(CriteriaQuery criteriaQuery) {
        return criteriaQuery.getFactory().getSqlFunctionRegistry()
                .findSQLFunction(CONCAT_FUNCTION_NAME);
    }

    @Override
    public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery)
            throws HibernateException {
        return new Type[] { StringType.INSTANCE };
    }

}

Using

projectionList.add(
    new ConcatProjection("address.street", 
        "address.houseNumber",  "address.postcode"), "fullAddress");
like image 126
v.ladynev Avatar answered Sep 23 '22 12:09

v.ladynev