Search in sources :

Example 56 with SkipForDialect

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

the class SQLExceptionConversionTest method testIntegrityViolation.

@Test
@SkipForDialect(value = { MySQLMyISAMDialect.class, AbstractHANADialect.class }, comment = "MySQL (MyISAM) / Hana do not support FK violation checking")
public void testIntegrityViolation() throws Exception {
    final Session session = openSession();
    session.beginTransaction();
    session.doWork(new Work() {

        @Override
        public void execute(Connection connection) throws SQLException {
            // Attempt to insert some bad values into the T_MEMBERSHIP table that should
            // result in a constraint violation
            PreparedStatement ps = null;
            try {
                ps = ((SessionImplementor) session).getJdbcCoordinator().getStatementPreparer().prepareStatement("INSERT INTO T_MEMBERSHIP (user_id, group_id) VALUES (?, ?)");
                // Non-existent user_id
                ps.setLong(1, 52134241);
                // Non-existent group_id
                ps.setLong(2, 5342);
                ((SessionImplementor) session).getJdbcCoordinator().getResultSetReturn().executeUpdate(ps);
                fail("INSERT should have failed");
            } catch (ConstraintViolationException ignore) {
            // expected outcome
            } finally {
                releaseStatement(session, ps);
            }
        }
    });
    session.getTransaction().rollback();
    session.close();
}
Also used : SQLException(java.sql.SQLException) Work(org.hibernate.jdbc.Work) Connection(java.sql.Connection) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) PreparedStatement(java.sql.PreparedStatement) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 57 with SkipForDialect

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

the class SequenceGeneratorTest method testStartOfSequence.

/**
	 * This seems a little trivial, but we need to guarantee that all Dialects start their sequences on a non-0 value.
	 */
@Test
@TestForIssue(jiraKey = "HHH-8814")
@RequiresDialectFeature(DialectChecks.SupportsSequences.class)
@SkipForDialect(value = SQLServer2012Dialect.class, comment = "SQLServer2012Dialect initializes sequence to minimum value (e.g., Long.MIN_VALUE; Hibernate assumes it is uninitialized.")
public void testStartOfSequence() throws Exception {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    final Person person = new Person();
    s.persist(person);
    tx.commit();
    s.close();
    assertTrue(person.getId() > 0);
    assertTrue(sqlStatementInterceptor.getSqlQueries().stream().filter(sql -> sql.contains("product_sequence")).findFirst().isPresent());
}
Also used : SkipForDialect(org.hibernate.testing.SkipForDialect) BaseNonConfigCoreFunctionalTestCase(org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature) Session(org.hibernate.Session) SessionFactoryBuilder(org.hibernate.boot.SessionFactoryBuilder) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) SQLServer2012Dialect(org.hibernate.dialect.SQLServer2012Dialect) Transaction(org.hibernate.Transaction) TestForIssue(org.hibernate.testing.TestForIssue) SQLStatementInterceptor(org.hibernate.test.util.jdbc.SQLStatementInterceptor) DialectChecks(org.hibernate.testing.DialectChecks) Map(java.util.Map) Environment(org.hibernate.cfg.Environment) Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature) TestForIssue(org.hibernate.testing.TestForIssue)

Example 58 with SkipForDialect

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

the class ScrollableCollectionFetchingTest method testScrollingJoinFetchesPositioning.

