Search in sources :

Example 46 with SkipForDialect

use of org.hibernate.testing.SkipForDialect in project hibernate-orm by hibernate.

the class OneToOneLinkTest method testOneToOneViaAssociationTable.

@Test
@SkipForDialect(value = Oracle10gDialect.class, comment = "oracle12c returns time in getDate.  For now, skip.")
public void testOneToOneViaAssociationTable() {
    Person p = new Person();
    p.setName("Gavin King");
    p.setDob(new Date());
    Employee e = new Employee();
    p.setEmployee(e);
    e.setPerson(p);
    Session s = openSession();
    Transaction t = s.beginTransaction();
    s.persist(p);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    e = (Employee) s.createQuery("from Employee e where e.person.name like 'Gavin%'").uniqueResult();
    assertEquals(e.getPerson().getName(), "Gavin King");
    assertFalse(Hibernate.isInitialized(e.getPerson()));
    assertNull(e.getPerson().getCustomer());
    s.clear();
    e = (Employee) s.createQuery("from Employee e where e.person.dob = :date").setDate("date", new Date()).uniqueResult();
    assertEquals(e.getPerson().getName(), "Gavin King");
    assertFalse(Hibernate.isInitialized(e.getPerson()));
    assertNull(e.getPerson().getCustomer());
    s.clear();
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    e = (Employee) s.createQuery("from Employee e join fetch e.person p left join fetch p.customer").uniqueResult();
    assertTrue(Hibernate.isInitialized(e.getPerson()));
    assertNull(e.getPerson().getCustomer());
    Customer c = new Customer();
    e.getPerson().setCustomer(c);
    c.setPerson(e.getPerson());
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    e = (Employee) s.createQuery("from Employee e join fetch e.person p left join fetch p.customer").uniqueResult();
    assertTrue(Hibernate.isInitialized(e.getPerson()));
    assertTrue(Hibernate.isInitialized(e.getPerson().getCustomer()));
    assertNotNull(e.getPerson().getCustomer());
    s.delete(e);
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Date(java.util.Date) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 47 with SkipForDialect

use of org.hibernate.testing.SkipForDialect in project hibernate-orm by hibernate.

the class UnionSubclassTest method testCustomColumnReadAndWrite.

@Test
@SkipForDialect(value = TeradataDialect.class, jiraKey = "HHH-8190", comment = "SQL uses Teradata reserved word: title")
public void testCustomColumnReadAndWrite() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    final double HEIGHT_INCHES = 73;
    final double HEIGHT_CENTIMETERS = HEIGHT_INCHES * 2.54d;
    Person p = new Person();
    p.setName("Emmanuel");
    p.setSex('M');
    p.setHeightInches(HEIGHT_INCHES);
    s.persist(p);
    final double PASSWORD_EXPIRY_WEEKS = 4;
    final double PASSWORD_EXPIRY_DAYS = PASSWORD_EXPIRY_WEEKS * 7d;
    Employee e = new Employee();
    e.setName("Steve");
    e.setSex('M');
    e.setTitle("Mr");
    e.setPasswordExpiryDays(PASSWORD_EXPIRY_DAYS);
    s.persist(e);
    s.flush();
    // Test value conversion during insert
    // Value returned by Oracle native query is a Types.NUMERIC, which is mapped to a BigDecimalType;
    // Cast returned value to Number then call Number.doubleValue() so it works on all dialects.
    Double heightViaSql = ((Number) s.createSQLQuery("select height_centimeters from UPerson where name='Emmanuel'").uniqueResult()).doubleValue();
    assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d);
    Double expiryViaSql = ((Number) s.createSQLQuery("select pwd_expiry_weeks from UEmployee where person_id=?").setLong(0, e.getId()).uniqueResult()).doubleValue();
    assertEquals(PASSWORD_EXPIRY_WEEKS, expiryViaSql, 0.01d);
    // Test projection
    Double heightViaHql = (Double) s.createQuery("select p.heightInches from Person p where p.name = 'Emmanuel'").uniqueResult();
    assertEquals(HEIGHT_INCHES, heightViaHql, 0.01d);
    Double expiryViaHql = (Double) s.createQuery("select e.passwordExpiryDays from Employee e where e.name = 'Steve'").uniqueResult();
    assertEquals(PASSWORD_EXPIRY_DAYS, expiryViaHql, 0.01d);
    // Test restriction and entity load via criteria
    p = (Person) s.createCriteria(Person.class).add(Restrictions.between("heightInches", HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d)).uniqueResult();
    assertEquals(HEIGHT_INCHES, p.getHeightInches(), 0.01d);
    e = (Employee) s.createCriteria(Employee.class).add(Restrictions.between("passwordExpiryDays", PASSWORD_EXPIRY_DAYS - 0.01d, PASSWORD_EXPIRY_DAYS + 0.01d)).uniqueResult();
    assertEquals(PASSWORD_EXPIRY_DAYS, e.getPasswordExpiryDays(), 0.01d);
    // Test predicate and entity load via HQL
    p = (Person) s.createQuery("from Person p where p.heightInches between ? and ?").setDouble(0, HEIGHT_INCHES - 0.01d).setDouble(1, HEIGHT_INCHES + 0.01d).uniqueResult();
    assertEquals(HEIGHT_INCHES, p.getHeightInches(), 0.01d);
    e = (Employee) s.createQuery("from Employee e where e.passwordExpiryDays between ? and ?").setDouble(0, PASSWORD_EXPIRY_DAYS - 0.01d).setDouble(1, PASSWORD_EXPIRY_DAYS + 0.01d).uniqueResult();
    assertEquals(PASSWORD_EXPIRY_DAYS, e.getPasswordExpiryDays(), 0.01d);
    // Test update
    p.setHeightInches(1);
    e.setPasswordExpiryDays(7);
    s.flush();
    heightViaSql = ((Number) s.createSQLQuery("select height_centimeters from UPerson where name='Emmanuel'").uniqueResult()).doubleValue();
    assertEquals(2.54d, heightViaSql, 0.01d);
    expiryViaSql = ((Number) s.createSQLQuery("select pwd_expiry_weeks from UEmployee where person_id=?").setLong(0, e.getId()).uniqueResult()).doubleValue();
    assertEquals(1d, expiryViaSql, 0.01d);
    s.delete(p);
    s.delete(e);
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 48 with SkipForDialect

