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

Amit Anand

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.

v.ladynev

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");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I set a @Transient field value after query? Using Hibernate Criteria for query

How to join two tables with a group query using Hibernate (5.4) Criteria?

Hibernate Criteria / Query on object properties

How to build a query using Hibernate Criteria and Projections

How can I stack search criteria using & in a nested query in ElasticSearch

How can I do two countifs criteria while using importrange?

How do you "OR" criteria together when using a criteria query with hibernate?

How can I concatenate two arrays in C using memcpy?

How can I merge or concatenate two sequential models using Keras?

How can I concatenate two strings using template literals in typeScript?

How can I concatenate results of two observables using mergemap

Hibernate criteria query. After creating a sub-criteria, can I return to the original criteria?

How can I write a Hibernate Criteria query, for a super-class, and check for a certain sub-class?

How can I make a query that will sort data on two criteria then add all values corresponding to that criteria from the initial table/query?

How to create restriction between two different aliases on Hibernate Criteria query?

How do I do arithmetic with values from two columns with different criteria and then combining the answers, all in one query?

create query using hibernate criteria

How do I check if an object has only one property or only two properties and no other property in javascript

Hibernate criteria query on different properties of different objects

Date between two properties in Hibernate Criteria API

How can I concatenate two LSTM with Keras?

how can I concatenate two arrays in javascript?

How can I concatenate text in one row?

How can I different parameters using Criteria Restriction.like, Hibernate and Spring?

How can I delete duplicates group 3 columns using two criteria (first two columns)?

How do I create my query with Hibernate Criteria

How can I sort two vectors in the same way, with criteria that uses only one of the vectors?

How can I automatically number rows in Excel based off two different criteria, one being date?

How can I query for multiple properties not known in advance using Expando?

TOP Ranking

HotTag

Archive