为了账号安全,请及时绑定邮箱和手机立即绑定

没有 Spring 和 XML 的最小 JPA 示例

没有 Spring 和 XML 的最小 JPA 示例

喵喵时光机 2022-12-28 10:53:24
爪哇        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 或任何其他实现)的最小工作示例?
查看完整描述

1 回答

?
动漫人物

TA贡献1815条经验 获得超10个赞

如果您只添加一个META-INF/persistence.xml. 但是,如果您坚持不这样做,则persistence.xml必须自己实施PersistenceUnitInfo,这与仅添加一个persistence.xml. 可以在此处找到一个最小的实现:


public class PersistenceUnitInfoImpl implements PersistenceUnitInfo {


    public static final String JPA_VERSION = "2.1";


    private final String persistenceUnitName;


    private PersistenceUnitTransactionType transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;


    private final List<String> managedClassNames;


    private final List<String> mappingFileNames = new ArrayList<>();


    private final Properties properties;


    private DataSource jtaDataSource;


    private DataSource nonJtaDataSource;


    public PersistenceUnitInfoImpl(

            String persistenceUnitName,

            List<String> managedClassNames,

            Properties properties) {

        this.persistenceUnitName = persistenceUnitName;

        this.managedClassNames = managedClassNames;

        this.properties = properties;

    }


    @Override

    public String getPersistenceUnitName() {

        return persistenceUnitName;

    }


    @Override

    public String getPersistenceProviderClassName() {

        return HibernatePersistenceProvider.class.getName();

    }


    @Override

    public PersistenceUnitTransactionType getTransactionType() {

        return transactionType;

    }


    @Override

    public DataSource getJtaDataSource() {

        return jtaDataSource;

    }


    public PersistenceUnitInfoImpl setJtaDataSource(

            DataSource jtaDataSource) {

        this.jtaDataSource = jtaDataSource;

        this.nonJtaDataSource = null;

        transactionType = PersistenceUnitTransactionType.JTA;

        return this;

    }


    @Override

    public DataSource getNonJtaDataSource() {

        return nonJtaDataSource;

    }


    public PersistenceUnitInfoImpl setNonJtaDataSource(

            DataSource nonJtaDataSource) {

        this.nonJtaDataSource = nonJtaDataSource;

        this.jtaDataSource = null;

        transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;

        return this;

    }


    @Override

    public List<String> getMappingFileNames() {

        return mappingFileNames;

    }


    @Override

    public List<URL> getJarFileUrls() {

        return Collections.emptyList();

    }


    @Override

    public URL getPersistenceUnitRootUrl() {

        return null;

    }


    @Override

    public List<String> getManagedClassNames() {

        return managedClassNames;

    }


    @Override

    public boolean excludeUnlistedClasses() {

        return false;

    }


    @Override

    public SharedCacheMode getSharedCacheMode() {

        return SharedCacheMode.UNSPECIFIED;

    }


    @Override

    public ValidationMode getValidationMode() {

        return ValidationMode.AUTO;

    }


    public Properties getProperties() {

        return properties;

    }


    @Override

    public String getPersistenceXMLSchemaVersion() {

        return JPA_VERSION;

    }


    @Override

    public ClassLoader getClassLoader() {

        return Thread.currentThread().getContextClassLoader();

    }


    @Override

    public void addTransformer(ClassTransformer transformer) {


    }


    @Override

    public ClassLoader getNewTempClassLoader() {

        return null;

    }

}

然后用它来引导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


查看完整回答
反对 回复 2022-12-28
  • 1 回答
  • 0 关注
  • 57 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信