Search in sources :

Example 1 with Car

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());
}
Also used : Car(org.hibernate.envers.test.integration.query.entities.Car) AuditReader(org.hibernate.envers.AuditReader) Test(org.junit.Test)

Example 2 with Car

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());
}
Also used : Car(org.hibernate.envers.test.integration.query.entities.Car) AuditReader(org.hibernate.envers.AuditReader) Test(org.junit.Test)

Example 3 with Car

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());
}
Also used : Car(org.hibernate.envers.test.integration.query.entities.Car) AuditReader(org.hibernate.envers.AuditReader) Test(org.junit.Test)

Example 4 with Car

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());
}
Also used : Car(org.hibernate.envers.test.integration.query.entities.Car) AuditReader(org.hibernate.envers.AuditReader) Test(org.junit.Test)

Example 5 with Car

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());
}
Also used : Car(org.hibernate.envers.test.integration.query.entities.Car) AuditReader(org.hibernate.envers.AuditReader) Test(org.junit.Test)

Aggregations

Car (org.hibernate.envers.test.integration.query.entities.Car)9 Test (org.junit.Test)9 AuditReader (org.hibernate.envers.AuditReader)7 EntityManager (javax.persistence.EntityManager)2 Priority (org.hibernate.envers.test.Priority)2 Address (org.hibernate.envers.test.integration.query.entities.Address)2 Person (org.hibernate.envers.test.integration.query.entities.Person)2