use of java.sql.Statement in project hibernate-orm by hibernate.
the class OracleSQLCallableStatementProxyTest method init.
@Before
public void init() {
doInHibernate(this::sessionFactory, session -> {
session.doWork(connection -> {
Statement statement = null;
try {
statement = connection.createStatement();
statement.executeUpdate("CREATE OR REPLACE FUNCTION fn_person ( " + " personId IN NUMBER) " + " RETURN SYS_REFCURSOR " + "IS " + " persons SYS_REFCURSOR; " + "BEGIN " + " OPEN persons FOR " + " SELECT " + " p.id AS \"p.id\", " + " p.name AS \"p.name\", " + " p.nickName AS \"p.nickName\" " + " FROM person p " + " WHERE p.id = personId; " + " RETURN persons; " + "END;");
} catch (SQLException ignore) {
} finally {
if (statement != null) {
statement.close();
}
}
});
});
doInHibernate(this::sessionFactory, session -> {
Person person1 = new Person();
person1.setId(1L);
person1.setName("John Doe");
person1.setNickName("JD");
session.persist(person1);
});
}
use of java.sql.Statement in project hibernate-orm by hibernate.
the class CustomSQLTest 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_phones ADD COLUMN valid boolean");
}
});
});
}
use of java.sql.Statement in project hibernate-orm by hibernate.
the class MySQLStoredProcedureTest method destroy.
@After
public void destroy() {
doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> {
try (Statement statement = connection.createStatement()) {
statement.executeUpdate("DROP PROCEDURE IF EXISTS sp_count_phones");
} catch (SQLException ignore) {
}
});
});
doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> {
try (Statement statement = connection.createStatement()) {
statement.executeUpdate("DROP PROCEDURE IF EXISTS sp_phones");
} catch (SQLException ignore) {
}
});
});
doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> {
try (Statement statement = connection.createStatement()) {
statement.executeUpdate("DROP FUNCTION IF EXISTS fn_count_phones");
} catch (SQLException ignore) {
}
});
});
}
use of java.sql.Statement in project hibernate-orm by hibernate.
the class OracleCustomSQLWithStoredProcedureTest 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 valid NUMBER(1) DEFAULT 0 NOT NULL");
statement.executeUpdate("CREATE OR REPLACE PROCEDURE sp_delete_person ( " + " personId IN NUMBER ) " + "AS " + "BEGIN " + " UPDATE person SET valid = 0 WHERE id = personId; " + "END;");
}
});
});
}
use of java.sql.Statement in project hibernate-orm by hibernate.
the class OracleStoredProcedureTest 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("CREATE OR REPLACE PROCEDURE sp_count_phones ( " + " personId IN NUMBER, " + " phoneCount OUT NUMBER ) " + "AS " + "BEGIN " + " SELECT COUNT(*) INTO phoneCount " + " FROM phone " + " WHERE person_id = personId; " + "END;");
statement.executeUpdate("CREATE OR REPLACE PROCEDURE sp_person_phones ( " + " personId IN NUMBER, " + " personPhones OUT SYS_REFCURSOR ) " + "AS " + "BEGIN " + " OPEN personPhones FOR " + " SELECT *" + " FROM phone " + " WHERE person_id = personId; " + "END;");
statement.executeUpdate("CREATE OR REPLACE FUNCTION fn_count_phones ( " + " personId IN NUMBER ) " + " RETURN NUMBER " + "IS " + " phoneCount NUMBER; " + "BEGIN " + " SELECT COUNT(*) INTO phoneCount " + " FROM phone " + " WHERE person_id = personId; " + " RETURN( phoneCount ); " + "END;");
}
});
});
doInJPA(this::entityManagerFactory, entityManager -> {
Person person1 = new Person("John Doe");
person1.setNickName("JD");
person1.setAddress("Earth");
person1.setCreatedOn(Timestamp.from(LocalDateTime.of(2000, 1, 1, 0, 0, 0).toInstant(ZoneOffset.UTC)));
person1.getAddresses().put(AddressType.HOME, "Home address");
person1.getAddresses().put(AddressType.OFFICE, "Office address");
entityManager.persist(person1);
Phone phone1 = new Phone("123-456-7890");
phone1.setId(1L);
phone1.setType(PhoneType.MOBILE);
person1.addPhone(phone1);
Phone phone2 = new Phone("098_765-4321");
phone2.setId(2L);
phone2.setType(PhoneType.LAND_LINE);
person1.addPhone(phone2);
});
}
Aggregations