Hi,
I need to execute plain SQL query inside an ejb 3 service method (for example I need to get data from some technical oracle tables), my strategy was the following:
Get a plain jdbc connection
Code:
private Connection getConnection() {
org.hibernate.Session hibernateSession = entityManager.unwrap(org.hibernate.Session.class);
final Connection[] connections = new Connection[1];
hibernateSession.doWork(connection -> connections[0] = connection);
return connections[0];
}
ejb method:
Code:
public String doStuff() throws MyException{
SomeKindOfManagedObject o = new SomeKindOfManagedObject()
entityManager.persist(o)
//business logic
//modification to object "o"
final Connection connection = getConnection();
String query = "select * from all_tab_partitions where table_name = '"+ MY_TABLE +"'";
try (final Statement statement = connection.createStatement()) {
final ResultSet rs = statement.executeQuery(query);
doSomeKindOfStuff(rs);
}catch(Exception e){
logger.log(e);
throw new MyException(e);
}
entityManager.merge(o);
}
are there some kind of potential hidden issues related to unwrapping and using a plain jdbc connection as I did?
Is it tricky to mix ORM operation and plain JDBC query as I did?
Thanks!