Search in sources :

Example 1 with Address

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

the class AssociationToOneInnerJoinQueryTest method testAssociationQueryWithProjection.

@Test
public void testAssociationQueryWithProjection() {
    AuditReader auditReader = getAuditReader();
    List<Integer> list1 = auditReader.createQuery().forEntitiesAtRevision(Car.class, 2).traverseRelation("owner", JoinType.INNER).addProjection(AuditEntity.property("age")).addOrder(AuditEntity.property("age").asc()).getResultList();
    assertEquals("Unexpected number of results", 3, list1.size());
    assertEquals("Unexpected age at index 0", Integer.valueOf(20), list1.get(0));
    assertEquals("Unexpected age at index 0", Integer.valueOf(30), list1.get(1));
    assertEquals("Unexpected age at index 0", Integer.valueOf(40), list1.get(2));
    List<Address> list2 = auditReader.createQuery().forEntitiesAtRevision(Car.class, 2).traverseRelation("owner", JoinType.INNER).addOrder(AuditEntity.property("age").asc()).traverseRelation("address", JoinType.INNER).addProjection(AuditEntity.selectEntity(false)).getResultList();
    assertEquals("Unexpected number of results", 3, list2.size());
    assertEquals("Unexpected address at index 0", address1.getId(), list2.get(0).getId());
    assertEquals("Unexpected address at index 1", address1.getId(), list2.get(1).getId());
    assertEquals("Unexpected address at index 2", address2.getId(), list2.get(2).getId());
    List<Address> list3 = auditReader.createQuery().forEntitiesAtRevision(Car.class, 2).traverseRelation("owner", JoinType.INNER).traverseRelation("address", JoinType.INNER).addProjection(AuditEntity.selectEntity(true)).addOrder(AuditEntity.property("number").asc()).getResultList();
    assertEquals("Unexpected number of results", 2, list3.size());
    assertEquals("Unexpected address at index 0", address1.getId(), list3.get(0).getId());
    assertEquals("Unexpected address at index 1", address2.getId(), list3.get(1).getId());
    List<Object[]> list4 = auditReader.createQuery().forEntitiesAtRevision(Car.class, 2).traverseRelation("owner", JoinType.INNER).addOrder(AuditEntity.property("age").asc()).addProjection(AuditEntity.selectEntity(false)).traverseRelation("address", JoinType.INNER).addProjection(AuditEntity.property("number")).getResultList();
    assertEquals("Unexpected number of results", 3, list4.size());
    final Object[] index0 = list4.get(0);
    assertEquals("Unexpected owner at index 0", vwOwner.getId(), ((Person) index0[0]).getId());
    assertEquals("Unexpected number at index 0", Integer.valueOf(5), index0[1]);
    final Object[] index1 = list4.get(1);
    assertEquals("Unexpected owner at index 1", fordOwner.getId(), ((Person) index1[0]).getId());
    assertEquals("Unexpected number at index 1", Integer.valueOf(5), index1[1]);
    final Object[] index2 = list4.get(2);
    assertEquals("Unexpected owner at index 2", toyotaOwner.getId(), ((Person) index2[0]).getId());
    assertEquals("Unexpected number at index 2", Integer.valueOf(30), index2[1]);
}
Also used : Address(org.hibernate.envers.test.integration.query.entities.Address) Car(org.hibernate.envers.test.integration.query.entities.Car) AuditReader(org.hibernate.envers.AuditReader) Test(org.junit.Test)

Example 2 with Address

use of org.hibernate.envers.test.integration.query.entities.Address 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 3 with Address

use of org.hibernate.envers.test.integration.query.entities.Address 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)

Aggregations

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