@Test
@SkipForDialect(value = CUBRIDDialect.class, comment = "As of verion 8.4.1 CUBRID doesn't support temporary tables. This test fails with" + "HibernateException: cannot doAfterTransactionCompletion multi-table deletes using dialect not supporting temp tables")
@SkipForDialect(value = AbstractHANADialect.class, comment = "HANA only supports forward-only cursors.")
public void testScrollingJoinFetchesPositioning() {
    TestData data = new TestData();
    data.prepare();
    Session s = openSession();
    Transaction txn = s.beginTransaction();
    ScrollableResults results = s.createQuery("from Animal a left join fetch a.offspring where a.description like :desc order by a.id").setString("desc", "root%").scroll();
    results.first();
    Animal animal = (Animal) results.get(0);
    assertEquals("first() did not return expected row", data.root1Id, animal.getId());
    results.scroll(1);
    animal = (Animal) results.get(0);
    assertEquals("scroll(1) did not return expected row", data.root2Id, animal.getId());
    results.scroll(-1);
    animal = (Animal) results.get(0);
    assertEquals("scroll(-1) did not return expected row", data.root1Id, animal.getId());
    results.setRowNumber(1);
    animal = (Animal) results.get(0);
    assertEquals("setRowNumber(1) did not return expected row", data.root1Id, animal.getId());
    results.setRowNumber(2);
    animal = (Animal) results.get(0);
    assertEquals("setRowNumber(2) did not return expected row", data.root2Id, animal.getId());
    txn.commit();
    s.close();
    data.cleanup();
}
Also used : Transaction(org.hibernate.Transaction) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 59 with SkipForDialect

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

the class ScrollableCollectionFetchingTest method testScrollingJoinFetchesSingleRowResultSet.

