Search in sources :

Example 31 with Statement

use of java.sql.Statement in project hibernate-orm by hibernate.

the class CollectionLoaderTest method init.

@Before
public void init() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        session.doWork(connection -> {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate(String.format("ALTER TABLE person %s valid %s", getDialect().getAddColumnString(), getDialect().getTypeName(Types.BOOLEAN)));
                statement.executeUpdate(String.format("ALTER TABLE Person_phones %s valid %s", getDialect().getAddColumnString(), getDialect().getTypeName(Types.BOOLEAN)));
            }
        });
    });
}
Also used : Statement(java.sql.Statement) Session(org.hibernate.Session) Before(org.junit.Before)

Example 32 with Statement

use of java.sql.Statement in project hibernate-orm by hibernate.

the class CustomSQLSecondaryTableTest method init.

@Before
public void init() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        session.doWork(connection -> {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("ALTER TABLE person ADD COLUMN valid boolean");
                statement.executeUpdate("ALTER TABLE person_details ADD COLUMN valid boolean");
            }
        });
    });
}
Also used : Statement(java.sql.Statement) Session(org.hibernate.Session) Before(org.junit.Before)

Example 33 with Statement

use of java.sql.Statement in project hibernate-orm by hibernate.

the class DiscriminatorNotNullSingleTableTest method test.

@Test
public void test() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        entityManager.unwrap(Session.class).doWork(connection -> {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("ALTER TABLE Account ALTER COLUMN DTYPE SET NULL");
            }
        });
        DebitAccount debitAccount = new DebitAccount();
        debitAccount.setId(1L);
        debitAccount.setOwner("John Doe");
        debitAccount.setBalance(BigDecimal.valueOf(100));
        debitAccount.setInterestRate(BigDecimal.valueOf(1.5d));
        debitAccount.setOverdraftFee(BigDecimal.valueOf(25));
        CreditAccount creditAccount = new CreditAccount();
        creditAccount.setId(2L);
        creditAccount.setOwner("John Doe");
        creditAccount.setBalance(BigDecimal.valueOf(1000));
        creditAccount.setInterestRate(BigDecimal.valueOf(1.9d));
        creditAccount.setCreditLimit(BigDecimal.valueOf(5000));
        Account account = new Account();
        account.setId(3L);
        account.setOwner("John Doe");
        account.setBalance(BigDecimal.valueOf(1000));
        account.setInterestRate(BigDecimal.valueOf(1.9d));
        entityManager.persist(debitAccount);
        entityManager.persist(creditAccount);
        entityManager.persist(account);
        entityManager.unwrap(Session.class).doWork(connection -> {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("insert into Account (DTYPE, active, balance, interestRate, owner, id) " + "values ('Other', true, 25, 0.5, 'Vlad', 4)");
            }
        });
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Map<Long, Account> accounts = entityManager.createQuery("select a from Account a", Account.class).getResultList().stream().collect(Collectors.toMap(Account::getId, Function.identity()));
        assertEquals(4, accounts.size());
        assertEquals(DebitAccount.class, accounts.get(1L).getClass());
        assertEquals(CreditAccount.class, accounts.get(2L).getClass());
        assertEquals(Account.class, accounts.get(3L).getClass());
        assertEquals(OtherAccount.class, accounts.get(4L).getClass());
    });
}
Also used : Statement(java.sql.Statement) Session(org.hibernate.Session) Test(org.junit.Test)

Example 34 with Statement

use of java.sql.Statement in project hibernate-orm by hibernate.

the class DiscriminatorOptionsNotNullSingleTableTest method test.

@Test
public void test() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        entityManager.unwrap(Session.class).doWork(connection -> {
            try (Statement statement = connection.createStatement()) {
            }
        });
        DebitAccount debitAccount = new DebitAccount();
        debitAccount.setId(1L);
        debitAccount.setOwner("John Doe");
        debitAccount.setBalance(BigDecimal.valueOf(100));
        debitAccount.setInterestRate(BigDecimal.valueOf(1.5d));
        debitAccount.setOverdraftFee(BigDecimal.valueOf(25));
        CreditAccount creditAccount = new CreditAccount();
        creditAccount.setId(2L);
        creditAccount.setOwner("John Doe");
        creditAccount.setBalance(BigDecimal.valueOf(1000));
        creditAccount.setInterestRate(BigDecimal.valueOf(1.9d));
        creditAccount.setCreditLimit(BigDecimal.valueOf(5000));
        Account account = new Account();
        account.setId(3L);
        account.setOwner("John Doe");
        account.setBalance(BigDecimal.valueOf(1000));
        account.setInterestRate(BigDecimal.valueOf(1.9d));
        entityManager.persist(debitAccount);
        entityManager.persist(creditAccount);
        entityManager.persist(account);
        entityManager.unwrap(Session.class).doWork(connection -> {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("insert into Account (DTYPE, active, balance, interestRate, owner, id) " + "values ('Other', true, 25, 0.5, 'Vlad', 4)");
            }
        });
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Map<Long, Account> accounts = entityManager.createQuery("select a from Account a", Account.class).getResultList().stream().collect(Collectors.toMap(Account::getId, Function.identity()));
        assertEquals(4, accounts.size());
        assertEquals(DebitAccount.class, accounts.get(1L).getClass());
        assertEquals(CreditAccount.class, accounts.get(2L).getClass());
        assertEquals(Account.class, accounts.get(3L).getClass());
        assertEquals(OtherAccount.class, accounts.get(4L).getClass());
    });
}
Also used : Statement(java.sql.Statement) Session(org.hibernate.Session) Test(org.junit.Test)

Example 35 with Statement

use of java.sql.Statement in project hibernate-orm by hibernate.

the class JdbcCompatibilityTest method testJdbc42.

@Test
@TestForIssue(jiraKey = "HHH-11308")
public void testJdbc42() {
    doInHibernate(this::sessionFactory, session -> {
        for (int i = 0; i < 5; i++) {
            IrrelevantEntity entity = new IrrelevantEntity();
            entity.setName(getClass().getName());
            session.persist(entity);
        }
        session.flush();
        session.doWork(connection -> {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("DELETE FROM IrrelevantEntity");
                assertEquals(5, statement.getLargeUpdateCount());
            }
        });
    });
}
Also used : Statement(java.sql.Statement) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

Statement (java.sql.Statement)3054 Connection (java.sql.Connection)1634 ResultSet (java.sql.ResultSet)1631 SQLException (java.sql.SQLException)1529 PreparedStatement (java.sql.PreparedStatement)1329 Test (org.junit.Test)570 ArrayList (java.util.ArrayList)323 CallableStatement (java.sql.CallableStatement)135 ResultSetMetaData (java.sql.ResultSetMetaData)127 IOException (java.io.IOException)121 Properties (java.util.Properties)114 HashMap (java.util.HashMap)83 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)81 DruidPooledStatement (com.alibaba.druid.pool.DruidPooledStatement)71 StringPlus (mom.trd.opentheso.bdd.tools.StringPlus)63 DataSource (javax.sql.DataSource)62 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)61 Vector (java.util.Vector)61 DatabaseMetaData (java.sql.DatabaseMetaData)53 KeyValue (org.vcell.util.document.KeyValue)49