Search in sources :

Example 6 with Car

use of org.hibernate.envers.test.integration.query.entities.Car in project hibernate-orm by hibernate.

the class AssociationToOneInnerJoinQueryTest method initData.

@Test
@Priority(10)
public void initData() {
    EntityManager em = getEntityManager();
    // revision 1
    em.getTransaction().begin();
    address1 = new Address("Freiburgerstrasse", 5);
    em.persist(address1);
    address2 = new Address("Hindenburgstrasse", 30);
    em.persist(address2);
    vwOwner = new Person("VW owner", 20, address1);
    em.persist(vwOwner);
    fordOwner = new Person("Ford owner", 30, address1);
    em.persist(fordOwner);
    toyotaOwner = new Person("Toyota owner", 30, address2);
    em.persist(toyotaOwner);
    final Person nonOwner = new Person("NonOwner", 30, address1);
    em.persist(nonOwner);
    vw = new Car("VW");
    vw.setOwner(vwOwner);
    em.persist(vw);
    ford = new Car("Ford");
    ford.setOwner(fordOwner);
    em.persist(ford);
    toyota = new Car("Toyota");
    toyota.setOwner(toyotaOwner);
    em.persist(toyota);
    em.getTransaction().commit();
    // revision 2
    em.getTransaction().begin();
    toyotaOwner.setAge(40);
    em.getTransaction().commit();
}
Also used : EntityManager(javax.persistence.EntityManager) Address(org.hibernate.envers.test.integration.query.entities.Address) Car(org.hibernate.envers.test.integration.query.entities.Car) Person(org.hibernate.envers.test.integration.query.entities.Person) Test(org.junit.Test) Priority(org.hibernate.envers.test.Priority)

Example 7 with Car

use of org.hibernate.envers.test.integration.query.entities.Car in project hibernate-orm by hibernate.

the class AssociationToOneInnerJoinQueryTest method testAssociationQuery.

@Test
public void testAssociationQuery() {
    final AuditReader auditReader = getAuditReader();
    final Car result1 = (Car) auditReader.createQuery().forEntitiesAtRevision(Car.class, 1).traverseRelation("owner", JoinType.INNER).add(AuditEntity.property("name").like("Ford%")).getSingleResult();
    assertEquals("Unexpected single car at revision 1", ford.getId(), result1.getId());
    Car result2 = (Car) auditReader.createQuery().forEntitiesAtRevision(Car.class, 1).traverseRelation("owner", JoinType.INNER).traverseRelation("address", JoinType.INNER).add(AuditEntity.property("number").eq(30)).getSingleResult();
    assertEquals("Unexpected single car at revision 1", toyota.getId(), result2.getId());
    List<Car> resultList1 = auditReader.createQuery().forEntitiesAtRevision(Car.class, 1).traverseRelation("owner", JoinType.INNER).add(AuditEntity.property("age").ge(30)).add(AuditEntity.property("age").lt(40)).up().addOrder(AuditEntity.property("make").asc()).getResultList();
    assertEquals("Unexpected number of cars for query in revision 1", 2, resultList1.size());
    assertEquals("Unexpected car at index 0 in revision 1", ford.getId(), resultList1.get(0).getId());
    assertEquals("Unexpected car at index 1 in revision 2", toyota.getId(), resultList1.get(1).getId());
    Car result3 = (Car) auditReader.createQuery().forEntitiesAtRevision(Car.class, 2).traverseRelation("owner", JoinType.INNER).add(AuditEntity.property("age").ge(30)).add(AuditEntity.property("age").lt(40)).up().addOrder(AuditEntity.property("make").asc()).getSingleResult();
    assertEquals("Unexpected car at revision 2", ford.getId(), result3.getId());
}
Also used : Car(org.hibernate.envers.test.integration.query.entities.Car) AuditReader(org.hibernate.envers.AuditReader) Test(org.junit.Test)

Example 8 with Car

use of org.hibernate.envers.test.integration.query.entities.Car in project hibernate-orm by hibernate.

the class AssociationToOneLeftJoinQueryTest method initData.

@Test
@Priority(10)
public void initData() {
    final EntityManager em = getEntityManager();
    // revision 1
    em.getTransaction().begin();
    address1 = new Address("address1", 1);
    em.persist(address1);
    Address address2 = new Address("address2", 2);
    em.persist(address2);
    person1 = new Person("person1", 30, address1);
    em.persist(person1);
    person2 = new Person("person2", 20, null);
    em.persist(person2);
    Person person3 = new Person("person3", 10, address1);
    em.persist(person3);
    car1 = new Car("car1");
    car1.setOwner(person1);
    em.persist(car1);
    car2 = new Car("car2");
    car2.setOwner(person2);
    em.persist(car2);
    car3 = new Car("car3");
    em.persist(car3);
    em.getTransaction().commit();
    // revision 2
    em.getTransaction().begin();
    person2.setAge(21);
    em.getTransaction().commit();
}
Also used : EntityManager(javax.persistence.EntityManager) Address(org.hibernate.envers.test.integration.query.entities.Address) Car(org.hibernate.envers.test.integration.query.entities.Car) Person(org.hibernate.envers.test.integration.query.entities.Person) Test(org.junit.Test) Priority(org.hibernate.envers.test.Priority)

Example 9 with Car

use of org.hibernate.envers.test.integration.query.entities.Car in project hibernate-orm by hibernate.

the class AssociationToOneLeftJoinQueryTest method testEntitiesWithANullRelatedIdAreNotJoinedToOtherEntities.

/**
	 * 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' are and do not
	 * match the query criterias, still joined to other entities which matched
	 * match the query criterias.
	 * This test ensures that this defect is no longer in the current implementation.
	 */
@Test
public void testEntitiesWithANullRelatedIdAreNotJoinedToOtherEntities() {
    final AuditReader auditReader = getAuditReader();
    List<Car> resultList = auditReader.createQuery().forEntitiesAtRevision(Car.class, 1).traverseRelation("owner", JoinType.LEFT, "p").up().add(AuditEntity.and(AuditEntity.property("make").eq("car3"), AuditEntity.property("p", "age").eq(30))).getResultList();
    assertTrue("Expected no cars to be returned, because car3 does not have an owner", resultList.isEmpty());
}
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