mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-26 05:14:57 +00:00
6 - SQM based on JPA type system
This commit is contained in:
parent
d1b86c2a72
commit
ff59a1301d
@ -9,14 +9,16 @@
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Currency;
|
||||
import java.util.List;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.query.Query;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.dialect.DB2Dialect;
|
||||
import org.hibernate.dialect.HSQLDialect;
|
||||
import org.hibernate.dialect.SybaseASE15Dialect;
|
||||
import org.hibernate.hql.internal.ast.QuerySyntaxException;
|
||||
import org.hibernate.query.Query;
|
||||
import org.hibernate.query.SemanticException;
|
||||
|
||||
import org.hibernate.testing.DialectChecks;
|
||||
import org.hibernate.testing.RequiresDialectFeature;
|
||||
@ -39,18 +41,21 @@ public String[] getMappings() {
|
||||
|
||||
@Test
|
||||
public void testCompositeUserType() {
|
||||
Session s = openSession();
|
||||
org.hibernate.Transaction t = s.beginTransaction();
|
||||
|
||||
inTransaction(
|
||||
s -> {
|
||||
Transaction tran = new Transaction();
|
||||
tran.setDescription( "a small transaction" );
|
||||
tran.setValue( new MonetoryAmount( new BigDecimal( 1.5 ), Currency.getInstance( "USD" ) ) );
|
||||
s.persist( tran );
|
||||
|
||||
List result = s.createQuery("from Transaction tran where tran.value.amount > 1.0 and tran.value.currency = 'USD'").list();
|
||||
List result = s.createQuery(
|
||||
"from Transaction tran where tran.value.amount > 1.0 and tran.value.currency = 'USD'" )
|
||||
.list();
|
||||
assertEquals( result.size(), 1 );
|
||||
tran.getValue().setCurrency( Currency.getInstance( "AUD" ) );
|
||||
result = s.createQuery("from Transaction tran where tran.value.amount > 1.0 and tran.value.currency = 'AUD'").list();
|
||||
result = s.createQuery(
|
||||
"from Transaction tran where tran.value.amount > 1.0 and tran.value.currency = 'AUD'" )
|
||||
.list();
|
||||
assertEquals( result.size(), 1 );
|
||||
|
||||
if ( !( getDialect() instanceof HSQLDialect ) ) {
|
||||
@ -64,16 +69,16 @@ public void testCompositeUserType() {
|
||||
}
|
||||
|
||||
s.delete( tran );
|
||||
t.commit();
|
||||
s.close();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SkipForDialect(value = { SybaseASE15Dialect.class }, jiraKey = "HHH-6788")
|
||||
@SkipForDialect(value = { DB2Dialect.class }, jiraKey = "HHH-6867")
|
||||
public void testCustomColumnReadAndWrite() {
|
||||
Session s = openSession();
|
||||
org.hibernate.Transaction t = s.beginTransaction();
|
||||
inTransaction(
|
||||
s -> {
|
||||
final BigDecimal AMOUNT = new BigDecimal( 73000000d );
|
||||
final BigDecimal AMOUNT_MILLIONS = AMOUNT.divide( new BigDecimal( 1000000d ) );
|
||||
MutualFund f = new MutualFund();
|
||||
@ -82,40 +87,50 @@ public void testCustomColumnReadAndWrite() {
|
||||
s.flush();
|
||||
|
||||
// Test value conversion during insert
|
||||
BigDecimal amountViaSql = (BigDecimal)s.createSQLQuery("select amount_millions from MutualFund").uniqueResult();
|
||||
BigDecimal amountViaSql = (BigDecimal) s.createNativeQuery( "select amount_millions from MutualFund" )
|
||||
.uniqueResult();
|
||||
assertEquals( AMOUNT_MILLIONS.doubleValue(), amountViaSql.doubleValue(), 0.01d );
|
||||
|
||||
// Test projection
|
||||
BigDecimal amountViaHql = (BigDecimal)s.createQuery("select f.holdings.amount from MutualFund f").uniqueResult();
|
||||
BigDecimal amountViaHql = (BigDecimal) s.createQuery( "select f.holdings.amount from MutualFund f" )
|
||||
.uniqueResult();
|
||||
assertEquals( AMOUNT.doubleValue(), amountViaHql.doubleValue(), 0.01d );
|
||||
|
||||
// Test restriction and entity load via criteria
|
||||
BigDecimal one = new BigDecimal( 1 );
|
||||
f = (MutualFund)s.createCriteria(MutualFund.class)
|
||||
.add(Restrictions.between("holdings.amount", AMOUNT.subtract(one), AMOUNT.add(one)))
|
||||
.uniqueResult();
|
||||
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<MutualFund> criteria = criteriaBuilder.createQuery( MutualFund.class );
|
||||
Root<MutualFund> root = criteria.from( MutualFund.class );
|
||||
criteria.where( criteriaBuilder.between(
|
||||
root.get( "holdings" ).get( "amount" ),
|
||||
AMOUNT.subtract( one ),
|
||||
AMOUNT.add( one )
|
||||
) );
|
||||
f = s.createQuery( criteria ).uniqueResult();
|
||||
// f = (MutualFund)s.createCriteria(MutualFund.class)
|
||||
// .add(Restrictions.between("holdings.amount", AMOUNT.subtract(one), AMOUNT.add(one)))
|
||||
// .uniqueResult();
|
||||
assertEquals( AMOUNT.doubleValue(), f.getHoldings().getAmount().doubleValue(), 0.01d );
|
||||
|
||||
// Test predicate and entity load via HQL
|
||||
f = (MutualFund) s.createQuery( "from MutualFund f where f.holdings.amount between ?1 and ?2" )
|
||||
.setBigDecimal(1, AMOUNT.subtract(one))
|
||||
.setBigDecimal(2, AMOUNT.add(one))
|
||||
.setParameter( 1, AMOUNT.subtract( one ) )
|
||||
.setParameter( 2, AMOUNT.add( one ) )
|
||||
.uniqueResult();
|
||||
assertEquals( AMOUNT.doubleValue(), f.getHoldings().getAmount().doubleValue(), 0.01d );
|
||||
|
||||
s.delete( f );
|
||||
t.commit();
|
||||
s.close();
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the {@code =} operator on composite types.
|
||||
*/
|
||||
public void testEqualOperator() {
|
||||
final Session s = openSession();
|
||||
s.getTransaction().begin();
|
||||
|
||||
inTransaction(
|
||||
s -> {
|
||||
final Transaction txn = new Transaction();
|
||||
txn.setDescription( "foo" );
|
||||
txn.setValue( new MonetoryAmount( new BigDecimal( 42 ), Currency.getInstance( "AUD" ) ) );
|
||||
@ -125,19 +140,31 @@ public void testEqualOperator() {
|
||||
final Query q = s.createQuery( "from Transaction where value = :amount" );
|
||||
|
||||
/* Both amount and currency match. */
|
||||
q.setParameter( "amount", new MonetoryAmount( new BigDecimal( 42 ), Currency.getInstance( "AUD" ) ) );
|
||||
q.setParameter(
|
||||
"amount",
|
||||
new MonetoryAmount( new BigDecimal( 42 ), Currency.getInstance( "AUD" ) )
|
||||
);
|
||||
assertEquals( 1, q.list().size() );
|
||||
|
||||
/* Only currency matches. */
|
||||
q.setParameter( "amount", new MonetoryAmount( new BigDecimal( 36 ), Currency.getInstance( "AUD" ) ) );
|
||||
q.setParameter(
|
||||
"amount",
|
||||
new MonetoryAmount( new BigDecimal( 36 ), Currency.getInstance( "AUD" ) )
|
||||
);
|
||||
assertEquals( 0, q.list().size() );
|
||||
|
||||
/* Only amount matches. */
|
||||
q.setParameter( "amount", new MonetoryAmount( new BigDecimal( 42 ), Currency.getInstance( "EUR" ) ) );
|
||||
q.setParameter(
|
||||
"amount",
|
||||
new MonetoryAmount( new BigDecimal( 42 ), Currency.getInstance( "EUR" ) )
|
||||
);
|
||||
assertEquals( 0, q.list().size() );
|
||||
|
||||
/* None match. */
|
||||
q.setParameter( "amount", new MonetoryAmount( new BigDecimal( 76 ), Currency.getInstance( "USD" ) ) );
|
||||
q.setParameter(
|
||||
"amount",
|
||||
new MonetoryAmount( new BigDecimal( 76 ), Currency.getInstance( "USD" ) )
|
||||
);
|
||||
assertEquals( 0, q.list().size() );
|
||||
|
||||
final Query qTimestamp = s.createQuery( "from Transaction where timestamp = :timestamp" );
|
||||
@ -167,8 +194,8 @@ public void testEqualOperator() {
|
||||
assertEquals( 0, qTimestamp.list().size() );
|
||||
|
||||
s.delete( txn );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -177,9 +204,8 @@ public void testEqualOperator() {
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-5946")
|
||||
public void testNotEqualOperator() {
|
||||
final Session s = openSession();
|
||||
s.getTransaction().begin();
|
||||
|
||||
inTransaction(
|
||||
s -> {
|
||||
final Transaction t1 = new Transaction();
|
||||
t1.setDescription( "foo" );
|
||||
t1.setValue( new MonetoryAmount( new BigDecimal( 178 ), Currency.getInstance( "EUR" ) ) );
|
||||
@ -199,11 +225,17 @@ public void testNotEqualOperator() {
|
||||
s.persist( t3 );
|
||||
|
||||
final Query q1 = s.createQuery( "from Transaction where value <> :amount" );
|
||||
q1.setParameter( "amount", new MonetoryAmount( new BigDecimal( 178 ), Currency.getInstance( "EUR" ) ) );
|
||||
q1.setParameter(
|
||||
"amount",
|
||||
new MonetoryAmount( new BigDecimal( 178 ), Currency.getInstance( "EUR" ) )
|
||||
);
|
||||
assertEquals( 2, q1.list().size() );
|
||||
|
||||
final Query q2 = s.createQuery( "from Transaction where value <> :amount and description = :str" );
|
||||
q2.setParameter( "amount", new MonetoryAmount( new BigDecimal( 1000000 ), Currency.getInstance( "USD" ) ) );
|
||||
q2.setParameter(
|
||||
"amount",
|
||||
new MonetoryAmount( new BigDecimal( 1000000 ), Currency.getInstance( "USD" ) )
|
||||
);
|
||||
q2.setParameter( "str", "bar" );
|
||||
assertEquals( 1, q2.list().size() );
|
||||
|
||||
@ -214,8 +246,8 @@ public void testNotEqualOperator() {
|
||||
s.delete( t3 );
|
||||
s.delete( t2 );
|
||||
s.delete( t1 );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -226,19 +258,15 @@ public void testNotEqualOperator() {
|
||||
@TestForIssue(jiraKey = "HHH-5946")
|
||||
@RequiresDialectFeature(value = DialectChecks.DoesNotSupportRowValueConstructorSyntax.class)
|
||||
public void testLessThanOperator() {
|
||||
final Session s = openSession();
|
||||
try {
|
||||
try (Session s = openSession()) {
|
||||
final Query q = s.createQuery( "from Transaction where value < :amount" );
|
||||
q.setParameter( "amount", new MonetoryAmount( BigDecimal.ZERO, Currency.getInstance( "EUR" ) ) );
|
||||
q.list();
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
assertTyping( QuerySyntaxException.class, e.getCause() );
|
||||
assertTyping( SemanticException.class, e.getCause() );
|
||||
//expected
|
||||
}
|
||||
finally {
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -249,19 +277,15 @@ public void testLessThanOperator() {
|
||||
@TestForIssue(jiraKey = "HHH-5946")
|
||||
@RequiresDialectFeature(value = DialectChecks.DoesNotSupportRowValueConstructorSyntax.class)
|
||||
public void testLessOrEqualOperator() {
|
||||
final Session s = openSession();
|
||||
try {
|
||||
try (Session s = openSession()) {
|
||||
final Query q = s.createQuery( "from Transaction where value <= :amount" );
|
||||
q.setParameter( "amount", new MonetoryAmount( BigDecimal.ZERO, Currency.getInstance( "USD" ) ) );
|
||||
q.list();
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
assertTyping( QuerySyntaxException.class, e.getCause() );
|
||||
assertTyping( SemanticException.class, e.getCause() );
|
||||
//expected
|
||||
}
|
||||
finally {
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -272,19 +296,15 @@ public void testLessOrEqualOperator() {
|
||||
@TestForIssue(jiraKey = "HHH-5946")
|
||||
@RequiresDialectFeature(value = DialectChecks.DoesNotSupportRowValueConstructorSyntax.class)
|
||||
public void testGreaterThanOperator() {
|
||||
final Session s = openSession();
|
||||
try {
|
||||
try (Session s = openSession()) {
|
||||
final Query q = s.createQuery( "from Transaction where value > :amount" );
|
||||
q.setParameter( "amount", new MonetoryAmount( BigDecimal.ZERO, Currency.getInstance( "EUR" ) ) );
|
||||
q.list();
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
assertTyping( QuerySyntaxException.class, e.getCause() );
|
||||
assertTyping( SemanticException.class, e.getCause() );
|
||||
//expected
|
||||
}
|
||||
finally {
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -295,19 +315,15 @@ public void testGreaterThanOperator() {
|
||||
@TestForIssue(jiraKey = "HHH-5946")
|
||||
@RequiresDialectFeature(value = DialectChecks.DoesNotSupportRowValueConstructorSyntax.class)
|
||||
public void testGreaterOrEqualOperator() {
|
||||
final Session s = openSession();
|
||||
try {
|
||||
try (Session s = openSession()) {
|
||||
final Query q = s.createQuery( "from Transaction where value >= :amount" );
|
||||
q.setParameter( "amount", new MonetoryAmount( BigDecimal.ZERO, Currency.getInstance( "USD" ) ) );
|
||||
q.list();
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
assertTyping( QuerySyntaxException.class, e.getCause() );
|
||||
assertTyping( SemanticException.class, e.getCause() );
|
||||
//expected
|
||||
}
|
||||
finally {
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,6 +21,7 @@
|
||||
|
||||
import org.hibernate.LockOptions;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.engine.spi.QueryParameters;
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
import org.hibernate.jdbc.Work;
|
||||
import org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor;
|
||||
|
@ -16,6 +16,7 @@
|
||||
import org.hibernate.LockOptions;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.engine.spi.QueryParameters;
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
import org.hibernate.jdbc.Work;
|
||||
import org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor;
|
||||
|
@ -17,7 +17,6 @@
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.dialect.PostgreSQL81Dialect;
|
||||
import org.hibernate.dialect.PostgreSQLDialect;
|
||||
import org.hibernate.id.PostInsertIdentifierGenerator;
|
||||
|
||||
import org.hibernate.testing.DialectCheck;
|
||||
import org.hibernate.testing.RequiresDialectFeature;
|
@ -37,8 +37,6 @@
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
Loading…
x
Reference in New Issue
Block a user