Skip to content

Commit

Permalink
HHH-12280 - Resolve {alias} in @formula like Restrictions.sqlRestrict…
Browse files Browse the repository at this point in the history
…ion()
  • Loading branch information
quaff authored and vladmihalcea committed Feb 12, 2018
1 parent 993a229 commit c2351bb
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 2 deletions.
Expand Up @@ -89,7 +89,7 @@ protected String substituteBrackets(String sqlQuery) throws QueryException {
for ( int curr = 0; curr < sqlQuery.length(); curr = right + 1 ) {
if ( ( left = sqlQuery.indexOf( '{', curr ) ) < 0 ) {
// No additional open braces found in the string, append the
// rest of the string in its entirty and quit this loop
// rest of the string in its entirety and quit this loop
result.append( sqlQuery.substring( curr ) );
break;
}
Expand Down Expand Up @@ -171,6 +171,9 @@ else if ( context.isEntityAlias( aliasName ) ) {
}
}
}
else {
result.append( '{' ).append(aliasPath).append( '}' );
}
}

// Possibly handle :something parameters for the query ?
Expand Down
Expand Up @@ -10,6 +10,7 @@

import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.function.SQLFunctionRegistry;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.sql.Template;

/**
Expand All @@ -33,7 +34,8 @@ public Formula(String formula) {

@Override
public String getTemplate(Dialect dialect, SQLFunctionRegistry functionRegistry) {
return Template.renderWhereStringTemplate(formula, dialect, functionRegistry);
String template = Template.renderWhereStringTemplate(formula, dialect, functionRegistry);
return StringHelper.replace( template, "{alias}", Template.TEMPLATE );
}

@Override
Expand Down
@@ -0,0 +1,130 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.annotations.formula;

import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.annotations.Formula;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.query.Query;
import org.hibernate.test.annotations.formula.FormulaWithColumnTypesTest.ExtendedDialect;
import org.hibernate.test.locking.A;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;

/**
* @author Yanming Zhou*
*/
@RequiresDialect(H2Dialect.class)
public class FormulaWithAliasTest extends BaseCoreFunctionalTestCase {

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Customer.class };
}

@Override
protected void configure(Configuration configuration) {
configuration.setProperty(
Environment.DIALECT,
ExtendedDialect.class.getName()
);
}

@Test
@TestForIssue(jiraKey = "HHH-12280")
public void testFormulaWithAlias() throws Exception {
doInHibernate( this::sessionFactory, session -> {
Customer company1 = new Customer();
company1.setBalance(new BigDecimal(100));
company1.setVip(true);
session.persist(company1);

Customer company2 = new Customer();
company2.setBalance(new BigDecimal(1000));
company2.setVip(false);
session.persist(company2);
} );

doInHibernate( this::sessionFactory, session -> {
List<Customer> customers = session.createQuery(
"select c " +
"from Customer c ", Customer.class)
.getResultList();

assertEquals(2, customers.size());
assertEquals(1d, customers.get(0).getPercentage().doubleValue(), 0);
assertEquals(1d, customers.get(1).getPercentage().doubleValue(), 0);
} );
}

@Entity(name = "Customer")
public static class Customer implements Serializable{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private BigDecimal balance;

@Formula("balance/(select sum(c.balance) from Customer c where c.vip = {alias}.vip)")
private BigDecimal percentage;

private boolean vip;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public BigDecimal getBalance() {
return balance;
}

public void setBalance(BigDecimal balance) {
this.balance = balance;
}

public BigDecimal getPercentage() {
return percentage;
}

public void setPercentage(BigDecimal percentage) {
this.percentage = percentage;
}

public boolean isVip() {
return vip;
}

public void setVip(boolean vip) {
this.vip = vip;
}

}

}

0 comments on commit c2351bb

Please sign in to comment.