@Test
@SkipForDialect(value = CUBRIDDialect.class, comment = "As of verion 8.4.1 CUBRID doesn't support temporary tables. This test fails with" + "HibernateException: cannot doAfterTransactionCompletion multi-table deletes using dialect not supporting temp tables")
@SkipForDialect(value = AbstractHANADialect.class, comment = "HANA only supports forward-only cursors")
public void testScrollingJoinFetchesSingleRowResultSet() {
    Session s = openSession();
    Transaction txn = s.beginTransaction();
    Animal mother = new Animal();
    mother.setDescription("root-1");
    Animal daughter = new Animal();
    daughter.setDescription("daughter");
    daughter.setMother(mother);
    mother.addOffspring(daughter);
    s.save(mother);
    s.save(daughter);
    txn.commit();
    s.close();
    s = openSession();
    txn = s.beginTransaction();
    assertNotNull(s.createQuery("from Animal a left join fetch a.offspring where a.description like :desc order by a.id").setString("desc", "root%").uniqueResult());
    ScrollableResults results = s.createQuery("from Animal a left join fetch a.offspring where a.description like :desc order by a.id").setString("desc", "root%").scroll();
    assertFalse(results.isFirst());
    assertFalse(results.isLast());
    assertFalse(results.previous());
    assertTrue(results.next());
    assertTrue(results.isFirst());
    assertTrue(results.isLast());
    assertFalse(results.next());
    assertFalse(results.isFirst());
    assertFalse(results.isLast());
    assertTrue(results.previous());
    assertTrue(results.isFirst());
    assertTrue(results.isLast());
    assertFalse(results.previous());
    assertFalse(results.isFirst());
    assertFalse(results.isLast());
    assertTrue(results.next());
    assertTrue(results.isFirst());
    assertTrue(results.isLast());
    results.beforeFirst();
    assertFalse(results.isFirst());
    assertFalse(results.isLast());
    assertFalse(results.previous());
    assertTrue(results.first());
    assertTrue(results.isFirst());
    assertTrue(results.isLast());
    assertFalse(results.next());
    results.afterLast();
    assertFalse(results.isFirst());
    assertFalse(results.isLast());
    assertFalse(results.next());
    assertTrue(results.last());
    assertTrue(results.isFirst());
    assertTrue(results.isLast());
    assertFalse(results.next());
    assertTrue(results.first());
    assertTrue(results.isFirst());
    assertTrue(results.isLast());
    for (int i = 1; i < 3; i++) {
        assertTrue(results.setRowNumber(1));
        assertTrue(results.isFirst());
        assertTrue(results.isLast());
        assertFalse(results.scroll(i));
        assertFalse(results.isFirst());
        assertFalse(results.isLast());
        assertTrue(results.setRowNumber(1));
        assertTrue(results.isFirst());
        assertTrue(results.isLast());
        assertFalse(results.scroll(-i));
        assertFalse(results.isFirst());
        assertFalse(results.isLast());
        if (i != 1) {
            assertFalse(results.setRowNumber(i));
            assertFalse(results.isFirst());
            assertFalse(results.isLast());
            assertFalse(results.setRowNumber(-i));
            assertFalse(results.isFirst());
            assertFalse(results.isLast());
        }
    }
    txn.commit();
    s.close();
    s = openSession();
    txn = s.beginTransaction();
    s.createQuery("delete Animal where not description like 'root%'").executeUpdate();
    s.createQuery("delete Animal").executeUpdate();
    txn.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 60 with SkipForDialect

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

the class ClobLocatorTest method testBoundedClobLocatorAccess.

@Test
@SkipForDialect(value = TeradataDialect.class, jiraKey = "HHH-6637", comment = "Teradata requires locator to be used in same session where it was created/retrieved")
public void testBoundedClobLocatorAccess() throws Throwable {
    String original = buildString(CLOB_SIZE, 'x');
    String changed = buildString(CLOB_SIZE, 'y');
    String empty = "";
    Session s = openSession();
    s.beginTransaction();
    LobHolder entity = new LobHolder();
    entity.setClobLocator(s.getLobHelper().createClob(original));
    s.save(entity);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    entity = s.get(LobHolder.class, entity.getId());
    assertEquals(CLOB_SIZE, entity.getClobLocator().length());
    assertEquals(original, extractData(entity.getClobLocator()));
    s.getTransaction().commit();
    s.close();
    // test mutation via setting the new clob data...
    if (getDialect().supportsLobValueChangePropogation()) {
        s = openSession();
        s.beginTransaction();
        entity = (LobHolder) s.byId(LobHolder.class).with(LockOptions.UPGRADE).load(entity.getId());
        entity.getClobLocator().truncate(1);
        entity.getClobLocator().setString(1, changed);
        s.getTransaction().commit();
        s.close();
        s = openSession();
        s.beginTransaction();
        entity = (LobHolder) s.byId(LobHolder.class).with(LockOptions.UPGRADE).load(entity.getId());
        assertNotNull(entity.getClobLocator());
        assertEquals(CLOB_SIZE, entity.getClobLocator().length());
        assertEquals(changed, extractData(entity.getClobLocator()));
        entity.getClobLocator().truncate(1);
        entity.getClobLocator().setString(1, original);
        s.getTransaction().commit();
        s.close();
    }
    // test mutation via supplying a new clob locator instance...
    s = openSession();
    s.beginTransaction();
    entity = (LobHolder) s.byId(LobHolder.class).with(LockOptions.UPGRADE).load(entity.getId());
    assertNotNull(entity.getClobLocator());
    assertEquals(CLOB_SIZE, entity.getClobLocator().length());
    assertEquals(original, extractData(entity.getClobLocator()));
    entity.setClobLocator(s.getLobHelper().createClob(changed));
    s.getTransaction().commit();
    s.close();
    // test empty clob
    if (!(getDialect() instanceof SybaseASE157Dialect)) {
        // Skip for Sybase. HHH-6425
        s = openSession();
        s.beginTransaction();
        entity = s.get(LobHolder.class, entity.getId());
        assertEquals(CLOB_SIZE, entity.getClobLocator().length());
        assertEquals(changed, extractData(entity.getClobLocator()));
        entity.setClobLocator(s.getLobHelper().createClob(empty));
        s.getTransaction().commit();
        s.close();
        s = openSession();
        s.beginTransaction();
        entity = s.get(LobHolder.class, entity.getId());
        if (entity.getClobLocator() != null) {
            assertEquals(empty.length(), entity.getClobLocator().length());
            assertEquals(empty, extractData(entity.getClobLocator()));
        }
        s.delete(entity);
        s.getTransaction().commit();
        s.close();
    }
}
Also used : SybaseASE157Dialect(org.hibernate.dialect.SybaseASE157Dialect) 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