use of org.hibernate.testing.SkipForDialect in project hibernate-orm by hibernate.

the class UnionSubclassTest method testUnionSubclass.

@Test
@SkipForDialect(value = TeradataDialect.class, jiraKey = "HHH-8190", comment = "SQL uses Teradata reserved word: title")
public void testUnionSubclass() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Employee mark = new Employee();
    mark.setName("Mark");
    mark.setTitle("internal sales");
    mark.setSex('M');
    mark.setAddress("buckhead");
    mark.setZip("30305");
    mark.setCountry("USA");
    Customer joe = new Customer();
    joe.setName("Joe");
    joe.setAddress("San Francisco");
    joe.setZip("XXXXX");
    joe.setCountry("USA");
    joe.setComments("Very demanding");
    joe.setSex('M');
    joe.setSalesperson(mark);
    Person yomomma = new Person();
    yomomma.setName("mum");
    yomomma.setSex('F');
    s.save(yomomma);
    s.save(mark);
    s.save(joe);
    assertEquals(s.createQuery("from java.io.Serializable").list().size(), 0);
    assertEquals(s.createQuery("from Person").list().size(), 3);
    assertEquals(s.createQuery("from Person p where p.class = Customer").list().size(), 1);
    assertEquals(s.createQuery("from Person p where p.class = Person").list().size(), 1);
    assertEquals(s.createQuery("from Person p where type(p) in :who").setParameter("who", Customer.class).list().size(), 1);
    assertEquals(s.createQuery("from Person p where type(p) in :who").setParameterList("who", new Class[] { Customer.class, Person.class }).list().size(), 2);
    s.clear();
    List customers = s.createQuery("from Customer c left join fetch c.salesperson").list();
    for (Object customer : customers) {
        Customer c = (Customer) customer;
        assertTrue(Hibernate.isInitialized(c.getSalesperson()));
        assertEquals(c.getSalesperson().getName(), "Mark");
    }
    assertEquals(customers.size(), 1);
    s.clear();
    customers = s.createQuery("from Customer").list();
    for (Object customer : customers) {
        Customer c = (Customer) customer;
        assertFalse(Hibernate.isInitialized(c.getSalesperson()));
        assertEquals(c.getSalesperson().getName(), "Mark");
    }
    assertEquals(customers.size(), 1);
    s.clear();
    mark = (Employee) s.get(Employee.class, Long.valueOf(mark.getId()));
    joe = (Customer) s.get(Customer.class, Long.valueOf(joe.getId()));
    mark.setZip("30306");
    assertEquals(s.createQuery("from Person p where p.address.zip = '30306'").list().size(), 1);
    s.createCriteria(Person.class).add(Restrictions.in("address", new Address[] { mark.getAddress(), joe.getAddress() })).list();
    s.delete(mark);
    s.delete(joe);
    s.delete(yomomma);
    assertTrue(s.createQuery("from Person").list().isEmpty());
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 49 with SkipForDialect

