Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. Map<String, String> properties = new HashMap<>();
  2. properties.put("provider", "org.hibernate.ejb.HibernatePersistence");
  3. properties.put("hibernate.show_sql", "true");
  4. properties.put("hibernate.format_sql", "true");
  5. properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
  6. properties.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
  7. properties.put("hibernate.connection.username", "username");
  8. properties.put("hibernate.connection.password", "password");
  9. properties.put("hibernate.connection.url", "jdbc:mysql://<hostname>:3306/<schema>");
  10.  
  11. Persistence.createEntityManagerFactory("localDB", properties);
  12.  
  13. compile group: 'org.hibernate', name: 'hibernate-core', version: '5.4.3.Final'
  14.  
  15. Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named localDB
  16.  
  17. public class PersistenceUnitInfoImpl implements PersistenceUnitInfo {
  18.  
  19. public static final String JPA_VERSION = "2.1";
  20.  
  21. private final String persistenceUnitName;
  22.  
  23. private PersistenceUnitTransactionType transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
  24.  
  25. private final List<String> managedClassNames;
  26.  
  27. private final List<String> mappingFileNames = new ArrayList<>();
  28.  
  29. private final Properties properties;
  30.  
  31. private DataSource jtaDataSource;
  32.  
  33. private DataSource nonJtaDataSource;
  34.  
  35. public PersistenceUnitInfoImpl(
  36. String persistenceUnitName,
  37. List<String> managedClassNames,
  38. Properties properties) {
  39. this.persistenceUnitName = persistenceUnitName;
  40. this.managedClassNames = managedClassNames;
  41. this.properties = properties;
  42. }
  43.  
  44. @Override
  45. public String getPersistenceUnitName() {
  46. return persistenceUnitName;
  47. }
  48.  
  49. @Override
  50. public String getPersistenceProviderClassName() {
  51. return HibernatePersistenceProvider.class.getName();
  52. }
  53.  
  54. @Override
  55. public PersistenceUnitTransactionType getTransactionType() {
  56. return transactionType;
  57. }
  58.  
  59. @Override
  60. public DataSource getJtaDataSource() {
  61. return jtaDataSource;
  62. }
  63.  
  64. public PersistenceUnitInfoImpl setJtaDataSource(
  65. DataSource jtaDataSource) {
  66. this.jtaDataSource = jtaDataSource;
  67. this.nonJtaDataSource = null;
  68. transactionType = PersistenceUnitTransactionType.JTA;
  69. return this;
  70. }
  71.  
  72. @Override
  73. public DataSource getNonJtaDataSource() {
  74. return nonJtaDataSource;
  75. }
  76.  
  77. public PersistenceUnitInfoImpl setNonJtaDataSource(
  78. DataSource nonJtaDataSource) {
  79. this.nonJtaDataSource = nonJtaDataSource;
  80. this.jtaDataSource = null;
  81. transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
  82. return this;
  83. }
  84.  
  85. @Override
  86. public List<String> getMappingFileNames() {
  87. return mappingFileNames;
  88. }
  89.  
  90. @Override
  91. public List<URL> getJarFileUrls() {
  92. return Collections.emptyList();
  93. }
  94.  
  95. @Override
  96. public URL getPersistenceUnitRootUrl() {
  97. return null;
  98. }
  99.  
  100. @Override
  101. public List<String> getManagedClassNames() {
  102. return managedClassNames;
  103. }
  104.  
  105. @Override
  106. public boolean excludeUnlistedClasses() {
  107. return false;
  108. }
  109.  
  110. @Override
  111. public SharedCacheMode getSharedCacheMode() {
  112. return SharedCacheMode.UNSPECIFIED;
  113. }
  114.  
  115. @Override
  116. public ValidationMode getValidationMode() {
  117. return ValidationMode.AUTO;
  118. }
  119.  
  120. public Properties getProperties() {
  121. return properties;
  122. }
  123.  
  124. @Override
  125. public String getPersistenceXMLSchemaVersion() {
  126. return JPA_VERSION;
  127. }
  128.  
  129. @Override
  130. public ClassLoader getClassLoader() {
  131. return Thread.currentThread().getContextClassLoader();
  132. }
  133.  
  134. @Override
  135. public void addTransformer(ClassTransformer transformer) {
  136.  
  137. }
  138.  
  139. @Override
  140. public ClassLoader getNewTempClassLoader() {
  141. return null;
  142. }
  143. }
  144.  
  145. Properties properties = new Properties();
  146. properties.put("hibernate.show_sql", "true");
  147. properties.put("hibernate.format_sql", "true");
  148. properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
  149. properties.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
  150. properties.put("hibernate.connection.username", "username");
  151. properties.put("hibernate.connection.password", "password");
  152. properties.put("hibernate.connection.url", "jdbc:mysql://<hostname>:3306/<schema>");
  153.  
  154. List<String> entitesClass = new ArrayList<>();
  155. entitesClass.add("com.company.entities.Foo");
  156. entitesClass.add("com.company.entities.Bar");
  157. PersistenceUnitInfoImpl punit = new PersistenceUnitInfoImpl("localDB", entitesClass , properties);
  158.  
  159. PersistenceProvider provider = new HibernatePersistenceProvider();
  160. EntityManagerFactory emf= provider.createContainerEntityManagerFactory(punit, properties);
  161.  
  162. EntityManager em = emf.createEntityManager();
  163. //blablblabl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement