6 - SQM based on JPA type system
This commit is contained in:
parent
a15774b7e0
commit
911c0220fe
|
@ -8,6 +8,12 @@ package org.hibernate.test.annotations.enumerated.custom_mapkey;
|
|||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.JoinType;
|
||||
import javax.persistence.criteria.MapJoin;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.mapping.Map;
|
||||
import org.hibernate.mapping.PersistentClass;
|
||||
|
@ -194,19 +200,29 @@ public class MapKeyCustomEnumTypeTest extends BaseNonConfigCoreFunctionalTestCas
|
|||
|
||||
private EntityMapEnum assertFindCriteria(
|
||||
EntityMapEnum expected,
|
||||
String mapPath, Object param) {
|
||||
String mapPath,
|
||||
Object param) {
|
||||
assertNotEquals( 0, expected.id );
|
||||
|
||||
Session session = openNewSession();
|
||||
session.beginTransaction();
|
||||
EntityMapEnum found = (EntityMapEnum) session.createCriteria( EntityMapEnum.class )
|
||||
.createCriteria( mapPath, "m" )
|
||||
.add( Restrictions.eq( "indices", param ) )
|
||||
.uniqueResult();
|
||||
//find
|
||||
assetEntityMapEnumEquals( expected, found );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
EntityMapEnum found = inTransactionReturn(
|
||||
session -> {
|
||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<EntityMapEnum> criteria = criteriaBuilder.createQuery( EntityMapEnum.class );
|
||||
Root<EntityMapEnum> root = criteria.from( EntityMapEnum.class );
|
||||
MapJoin<Object, Object,Object> join = (MapJoin<Object, Object,Object>) root.join( mapPath, JoinType.INNER );
|
||||
criteria.where( criteriaBuilder.equal( join.key(), param ) ) ;
|
||||
EntityMapEnum result = session.createQuery( criteria ).uniqueResult();
|
||||
|
||||
// EntityMapEnum result = (EntityMapEnum) session.createCriteria( EntityMapEnum.class )
|
||||
// .createCriteria( mapPath, "m" )
|
||||
// .add( Restrictions.eq( "indices", param ) )
|
||||
// .uniqueResult();
|
||||
//find
|
||||
assetEntityMapEnumEquals( expected, result );
|
||||
return result;
|
||||
}
|
||||
);
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
|
@ -219,7 +235,8 @@ public class MapKeyCustomEnumTypeTest extends BaseNonConfigCoreFunctionalTestCas
|
|||
|
||||
EntityMapEnum found = assertFindCriteria(
|
||||
entityMapEnum,
|
||||
"ordinalMap", Common.A1
|
||||
"ordinalMap",
|
||||
Common.A1
|
||||
);
|
||||
assertFalse( found.ordinalMap.isEmpty() );
|
||||
delete( id );
|
||||
|
@ -245,7 +262,8 @@ public class MapKeyCustomEnumTypeTest extends BaseNonConfigCoreFunctionalTestCas
|
|||
|
||||
found = assertFindCriteria(
|
||||
entityMapEnum,
|
||||
"firstLetterMap", FirstLetter.A_LETTER
|
||||
"firstLetterMap",
|
||||
FirstLetter.A_LETTER
|
||||
);
|
||||
assertFalse( found.firstLetterMap.isEmpty() );
|
||||
delete( id );
|
||||
|
|
|
@ -12,9 +12,10 @@ import javax.persistence.Column;
|
|||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.annotations.Formula;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.Environment;
|
||||
|
@ -48,75 +49,84 @@ public class FormulaWithColumnTypesTest extends BaseCoreFunctionalTestCase {
|
|||
@TestForIssue(jiraKey = "HHH-9951")
|
||||
public void testFormulaAnnotationWithTypeNames() {
|
||||
|
||||
Session session = openSession();
|
||||
Transaction transaction = session.beginTransaction();
|
||||
inTransaction(
|
||||
session -> {
|
||||
DisplayItem displayItem20 = new DisplayItem();
|
||||
displayItem20.setDisplayCode( "20" );
|
||||
|
||||
DisplayItem displayItem20 = new DisplayItem();
|
||||
displayItem20.setDisplayCode( "20" );
|
||||
DisplayItem displayItem03 = new DisplayItem();
|
||||
displayItem03.setDisplayCode( "03" );
|
||||
|
||||
DisplayItem displayItem03 = new DisplayItem();
|
||||
displayItem03.setDisplayCode( "03" );
|
||||
DisplayItem displayItem100 = new DisplayItem();
|
||||
displayItem100.setDisplayCode( "100" );
|
||||
|
||||
DisplayItem displayItem100 = new DisplayItem();
|
||||
displayItem100.setDisplayCode( "100" );
|
||||
|
||||
session.persist( displayItem20 );
|
||||
session.persist( displayItem03 );
|
||||
session.persist( displayItem100 );
|
||||
|
||||
transaction.commit();
|
||||
session.close();
|
||||
session.persist( displayItem20 );
|
||||
session.persist( displayItem03 );
|
||||
session.persist( displayItem100 );
|
||||
}
|
||||
);
|
||||
|
||||
// 1. Default sorting by display code natural ordering (resulting in 3-100-20).
|
||||
session = openSession();
|
||||
transaction = session.beginTransaction();
|
||||
inTransaction(
|
||||
session -> {
|
||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<DisplayItem> criteria = criteriaBuilder.createQuery( DisplayItem.class );
|
||||
Root<DisplayItem> root = criteria.from( DisplayItem.class );
|
||||
criteria.orderBy( criteriaBuilder.asc( root.get( "displayCode" ) ) );
|
||||
|
||||
List displayItems = session.createCriteria( DisplayItem.class )
|
||||
.addOrder( Order.asc( "displayCode" ) )
|
||||
.list();
|
||||
List displayItems = session.createQuery( criteria ).list();
|
||||
|
||||
assertNotNull( displayItems );
|
||||
assertEquals( displayItems.size(), 3 );
|
||||
assertEquals(
|
||||
"03",
|
||||
( (DisplayItem) displayItems.get( 0 ) ).getDisplayCode()
|
||||
);
|
||||
assertEquals(
|
||||
"100",
|
||||
( (DisplayItem) displayItems.get( 1 ) ).getDisplayCode()
|
||||
);
|
||||
assertEquals(
|
||||
"20",
|
||||
( (DisplayItem) displayItems.get( 2 ) ).getDisplayCode()
|
||||
);
|
||||
transaction.commit();
|
||||
session.close();
|
||||
// List displayItems = session.createCriteria( DisplayItem.class )
|
||||
// .addOrder( Order.asc( "displayCode" ) )
|
||||
// .list();
|
||||
|
||||
assertNotNull( displayItems );
|
||||
assertEquals( displayItems.size(), 3 );
|
||||
assertEquals(
|
||||
"03",
|
||||
( (DisplayItem) displayItems.get( 0 ) ).getDisplayCode()
|
||||
);
|
||||
assertEquals(
|
||||
"100",
|
||||
( (DisplayItem) displayItems.get( 1 ) ).getDisplayCode()
|
||||
);
|
||||
assertEquals(
|
||||
"20",
|
||||
( (DisplayItem) displayItems.get( 2 ) ).getDisplayCode()
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
// 2. Sorting by the casted type (resulting in 3-20-100).
|
||||
session = openSession();
|
||||
transaction = session.beginTransaction();
|
||||
inTransaction(
|
||||
session -> {
|
||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<DisplayItem> criteria = criteriaBuilder.createQuery( DisplayItem.class );
|
||||
Root<DisplayItem> root = criteria.from( DisplayItem.class );
|
||||
criteria.orderBy( criteriaBuilder.asc( root.get( "displayCodeAsInteger" ) ) );
|
||||
|
||||
List displayItemsSortedByInteger = session.createCriteria( DisplayItem.class )
|
||||
.addOrder( Order.asc( "displayCodeAsInteger" ) )
|
||||
.list();
|
||||
List displayItemsSortedByInteger = session.createQuery( criteria ).list();
|
||||
|
||||
assertNotNull( displayItemsSortedByInteger );
|
||||
assertEquals( displayItemsSortedByInteger.size(), 3 );
|
||||
assertEquals(
|
||||
"03",
|
||||
( (DisplayItem) displayItemsSortedByInteger.get( 0 ) ).getDisplayCode()
|
||||
// List displayItemsSortedByInteger = session.createCriteria( DisplayItem.class )
|
||||
// .addOrder( Order.asc( "displayCodeAsInteger" ) )
|
||||
// .list();
|
||||
|
||||
assertNotNull( displayItemsSortedByInteger );
|
||||
assertEquals( displayItemsSortedByInteger.size(), 3 );
|
||||
assertEquals(
|
||||
"03",
|
||||
( (DisplayItem) displayItemsSortedByInteger.get( 0 ) ).getDisplayCode()
|
||||
);
|
||||
assertEquals(
|
||||
"20",
|
||||
( (DisplayItem) displayItemsSortedByInteger.get( 1 ) ).getDisplayCode()
|
||||
);
|
||||
assertEquals(
|
||||
"100",
|
||||
( (DisplayItem) displayItemsSortedByInteger.get( 2 ) ).getDisplayCode()
|
||||
);
|
||||
}
|
||||
);
|
||||
assertEquals(
|
||||
"20",
|
||||
( (DisplayItem) displayItemsSortedByInteger.get( 1 ) ).getDisplayCode()
|
||||
);
|
||||
assertEquals(
|
||||
"100",
|
||||
( (DisplayItem) displayItemsSortedByInteger.get( 2 ) ).getDisplayCode()
|
||||
);
|
||||
transaction.commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -10,13 +10,17 @@ import java.util.Arrays;
|
|||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.dialect.H2Dialect;
|
||||
|
||||
import org.hibernate.testing.RequiresDialect;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
@ -40,98 +44,128 @@ public class DefaultNullOrderingTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testHqlDefaultNullOrdering() {
|
||||
Session session = openSession();
|
||||
inSession( session -> {
|
||||
// Populating database with test data.
|
||||
try {
|
||||
session.getTransaction().begin();
|
||||
Monkey monkey1 = new Monkey();
|
||||
monkey1.setName( null );
|
||||
Monkey monkey2 = new Monkey();
|
||||
monkey2.setName( "Warsaw ZOO" );
|
||||
session.persist( monkey1 );
|
||||
session.persist( monkey2 );
|
||||
session.getTransaction().commit();
|
||||
|
||||
// Populating database with test data.
|
||||
session.getTransaction().begin();
|
||||
Monkey monkey1 = new Monkey();
|
||||
monkey1.setName( null );
|
||||
Monkey monkey2 = new Monkey();
|
||||
monkey2.setName( "Warsaw ZOO" );
|
||||
session.persist( monkey1 );
|
||||
session.persist( monkey2 );
|
||||
session.getTransaction().commit();
|
||||
session.getTransaction().begin();
|
||||
List<Zoo> orderedResults = (List<Zoo>) session.createQuery( "from Monkey m order by m.name" )
|
||||
.list(); // Should order by NULLS LAST.
|
||||
Assert.assertEquals( Arrays.asList( monkey2, monkey1 ), orderedResults );
|
||||
session.getTransaction().commit();
|
||||
|
||||
session.getTransaction().begin();
|
||||
List<Zoo> orderedResults = (List<Zoo>) session.createQuery( "from Monkey m order by m.name" ).list(); // Should order by NULLS LAST.
|
||||
Assert.assertEquals( Arrays.asList( monkey2, monkey1 ), orderedResults );
|
||||
session.getTransaction().commit();
|
||||
session.clear();
|
||||
|
||||
session.clear();
|
||||
|
||||
// Cleanup data.
|
||||
session.getTransaction().begin();
|
||||
session.delete( monkey1 );
|
||||
session.delete( monkey2 );
|
||||
session.getTransaction().commit();
|
||||
|
||||
session.close();
|
||||
// Cleanup data.
|
||||
session.getTransaction().begin();
|
||||
session.delete( monkey1 );
|
||||
session.delete( monkey2 );
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
catch (Exception e) {
|
||||
if ( session.getTransaction().isActive() ) {
|
||||
session.getTransaction().rollback();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnnotationsDefaultNullOrdering() {
|
||||
Session session = openSession();
|
||||
inSession(
|
||||
session -> {
|
||||
try {
|
||||
// Populating database with test data.
|
||||
session.getTransaction().begin();
|
||||
Troop troop = new Troop();
|
||||
troop.setName( "Alpha 1" );
|
||||
Soldier ranger = new Soldier();
|
||||
ranger.setName( "Ranger 1" );
|
||||
troop.addSoldier( ranger );
|
||||
Soldier sniper = new Soldier();
|
||||
sniper.setName( null );
|
||||
troop.addSoldier( sniper );
|
||||
session.persist( troop );
|
||||
session.getTransaction().commit();
|
||||
|
||||
// Populating database with test data.
|
||||
session.getTransaction().begin();
|
||||
Troop troop = new Troop();
|
||||
troop.setName( "Alpha 1" );
|
||||
Soldier ranger = new Soldier();
|
||||
ranger.setName( "Ranger 1" );
|
||||
troop.addSoldier( ranger );
|
||||
Soldier sniper = new Soldier();
|
||||
sniper.setName( null );
|
||||
troop.addSoldier( sniper );
|
||||
session.persist( troop );
|
||||
session.getTransaction().commit();
|
||||
session.clear();
|
||||
|
||||
session.clear();
|
||||
session.getTransaction().begin();
|
||||
troop = (Troop) session.get( Troop.class, troop.getId() );
|
||||
Iterator<Soldier> iterator = troop.getSoldiers().iterator(); // Should order by NULLS LAST.
|
||||
Assert.assertEquals( ranger.getName(), iterator.next().getName() );
|
||||
Assert.assertNull( iterator.next().getName() );
|
||||
session.getTransaction().commit();
|
||||
|
||||
session.getTransaction().begin();
|
||||
troop = (Troop) session.get( Troop.class, troop.getId() );
|
||||
Iterator<Soldier> iterator = troop.getSoldiers().iterator(); // Should order by NULLS LAST.
|
||||
Assert.assertEquals( ranger.getName(), iterator.next().getName() );
|
||||
Assert.assertNull( iterator.next().getName() );
|
||||
session.getTransaction().commit();
|
||||
session.clear();
|
||||
|
||||
session.clear();
|
||||
|
||||
// Cleanup data.
|
||||
session.getTransaction().begin();
|
||||
session.delete( troop );
|
||||
session.getTransaction().commit();
|
||||
|
||||
session.close();
|
||||
// Cleanup data.
|
||||
session.getTransaction().begin();
|
||||
session.delete( troop );
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
catch (Exception e) {
|
||||
if ( session.getTransaction().isActive() ) {
|
||||
session.getTransaction().rollback();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCriteriaDefaultNullOrdering() {
|
||||
Session session = openSession();
|
||||
inSession(
|
||||
session -> {
|
||||
try{
|
||||
// Populating database with test data.
|
||||
session.getTransaction().begin();
|
||||
Monkey monkey1 = new Monkey();
|
||||
monkey1.setName( null );
|
||||
Monkey monkey2 = new Monkey();
|
||||
monkey2.setName( "Berlin ZOO" );
|
||||
session.persist( monkey1 );
|
||||
session.persist( monkey2 );
|
||||
session.getTransaction().commit();
|
||||
|
||||
// Populating database with test data.
|
||||
session.getTransaction().begin();
|
||||
Monkey monkey1 = new Monkey();
|
||||
monkey1.setName( null );
|
||||
Monkey monkey2 = new Monkey();
|
||||
monkey2.setName( "Berlin ZOO" );
|
||||
session.persist( monkey1 );
|
||||
session.persist( monkey2 );
|
||||
session.getTransaction().commit();
|
||||
session.getTransaction().begin();
|
||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<Monkey> criteria = criteriaBuilder.createQuery( Monkey.class );
|
||||
Root<Monkey> root = criteria.from( Monkey.class );
|
||||
criteria.orderBy( criteriaBuilder.asc( root.get( "name" ) ) );
|
||||
|
||||
session.getTransaction().begin();
|
||||
Criteria criteria = session.createCriteria( Monkey.class );
|
||||
criteria.addOrder( org.hibernate.criterion.Order.asc( "name" ) ); // Should order by NULLS LAST.
|
||||
Assert.assertEquals( Arrays.asList( monkey2, monkey1 ), criteria.list() );
|
||||
session.getTransaction().commit();
|
||||
Assert.assertEquals( Arrays.asList( monkey2, monkey1 ), session.createQuery( criteria ).list() );
|
||||
|
||||
session.clear();
|
||||
// Criteria criteria = session.createCriteria( Monkey.class );
|
||||
// criteria.addOrder( org.hibernate.criterion.Order.asc( "name" ) ); // Should order by NULLS LAST.
|
||||
// Assert.assertEquals( Arrays.asList( monkey2, monkey1 ), criteria.list() );
|
||||
session.getTransaction().commit();
|
||||
|
||||
// Cleanup data.
|
||||
session.getTransaction().begin();
|
||||
session.delete( monkey1 );
|
||||
session.delete( monkey2 );
|
||||
session.getTransaction().commit();
|
||||
session.clear();
|
||||
|
||||
session.close();
|
||||
// Cleanup data.
|
||||
session.getTransaction().begin();
|
||||
session.delete( monkey1 );
|
||||
session.delete( monkey2 );
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
catch (Exception e) {
|
||||
if ( session.getTransaction().isActive() ) {
|
||||
session.getTransaction().rollback();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.NullPrecedence;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.dialect.H2Dialect;
|
||||
import org.hibernate.dialect.MySQLDialect;
|
||||
|
@ -138,43 +137,7 @@ public class OrderByTest extends BaseCoreFunctionalTestCase {
|
|||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-465")
|
||||
@RequiresDialect(value = { H2Dialect.class, MySQLDialect.class, SQLServer2008Dialect.class },
|
||||
comment = "By default H2 places NULL values first, so testing 'NULLS LAST' expression. " +
|
||||
"For MySQL and SQL Server 2008 testing overridden Dialect#renderOrderByElement(String, String, String, NullPrecedence) method. " +
|
||||
"MySQL and SQL Server 2008 does not support NULLS FIRST / LAST syntax at the moment, so transforming the expression to 'CASE WHEN ...'.")
|
||||
public void testCriteriaNullsFirstLast() {
|
||||
Session session = openSession();
|
||||
|
||||
// Populating database with test data.
|
||||
session.getTransaction().begin();
|
||||
Zoo zoo1 = new Zoo( null );
|
||||
Zoo zoo2 = new Zoo( "Warsaw ZOO" );
|
||||
session.persist( zoo1 );
|
||||
session.persist( zoo2 );
|
||||
session.getTransaction().commit();
|
||||
|
||||
session.clear();
|
||||
|
||||
session.getTransaction().begin();
|
||||
Criteria criteria = session.createCriteria( Zoo.class );
|
||||
criteria.addOrder( org.hibernate.criterion.Order.asc( "name" ).nulls( NullPrecedence.LAST ) );
|
||||
Iterator<Zoo> iterator = (Iterator<Zoo>) criteria.list().iterator();
|
||||
Assert.assertEquals( zoo2.getName(), iterator.next().getName() );
|
||||
Assert.assertNull( iterator.next().getName() );
|
||||
session.getTransaction().commit();
|
||||
|
||||
session.clear();
|
||||
|
||||
// Cleanup data.
|
||||
session.getTransaction().begin();
|
||||
session.delete( zoo1 );
|
||||
session.delete( zoo2 );
|
||||
session.getTransaction().commit();
|
||||
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-465")
|
||||
|
|
|
@ -18,6 +18,11 @@ import org.junit.Test;
|
|||
import java.math.BigDecimal;
|
||||
import java.util.Iterator;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Join;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
|
@ -26,62 +31,55 @@ import static org.junit.Assert.assertNotNull;
|
|||
*/
|
||||
public class ReferencedColumnNameTest extends BaseCoreFunctionalTestCase {
|
||||
@Test
|
||||
public void testManyToOne() throws Exception {
|
||||
Session s;
|
||||
Transaction tx;
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
Postman pm = new Postman( "Bob", "A01" );
|
||||
public void testManyToOne() {
|
||||
Postman postman = new Postman( "Bob", "A01" );
|
||||
House house = new House();
|
||||
house.setPostman( pm );
|
||||
house.setPostman( postman );
|
||||
house.setAddress( "Rue des pres" );
|
||||
s.persist( pm );
|
||||
s.persist( house );
|
||||
tx.commit();
|
||||
s.close();
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
house = (House) s.get( House.class, house.getId() );
|
||||
assertNotNull( house.getPostman() );
|
||||
assertEquals( "Bob", house.getPostman().getName() );
|
||||
pm = house.getPostman();
|
||||
s.delete( house );
|
||||
s.delete( pm );
|
||||
tx.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
s.persist( postman );
|
||||
s.persist( house );
|
||||
}
|
||||
);
|
||||
|
||||
inTransaction(
|
||||
s -> {
|
||||
House h = s.get( House.class, house.getId() );
|
||||
assertNotNull( h.getPostman() );
|
||||
assertEquals( "Bob", h.getPostman().getName() );
|
||||
Postman pm = h.getPostman();
|
||||
s.delete( h );
|
||||
s.delete( pm );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneToMany() throws Exception {
|
||||
Session s;
|
||||
Transaction tx;
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
public void testOneToMany() {
|
||||
inTransaction(
|
||||
s -> {
|
||||
Rambler rambler = new Rambler( "Emmanuel" );
|
||||
Bag bag = new Bag( "0001", rambler );
|
||||
rambler.getBags().add( bag );
|
||||
s.persist( rambler );
|
||||
}
|
||||
);
|
||||
|
||||
Rambler rambler = new Rambler( "Emmanuel" );
|
||||
Bag bag = new Bag( "0001", rambler );
|
||||
rambler.getBags().add( bag );
|
||||
s.persist( rambler );
|
||||
inTransaction(
|
||||
s -> {
|
||||
Bag bag = (Bag) s.createQuery( "select b from Bag b left join fetch b.owner" ).uniqueResult();
|
||||
assertNotNull( bag );
|
||||
assertNotNull( bag.getOwner() );
|
||||
|
||||
tx.commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
|
||||
bag = (Bag) s.createQuery( "select b from Bag b left join fetch b.owner" ).uniqueResult();
|
||||
assertNotNull( bag );
|
||||
assertNotNull( bag.getOwner() );
|
||||
|
||||
rambler = (Rambler) s.createQuery( "select r from Rambler r left join fetch r.bags" ).uniqueResult();
|
||||
assertNotNull( rambler );
|
||||
assertNotNull( rambler.getBags() );
|
||||
assertEquals( 1, rambler.getBags().size() );
|
||||
s.delete( rambler.getBags().iterator().next() );
|
||||
s.delete( rambler );
|
||||
|
||||
tx.commit();
|
||||
s.close();
|
||||
Rambler rambler = (Rambler) s.createQuery( "select r from Rambler r left join fetch r.bags" ).uniqueResult();
|
||||
assertNotNull( rambler );
|
||||
assertNotNull( rambler.getBags() );
|
||||
assertEquals( 1, rambler.getBags().size() );
|
||||
s.delete( rambler.getBags().iterator().next() );
|
||||
s.delete( rambler );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -90,37 +88,33 @@ public class ReferencedColumnNameTest extends BaseCoreFunctionalTestCase {
|
|||
jiraKey = "HHH-8190",
|
||||
comment = "uses Teradata reserved word - type"
|
||||
)
|
||||
public void testUnidirectionalOneToMany() throws Exception {
|
||||
Session s;
|
||||
Transaction tx;
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
public void testUnidirectionalOneToMany() {
|
||||
inTransaction(
|
||||
s -> {
|
||||
Clothes clothes = new Clothes( "underwear", "interesting" );
|
||||
Luggage luggage = new Luggage( "Emmanuel", "Cabin Luggage" );
|
||||
luggage.getHasInside().add( clothes );
|
||||
s.persist( luggage );
|
||||
}
|
||||
);
|
||||
|
||||
Clothes clothes = new Clothes( "underwear", "interesting" );
|
||||
Luggage luggage = new Luggage( "Emmanuel", "Cabin Luggage" );
|
||||
luggage.getHasInside().add( clothes );
|
||||
s.persist( luggage );
|
||||
inTransaction(
|
||||
s -> {
|
||||
Luggage luggage = (Luggage) s.createQuery( "select l from Luggage l left join fetch l.hasInside" )
|
||||
.uniqueResult();
|
||||
assertNotNull( luggage );
|
||||
assertNotNull( luggage.getHasInside() );
|
||||
assertEquals( 1, luggage.getHasInside().size() );
|
||||
|
||||
tx.commit();
|
||||
s.close();
|
||||
s.delete( luggage.getHasInside().iterator().next() );
|
||||
s.delete( luggage );
|
||||
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
|
||||
luggage = (Luggage) s.createQuery( "select l from Luggage l left join fetch l.hasInside" ).uniqueResult();
|
||||
assertNotNull( luggage );
|
||||
assertNotNull( luggage.getHasInside() );
|
||||
assertEquals( 1, luggage.getHasInside().size() );
|
||||
|
||||
s.delete( luggage.getHasInside().iterator().next() );
|
||||
s.delete( luggage );
|
||||
|
||||
tx.commit();
|
||||
s.close();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testManyToMany() throws Exception {
|
||||
public void testManyToMany(){
|
||||
Session s;
|
||||
Transaction tx;
|
||||
s = openSession();
|
||||
|
@ -144,14 +138,14 @@ public class ReferencedColumnNameTest extends BaseCoreFunctionalTestCase {
|
|||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
|
||||
whiteHouse = (House) s.get( House.class, whiteHouse.getId() );
|
||||
whiteHouse = s.get( House.class, whiteHouse.getId() );
|
||||
assertNotNull( whiteHouse );
|
||||
assertEquals( 2, whiteHouse.getHasInhabitants().size() );
|
||||
|
||||
tx.commit();
|
||||
s.clear();
|
||||
tx = s.beginTransaction();
|
||||
bill = (Inhabitant) s.get( Inhabitant.class, bill.getId() );
|
||||
bill = s.get( Inhabitant.class, bill.getId() );
|
||||
assertNotNull( bill );
|
||||
assertEquals( 1, bill.getLivesIn().size() );
|
||||
assertEquals( whiteHouse.getAddress(), bill.getLivesIn().iterator().next().getAddress() );
|
||||
|
@ -167,7 +161,7 @@ public class ReferencedColumnNameTest extends BaseCoreFunctionalTestCase {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testManyToOneReferenceManyToOne() throws Exception {
|
||||
public void testManyToOneReferenceManyToOne() {
|
||||
Item item = new Item();
|
||||
item.setId( 1 );
|
||||
Vendor vendor = new Vendor();
|
||||
|
@ -183,18 +177,18 @@ public class ReferencedColumnNameTest extends BaseCoreFunctionalTestCase {
|
|||
wItem.setItem( item );
|
||||
wItem.setQtyInStock( new BigDecimal(1) );
|
||||
wItem.setVendor( vendor );
|
||||
Session s = openSession( );
|
||||
s.getTransaction().begin();
|
||||
s.persist( item );
|
||||
s.persist( vendor );
|
||||
s.persist( cost );
|
||||
s.persist( wItem );
|
||||
s.flush();
|
||||
s.clear();
|
||||
wItem = (WarehouseItem) s.get(WarehouseItem.class, wItem.getId() );
|
||||
assertNotNull( wItem.getDefaultCost().getItem() );
|
||||
s.getTransaction().rollback();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
s.persist( item );
|
||||
s.persist( vendor );
|
||||
s.persist( cost );
|
||||
s.persist( wItem );
|
||||
s.flush();
|
||||
s.clear();
|
||||
WarehouseItem warehouseItem = s.get(WarehouseItem.class, wItem.getId() );
|
||||
assertNotNull( warehouseItem.getDefaultCost().getItem() );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -217,46 +211,63 @@ public class ReferencedColumnNameTest extends BaseCoreFunctionalTestCase {
|
|||
house.neighbourPlaces.kitchen = new Place();
|
||||
house.neighbourPlaces.kitchen.name = "His Kitchen";
|
||||
|
||||
Session s = openSession();
|
||||
Transaction tx = s.beginTransaction();
|
||||
s.save( house );
|
||||
s.flush();
|
||||
inTransaction(
|
||||
s -> {
|
||||
s.save( house );
|
||||
s.flush();
|
||||
|
||||
HousePlaces get = (HousePlaces) s.get( HousePlaces.class, house.id );
|
||||
assertEquals( house.id, get.id );
|
||||
HousePlaces get = s.get( HousePlaces.class, house.id );
|
||||
assertEquals( house.id, get.id );
|
||||
|
||||
HousePlaces uniqueResult = (HousePlaces) s.createQuery( "from HousePlaces h where h.places.livingRoom.name='First'" )
|
||||
.uniqueResult();
|
||||
assertNotNull( uniqueResult );
|
||||
assertEquals( uniqueResult.places.livingRoom.name, "First" );
|
||||
assertEquals( uniqueResult.places.livingRoom.owner, "mine" );
|
||||
HousePlaces uniqueResult = (HousePlaces) s.createQuery( "from HousePlaces h where h.places.livingRoom.name='First'" )
|
||||
.uniqueResult();
|
||||
assertNotNull( uniqueResult );
|
||||
assertEquals( uniqueResult.places.livingRoom.name, "First" );
|
||||
assertEquals( uniqueResult.places.livingRoom.owner, "mine" );
|
||||
|
||||
uniqueResult = (HousePlaces) s.createQuery( "from HousePlaces h where h.places.livingRoom.owner=:owner" )
|
||||
.setParameter( "owner", "mine" ).uniqueResult();
|
||||
assertNotNull( uniqueResult );
|
||||
assertEquals( uniqueResult.places.livingRoom.name, "First" );
|
||||
assertEquals( uniqueResult.places.livingRoom.owner, "mine" );
|
||||
uniqueResult = (HousePlaces) s.createQuery( "from HousePlaces h where h.places.livingRoom.owner=:owner" )
|
||||
.setParameter( "owner", "mine" ).uniqueResult();
|
||||
assertNotNull( uniqueResult );
|
||||
assertEquals( uniqueResult.places.livingRoom.name, "First" );
|
||||
assertEquals( uniqueResult.places.livingRoom.owner, "mine" );
|
||||
|
||||
assertNotNull( s.createCriteria( HousePlaces.class ).add( Restrictions.eq( "places.livingRoom.owner", "mine" ) )
|
||||
.uniqueResult() );
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<HousePlaces> criteria = criteriaBuilder.createQuery( HousePlaces.class );
|
||||
Root<HousePlaces> root = criteria.from( HousePlaces.class );
|
||||
Join<Object, Object> join = root.join( "places" ).join( "livingRoom" );
|
||||
criteria.where( criteriaBuilder.equal( join.get( "owner" ), "mine" ) );
|
||||
|
||||
// override
|
||||
uniqueResult = (HousePlaces) s.createQuery( "from HousePlaces h where h.neighbourPlaces.livingRoom.owner='his'" )
|
||||
.uniqueResult();
|
||||
assertNotNull( uniqueResult );
|
||||
assertEquals( uniqueResult.neighbourPlaces.livingRoom.name, "Neighbour" );
|
||||
assertEquals( uniqueResult.neighbourPlaces.livingRoom.owner, "his" );
|
||||
assertNotNull(s.createQuery( criteria ).uniqueResult());
|
||||
|
||||
uniqueResult = (HousePlaces) s.createQuery( "from HousePlaces h where h.neighbourPlaces.livingRoom.name=:name" )
|
||||
.setParameter( "name", "Neighbour" ).uniqueResult();
|
||||
assertNotNull( uniqueResult );
|
||||
assertEquals( uniqueResult.neighbourPlaces.livingRoom.name, "Neighbour" );
|
||||
assertEquals( uniqueResult.neighbourPlaces.livingRoom.owner, "his" );
|
||||
// assertNotNull( s.createCriteria( HousePlaces.class ).add( Restrictions.eq( "places.livingRoom.owner", "mine" ) )
|
||||
// .uniqueResult() );
|
||||
|
||||
assertNotNull( s.createCriteria( HousePlaces.class )
|
||||
.add( Restrictions.eq( "neighbourPlaces.livingRoom.owner", "his" ) ).uniqueResult() );
|
||||
// override
|
||||
uniqueResult = (HousePlaces) s.createQuery( "from HousePlaces h where h.neighbourPlaces.livingRoom.owner='his'" )
|
||||
.uniqueResult();
|
||||
assertNotNull( uniqueResult );
|
||||
assertEquals( uniqueResult.neighbourPlaces.livingRoom.name, "Neighbour" );
|
||||
assertEquals( uniqueResult.neighbourPlaces.livingRoom.owner, "his" );
|
||||
|
||||
tx.rollback();
|
||||
uniqueResult = (HousePlaces) s.createQuery( "from HousePlaces h where h.neighbourPlaces.livingRoom.name=:name" )
|
||||
.setParameter( "name", "Neighbour" ).uniqueResult();
|
||||
assertNotNull( uniqueResult );
|
||||
assertEquals( uniqueResult.neighbourPlaces.livingRoom.name, "Neighbour" );
|
||||
assertEquals( uniqueResult.neighbourPlaces.livingRoom.owner, "his" );
|
||||
|
||||
criteria = criteriaBuilder.createQuery( HousePlaces.class );
|
||||
root = criteria.from( HousePlaces.class );
|
||||
join = root.join( "neighbourPlaces" ).join( "livingRoom" );
|
||||
criteria.where( criteriaBuilder.equal( join.get( "owner" ), "his" ) );
|
||||
|
||||
assertNotNull(s.createQuery( criteria ).uniqueResult());
|
||||
|
||||
// assertNotNull( s.createCriteria( HousePlaces.class )
|
||||
// .add( Restrictions.eq( "neighbourPlaces.livingRoom.owner", "his" ) ).uniqueResult() );
|
||||
s.delete( house );
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* 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.onetomany;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.hibernate.NullPrecedence;
|
||||
import org.hibernate.dialect.H2Dialect;
|
||||
import org.hibernate.dialect.MySQLDialect;
|
||||
import org.hibernate.dialect.SQLServer2008Dialect;
|
||||
|
||||
import org.hibernate.testing.RequiresDialect;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil2.inSession;
|
||||
|
||||
/**
|
||||
* @author Andrea Boriero
|
||||
*/
|
||||
public class OerderByNullsFirstLastTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-465")
|
||||
@RequiresDialect(value = { H2Dialect.class, MySQLDialect.class, SQLServer2008Dialect.class },
|
||||
comment = "By default H2 places NULL values first, so testing 'NULLS LAST' expression. " +
|
||||
"For MySQL and SQL Server 2008 testing overridden Dialect#renderOrderByElement(String, String, String, NullPrecedence) method. " +
|
||||
"MySQL and SQL Server 2008 does not support NULLS FIRST / LAST syntax at the moment, so transforming the expression to 'CASE WHEN ...'.")
|
||||
public void testCriteriaNullsFirstLast() {
|
||||
inSession( session -> {
|
||||
try{
|
||||
// Populating database with test data.
|
||||
session.getTransaction().begin();
|
||||
Zoo zoo1 = new Zoo( null );
|
||||
Zoo zoo2 = new Zoo( "Warsaw ZOO" );
|
||||
session.persist( zoo1 );
|
||||
session.persist( zoo2 );
|
||||
session.getTransaction().commit();
|
||||
|
||||
session.clear();
|
||||
|
||||
session.getTransaction().begin();
|
||||
|
||||
Criteria criteria = session.createCriteria( Zoo.class );
|
||||
criteria.addOrder( org.hibernate.criterion.Order.asc( "name" ).nulls( NullPrecedence.LAST ) );
|
||||
Iterator<Zoo> iterator = (Iterator<Zoo>) criteria.list().iterator();
|
||||
|
||||
Assert.assertEquals( zoo2.getName(), iterator.next().getName() );
|
||||
Assert.assertNull( iterator.next().getName() );
|
||||
session.getTransaction().commit();
|
||||
|
||||
session.clear();
|
||||
|
||||
// Cleanup data.
|
||||
session.getTransaction().begin();
|
||||
session.delete( zoo1 );
|
||||
session.delete( zoo2 );
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
catch (Exception e){
|
||||
if(session.getTransaction().isActive()){
|
||||
session.getTransaction().rollback();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[] {
|
||||
Order.class, OrderItem.class, Zoo.class, Tiger.class,
|
||||
Monkey.class, Visitor.class, Box.class, Item.class,
|
||||
BankAccount.class, Transaction.class,
|
||||
Comment.class, Forum.class, Post.class, User.class,
|
||||
Asset.class, Computer.class, Employee.class,
|
||||
A.class, B.class, C.class
|
||||
};
|
||||
}
|
||||
}
|
|
@ -582,3 +582,9 @@ public class BaseNonConfigCoreFunctionalTestCase extends BaseUnitTestCase {
|
|||
return TransactionUtil2.fromTransaction( sessionFactory(), action );
|
||||
}
|
||||
}
|
||||
|
||||
public <R> R inTransactionReturn(Function<SessionImplementor, R> action) {
|
||||
log.trace( "#inTransaction(action)" );
|
||||
return TransactionUtil2.inTransactionReturn( sessionFactory(), action );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,6 +52,21 @@ public class TransactionUtil2 {
|
|||
}
|
||||
}
|
||||
|
||||
public static <R> R inSessionReturn(SessionFactoryImplementor sfi, Function<SessionImplementor,R> action) {
|
||||
log.trace( "#inSession(SF,action)" );
|
||||
|
||||
R result = null;
|
||||
try (SessionImplementor session = (SessionImplementor) sfi.openSession()) {
|
||||
log.trace( "Session opened, calling action" );
|
||||
result = action.apply( session );
|
||||
log.trace( "called action" );
|
||||
}
|
||||
finally {
|
||||
log.trace( "Session closed (AutoCloseable)" );
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public static void inTransaction(SessionFactoryImplementor factory, Consumer<SessionImplementor> action) {
|
||||
log.trace( "#inTransaction(factory, action)");
|
||||
|
@ -70,6 +85,65 @@ public class TransactionUtil2 {
|
|||
);
|
||||
}
|
||||
|
||||
public static <R> R inTransactionReturn(SessionFactoryImplementor factory, Function<SessionImplementor, R> action) {
|
||||
log.trace( "#inTransaction(factory, action)" );
|
||||
|
||||
return inSessionReturn(
|
||||
factory,
|
||||
session -> inTransactionReturn( session, action )
|
||||
);
|
||||
}
|
||||
|
||||
public static <R> R inTransactionReturn(SessionImplementor session, Function<SessionImplementor,R> action) {
|
||||
log.trace( "inTransaction(session,action)" );
|
||||
|
||||
final Transaction txn = session.beginTransaction();
|
||||
log.trace( "Started transaction" );
|
||||
R result = null;
|
||||
try {
|
||||
log.trace( "Calling action in txn" );
|
||||
result = action.apply( session );
|
||||
log.trace( "Called action - in txn" );
|
||||
|
||||
if ( !txn.isActive() ) {
|
||||
throw new TransactionManagementException( ACTION_COMPLETED_TXN );
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
// an error happened in the action
|
||||
if ( ! txn.isActive() ) {
|
||||
log.warn( ACTION_COMPLETED_TXN, e );
|
||||
}
|
||||
else {
|
||||
log.trace( "Rolling back transaction due to action error" );
|
||||
try {
|
||||
txn.rollback();
|
||||
log.trace( "Rolled back transaction due to action error" );
|
||||
}
|
||||
catch (Exception inner) {
|
||||
log.trace( "Rolling back transaction due to action error failed; throwing original error" );
|
||||
}
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
|
||||
// action completed with no errors - attempt to commit the transaction allowing
|
||||
// any RollbackException to propagate. Note that when we get here we know the
|
||||
// txn is active
|
||||
|
||||
log.trace( "Committing transaction after successful action execution" );
|
||||
try {
|
||||
txn.commit();
|
||||
log.trace( "Committing transaction after successful action execution - success" );
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.trace( "Committing transaction after successful action execution - failure" );
|
||||
throw e;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void inTransaction(SessionImplementor session, Consumer<SessionImplementor> action) {
|
||||
log.trace( "inTransaction(session,action)" );
|
||||
|
||||
|
|
Loading…
Reference in New Issue