use of org.hibernate.envers.test.integration.query.entities.Car in project hibernate-orm by hibernate.
the class AssociationToOneInnerJoinQueryTest method testDisjunctionOfPropertiesFromDifferentEntities.
@Test
public void testDisjunctionOfPropertiesFromDifferentEntities() {
AuditReader auditReader = getAuditReader();
// all cars where the owner has an age of 20 or lives in an address with number 30.
List<Car> resultList = auditReader.createQuery().forEntitiesAtRevision(Car.class, 1).traverseRelation("owner", JoinType.INNER, "p").traverseRelation("address", JoinType.INNER, "a").up().up().add(AuditEntity.disjunction().add(AuditEntity.property("p", "age").eq(20)).add(AuditEntity.property("a", "number").eq(30))).addOrder(AuditEntity.property("make").asc()).getResultList();
assertEquals("Expected two cars to be returned, Toyota and VW", 2, resultList.size());
assertEquals("Unexpected car at index 0", toyota.getId(), resultList.get(0).getId());
assertEquals("Unexpected car at index 1", vw.getId(), resultList.get(1).getId());
}
use of org.hibernate.envers.test.integration.query.entities.Car in project hibernate-orm by hibernate.
the class AssociationToOneLeftJoinQueryTest method testLeftJoinOnAuditedEntity.
@Test
public void testLeftJoinOnAuditedEntity() {
final AuditReader auditReader = getAuditReader();
// all cars where the owner has an age of 20 or where there is no owner at all
List<Car> resultList = auditReader.createQuery().forEntitiesAtRevision(Car.class, 1).traverseRelation("owner", JoinType.LEFT, "p").up().add(AuditEntity.or(AuditEntity.property("p", "age").eq(20), AuditEntity.relatedId("owner").eq(null))).addOrder(AuditEntity.property("make").asc()).getResultList();
assertEquals("The result list should have 2 results, car1 because its owner has an age of 30 and car3 because it has no owner at all", 2, resultList.size());
Car car0 = resultList.get(0);
Car car1 = resultList.get(1);
assertEquals("Unexpected car at index 0", car2.getId(), car0.getId());
assertEquals("Unexpected car at index 0", car3.getId(), car1.getId());
}
use of org.hibernate.envers.test.integration.query.entities.Car in project hibernate-orm by hibernate.
the class AssociationToOneLeftJoinQueryTest method testEntitiesWithANullRelatedIdAreNotReturnedMoreThanOnce.
/**
* In a first attempt to implement left joins in Envers, a full join
* has been performed and than the entities has been filtered in the
* where clause. However, this approach did only work for inner joins
* but not for left joins. One of the defects in this approach is,
* that audit entities, which have a null 'relatedId' and do match
* the query criterias, have been returned multiple times by a query.
* This test ensures that this defect is no longer in the current implementation.
*/
@Test
public void testEntitiesWithANullRelatedIdAreNotReturnedMoreThanOnce() {
final AuditReader auditReader = getAuditReader();
List<Car> resultList = auditReader.createQuery().forEntitiesAtRevision(Car.class, 1).traverseRelation("owner", JoinType.LEFT, "p").up().add(AuditEntity.or(AuditEntity.property("make").eq("car3"), AuditEntity.property("p", "age").eq(10))).getResultList();
assertEquals("Expected car3 to be returned but only once", 1, resultList.size());
assertEquals("Unexpected car at index 0", car3.getId(), resultList.get(0).getId());
}
use of org.hibernate.envers.test.integration.query.entities.Car in project hibernate-orm by hibernate.
the class AssociationToOneInnerJoinQueryTest method testComparisonOfTwoPropertiesFromDifferentEntities.
@Test
public void testComparisonOfTwoPropertiesFromDifferentEntities() {
AuditReader auditReader = getAuditReader();
// the car where the owner age is equal to the owner address number.
Car result = (Car) auditReader.createQuery().forEntitiesAtRevision(Car.class, 1).traverseRelation("owner", JoinType.INNER, "p").traverseRelation("address", JoinType.INNER, "a").up().up().add(AuditEntity.property("p", "age").eqProperty("a", "number")).getSingleResult();
assertEquals("Unexpected car returned", toyota.getId(), result.getId());
}
use of org.hibernate.envers.test.integration.query.entities.Car in project hibernate-orm by hibernate.
the class AssociationToOneInnerJoinQueryTest method testAssociationQueryWithOrdering.
@Test
public void testAssociationQueryWithOrdering() {
AuditReader auditReader = getAuditReader();
List<Car> cars1 = auditReader.createQuery().forEntitiesAtRevision(Car.class, 1).traverseRelation("owner", JoinType.INNER).traverseRelation("address", JoinType.INNER).addOrder(AuditEntity.property("number").asc()).up().addOrder(AuditEntity.property("age").desc()).getResultList();
assertEquals("Unexpected number of results", 3, cars1.size());
assertEquals("Unexpected car at index 0", ford.getId(), cars1.get(0).getId());
assertEquals("Unexpected car at index 1", vw.getId(), cars1.get(1).getId());
assertEquals("Unexpected car at index 2", toyota.getId(), cars1.get(2).getId());
List<Car> cars2 = auditReader.createQuery().forEntitiesAtRevision(Car.class, 1).traverseRelation("owner", JoinType.INNER).traverseRelation("address", JoinType.INNER).addOrder(AuditEntity.property("number").asc()).up().addOrder(AuditEntity.property("age").asc()).getResultList();
assertEquals("Unexpected number of results", 3, cars2.size());
assertEquals("Unexpected car at index 0", vw.getId(), cars2.get(0).getId());
assertEquals("Unexpected car at index 1", ford.getId(), cars2.get(1).getId());
assertEquals("Unexpected car at index 2", toyota.getId(), cars2.get(2).getId());
}
Aggregations