use of org.hibernate.testing.SkipForDialect in project hibernate-orm by hibernate.

the class NativeSQLQueriesTest method testImageTypeInSQLQuery.

@SkipForDialect(value = AbstractHANADialect.class, comment = "On HANA, this returns a blob for the image column which doesn't get mapped to a byte[]")
@Test
public void testImageTypeInSQLQuery() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    byte[] photo = buildLongByteArray(15000, true);
    ImageHolder holder = new ImageHolder(photo);
    s.persist(holder);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    byte[] photoRead = (byte[]) s.createSQLQuery(getPhotosSQL()).uniqueResult();
    assertTrue(Arrays.equals(photo, photoRead));
    s.delete(holder);
    t.commit();
    s.close();
}
Also used : ImageHolder(org.hibernate.test.sql.hand.ImageHolder) Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 50 with SkipForDialect

use of org.hibernate.testing.SkipForDialect in project hibernate-orm by hibernate.

the class NativeSQLQueriesTest method testFailOnNoAddEntityOrScalar.

@Test
@SkipForDialect(H2Dialect.class)
public void testFailOnNoAddEntityOrScalar() {
    // Note: this passes, but for the wrong reason.
    //      there is actually an exception thrown, but it is the database
    //      throwing a sql exception because the SQL gets passed
    //      "un-processed"...
    //
    // Oddly, H2 accepts this query.
    Session s = openSession();
    s.beginTransaction();
    try {
        String sql = "select {org.*} " + "from organization org";
        s.createSQLQuery(sql).list();
        fail("Should throw an exception since no addEntity nor addScalar has been performed.");
    } catch (PersistenceException pe) {
    // expected behavior
    } finally {
        s.getTransaction().rollback();
        s.close();
    }
}
Also used : PersistenceException(javax.persistence.PersistenceException) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Aggregations

SkipForDialect (org.hibernate.testing.SkipForDialect)87 Test (org.junit.Test)86 Session (org.hibernate.Session)66 Transaction (org.hibernate.Transaction)36 List (java.util.List)26 ArrayList (java.util.ArrayList)20 TestForIssue (org.hibernate.testing.TestForIssue)16 EntityManager (javax.persistence.EntityManager)15 Query (javax.persistence.Query)11 Item (org.hibernate.jpa.test.Item)10 Query (org.hibernate.Query)7 Parameter (javax.persistence.Parameter)6 BigDecimal (java.math.BigDecimal)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 SQLQuery (org.hibernate.SQLQuery)4 ScrollableResults (org.hibernate.ScrollableResults)4 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 Date (java.util.Date)3 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)3