没有 Spring 和 XML 的最小 JPA 示例

爪哇

        Map<String, String> properties = new HashMap<>();
        properties.put("provider", "org.hibernate.ejb.HibernatePersistence");
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.format_sql", "true");
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        properties.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        properties.put("hibernate.connection.username", "username");
        properties.put("hibernate.connection.password", "password");
        properties.put("hibernate.connection.url", "jdbc:mysql://<hostname>:3306/<schema>");

        Persistence.createEntityManagerFactory("localDB", properties);

摇篮依赖

compile group: 'org.hibernate', name: 'hibernate-core', version: '5.4.3.Final'

当我运行它时,我得到

Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named localDB

我想在没有 Spring 和 persistence.xml/hibernate.cfg.xml 的情况下进行。我对注释配置很满意。

我不知道如何在带有 @Entity 注释(没有 XML)的类所在的属性中声明。

我在哪里可以找到没有 XML 或其他框架(Spring)的 JPA(Hibernate 或任何其他实现)的最小工作示例?


喵喵时光机
浏览 75回答 1
1回答

动漫人物

如果您只添加一个META-INF/persistence.xml. 但是,如果您坚持不这样做,则persistence.xml必须自己实施PersistenceUnitInfo,这与仅添加一个persistence.xml. 可以在此处找到一个最小的实现:public class PersistenceUnitInfoImpl implements PersistenceUnitInfo {&nbsp; &nbsp; public static final String JPA_VERSION = "2.1";&nbsp; &nbsp; private final String persistenceUnitName;&nbsp; &nbsp; private PersistenceUnitTransactionType transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;&nbsp; &nbsp; private final List<String> managedClassNames;&nbsp; &nbsp; private final List<String> mappingFileNames = new ArrayList<>();&nbsp; &nbsp; private final Properties properties;&nbsp; &nbsp; private DataSource jtaDataSource;&nbsp; &nbsp; private DataSource nonJtaDataSource;&nbsp; &nbsp; public PersistenceUnitInfoImpl(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String persistenceUnitName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<String> managedClassNames,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Properties properties) {&nbsp; &nbsp; &nbsp; &nbsp; this.persistenceUnitName = persistenceUnitName;&nbsp; &nbsp; &nbsp; &nbsp; this.managedClassNames = managedClassNames;&nbsp; &nbsp; &nbsp; &nbsp; this.properties = properties;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String getPersistenceUnitName() {&nbsp; &nbsp; &nbsp; &nbsp; return persistenceUnitName;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String getPersistenceProviderClassName() {&nbsp; &nbsp; &nbsp; &nbsp; return HibernatePersistenceProvider.class.getName();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public PersistenceUnitTransactionType getTransactionType() {&nbsp; &nbsp; &nbsp; &nbsp; return transactionType;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public DataSource getJtaDataSource() {&nbsp; &nbsp; &nbsp; &nbsp; return jtaDataSource;&nbsp; &nbsp; }&nbsp; &nbsp; public PersistenceUnitInfoImpl setJtaDataSource(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataSource jtaDataSource) {&nbsp; &nbsp; &nbsp; &nbsp; this.jtaDataSource = jtaDataSource;&nbsp; &nbsp; &nbsp; &nbsp; this.nonJtaDataSource = null;&nbsp; &nbsp; &nbsp; &nbsp; transactionType = PersistenceUnitTransactionType.JTA;&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public DataSource getNonJtaDataSource() {&nbsp; &nbsp; &nbsp; &nbsp; return nonJtaDataSource;&nbsp; &nbsp; }&nbsp; &nbsp; public PersistenceUnitInfoImpl setNonJtaDataSource(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataSource nonJtaDataSource) {&nbsp; &nbsp; &nbsp; &nbsp; this.nonJtaDataSource = nonJtaDataSource;&nbsp; &nbsp; &nbsp; &nbsp; this.jtaDataSource = null;&nbsp; &nbsp; &nbsp; &nbsp; transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public List<String> getMappingFileNames() {&nbsp; &nbsp; &nbsp; &nbsp; return mappingFileNames;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public List<URL> getJarFileUrls() {&nbsp; &nbsp; &nbsp; &nbsp; return Collections.emptyList();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public URL getPersistenceUnitRootUrl() {&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public List<String> getManagedClassNames() {&nbsp; &nbsp; &nbsp; &nbsp; return managedClassNames;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean excludeUnlistedClasses() {&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public SharedCacheMode getSharedCacheMode() {&nbsp; &nbsp; &nbsp; &nbsp; return SharedCacheMode.UNSPECIFIED;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public ValidationMode getValidationMode() {&nbsp; &nbsp; &nbsp; &nbsp; return ValidationMode.AUTO;&nbsp; &nbsp; }&nbsp; &nbsp; public Properties getProperties() {&nbsp; &nbsp; &nbsp; &nbsp; return properties;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String getPersistenceXMLSchemaVersion() {&nbsp; &nbsp; &nbsp; &nbsp; return JPA_VERSION;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public ClassLoader getClassLoader() {&nbsp; &nbsp; &nbsp; &nbsp; return Thread.currentThread().getContextClassLoader();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void addTransformer(ClassTransformer transformer) {&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public ClassLoader getNewTempClassLoader() {&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }}然后用它来引导EntityManagerFactory/ EntityManager:Properties properties = new Properties();properties.put("hibernate.show_sql", "true");properties.put("hibernate.format_sql", "true");properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");properties.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");properties.put("hibernate.connection.username", "username");properties.put("hibernate.connection.password", "password");properties.put("hibernate.connection.url", "jdbc:mysql://<hostname>:3306/<schema>");List<String> entitesClass = new ArrayList<>();entitesClass.add("com.company.entities.Foo");entitesClass.add("com.company.entities.Bar");PersistenceUnitInfoImpl punit = new PersistenceUnitInfoImpl("localDB", entitesClass , properties);PersistenceProvider provider = new HibernatePersistenceProvider();EntityManagerFactory emf= provider.createContainerEntityManagerFactory(punit, null);EntityManager em = emf.createEntityManager();//blablblabl
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java