Search in sources :

Example 1 with ResultSetReturn

use of org.hibernate.engine.jdbc.spi.ResultSetReturn in project hibernate-orm by hibernate.

the class SQLExceptionConversionTest method testNotNullConstraint.

@Test
@TestForIssue(jiraKey = "HHH-7357")
public void testNotNullConstraint() {
    final Session session = openSession();
    session.beginTransaction();
    final User user = new User();
    user.setUsername("Lukasz");
    session.save(user);
    session.flush();
    session.doWork(new Work() {

        @Override
        public void execute(Connection connection) throws SQLException {
            final JdbcCoordinator jdbcCoordinator = ((SessionImplementor) session).getJdbcCoordinator();
            final StatementPreparer statementPreparer = jdbcCoordinator.getStatementPreparer();
            final ResultSetReturn resultSetReturn = jdbcCoordinator.getResultSetReturn();
            PreparedStatement ps = null;
            try {
                ps = statementPreparer.prepareStatement("UPDATE T_USER SET user_name = ? WHERE user_id = ?");
                // Attempt to update user name to NULL (NOT NULL constraint defined).
                ps.setNull(1, Types.VARCHAR);
                ps.setLong(2, user.getId());
                resultSetReturn.executeUpdate(ps);
                fail("UPDATE should have failed because of not NULL constraint.");
            } catch (ConstraintViolationException ignore) {
            // expected outcome
            } finally {
                releaseStatement(session, ps);
            }
        }
    });
    session.getTransaction().rollback();
    session.close();
}
Also used : ResultSetReturn(org.hibernate.engine.jdbc.spi.ResultSetReturn) JdbcCoordinator(org.hibernate.engine.jdbc.spi.JdbcCoordinator) SQLException(java.sql.SQLException) Work(org.hibernate.jdbc.Work) Connection(java.sql.Connection) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) PreparedStatement(java.sql.PreparedStatement) StatementPreparer(org.hibernate.engine.jdbc.spi.StatementPreparer) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with ResultSetReturn

use of org.hibernate.engine.jdbc.spi.ResultSetReturn in project hibernate-orm by hibernate.

the class CursorFromCallableTest method executeStatement.

private void executeStatement(final String sql) {
    final Session session = openSession();
    session.getTransaction().begin();
    session.doWork(new Work() {

        @Override
        public void execute(Connection connection) throws SQLException {
            final JdbcCoordinator jdbcCoordinator = ((SessionImplementor) session).getJdbcCoordinator();
            final StatementPreparer statementPreparer = jdbcCoordinator.getStatementPreparer();
            final ResultSetReturn resultSetReturn = jdbcCoordinator.getResultSetReturn();
            PreparedStatement preparedStatement = null;
            try {
                preparedStatement = statementPreparer.prepareStatement(sql);
                resultSetReturn.execute(preparedStatement);
            } finally {
                if (preparedStatement != null) {
                    try {
                        jdbcCoordinator.getResourceRegistry().release(preparedStatement);
                    } catch (Throwable ignore) {
                    // ignore...
                    }
                }
            }
        }
    });
    session.getTransaction().commit();
    session.close();
}
Also used : ResultSetReturn(org.hibernate.engine.jdbc.spi.ResultSetReturn) JdbcCoordinator(org.hibernate.engine.jdbc.spi.JdbcCoordinator) SQLException(java.sql.SQLException) Work(org.hibernate.jdbc.Work) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) StatementPreparer(org.hibernate.engine.jdbc.spi.StatementPreparer) Session(org.hibernate.Session)

Aggregations

Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 Session (org.hibernate.Session)2 JdbcCoordinator (org.hibernate.engine.jdbc.spi.JdbcCoordinator)2 ResultSetReturn (org.hibernate.engine.jdbc.spi.ResultSetReturn)2 StatementPreparer (org.hibernate.engine.jdbc.spi.StatementPreparer)2 Work (org.hibernate.jdbc.Work)2 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)1 TestForIssue (org.hibernate.testing.TestForIssue)1 Test (org.junit.Test)1