Guest User

Untitled

a guest
Dec 11th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. entityManager.getTransaction().begin();
  2. java.sql.Connection connection = entityManager.unwrap(java.sql.Connection.class); // unwraps the Connection class.
  3. ...
  4. entityManager.getTransaction().commit();
  5.  
  6. entityManager.getTransaction().begin();
  7. UnitOfWork unitOfWork = (UnitOfWork)((JpaEntityManager)entityManager.getDelegate()).getActiveSession();
  8. unitOfWork.beginEarlyTransaction();
  9. Accessor accessor = unitOfWork.getAccessor();
  10. accessor.incrementCallCount(unitOfWork.getParent());
  11. accessor.decrementCallCount();
  12. java.sql.Connection connection = accessor.getConnection();
  13. ...
  14. entityManager.getTransaction().commit();
  15.  
  16. Session hibernateSession = entityManager.unwrap(Session.class);
  17. Connection jdbcConnection = hibernateSession.connection();
  18.  
  19. Connection connection = entityManager().unwrap(SessionImpl.class).connection();
  20.  
  21. private void executeNative(final String query) {
  22. Session session = entityManager.unwrap(Session.class);
  23. session.doWork(new Work() {
  24.  
  25. @Override
  26. public void execute(Connection connection) throws SQLException {
  27. Statement s = null;
  28. try {
  29. s = connection.createStatement();
  30. s.executeUpdate(query);
  31. }
  32. finally {
  33. if (s != null) {
  34. s.close();
  35. }
  36. }
  37. }
  38.  
  39. });
  40. }
  41.  
  42. SessionImpl sessionImpl = (SessionImpl) session;
  43. Connection conn = sessionImpl.connection();
  44.  
  45. import java.sql.Connection;
  46. import org.apache.openjpa.persistence.OpenJPAEntityManager;
  47. import org.apache.openjpa.persistence.OpenJPAPersistence;
  48.  
  49.  
  50.  
  51. public final class MsSqlDaoFactory {
  52.  
  53.  
  54. public static final Connection getConnection(final EntityManager entityManager) {
  55. OpenJPAEntityManager openJPAEntityManager = OpenJPAPersistence.cast(entityManager);
  56. Connection connection = (Connection) openJPAEntityManager.getConnection();
  57. return connection;
  58.  
  59. }
  60.  
  61. }
  62.  
  63. Statement statement = null;
  64. EntityManager em = null;
  65. em = emf.createEntityManager();
  66. EntityTransaction et = em.getTransaction();
  67.  
  68. if(!et.isActive()) {
  69. et.begin();
  70. }
  71.  
  72. java.sql.Connection connection = em.unwrap(java.sql.Connection.class);
  73.  
  74. String qquerry="SELE ...
  75. try {
  76. statement = connection.createStatement();
  77. ResultSet rs = statement.executeQuery(qquerry);
  78.  
  79. if (!rs.next()) {
  80. return null;
  81. }
  82. else{
  83. wwwwas=rs.getString(4);
  84. }
  85. statement.close();
  86. }
  87. catch (SQLException e) {
  88. System.out.println("n b-03:"+e);
  89. throw new RuntimeException(e.getMessage(), e);
  90. }
  91. finally {
  92. try {
  93. // em.getTransaction().commit();
  94. if(connection != null )
  95. connection.close();
  96. }
  97. catch (Exception e) {
  98. throw new RuntimeException(e.getMessage(), e);
  99. }
  100. }
Add Comment
Please, Sign In to add comment