【问题标题】:Change ejb code to EntityManager code将 ejb 代码更改为 EntityManager 代码
【发布时间】:2015-09-22 15:58:25
【问题描述】:
public class EjB3Configuration {

    private Ejb3Configuration configuration = null;

    protected PersistenceUnitInfo persistenceUnitInfo;

    public Ejb3Configuration getConfiguration() {
        synchronized(this) {
            if (configuration == null) {
                Ejb3Configuration temp = new Ejb3Configuration();
                String previousValue = persistenceUnitInfo.getProperties().getProperty("hibernate.hbm2ddl.auto");
                persistenceUnitInfo.getProperties().setProperty("hibernate.hbm2ddl.auto", "none");
                configuration = temp.configure(persistenceUnitInfo, new HashMap());
                configuration.getHibernateConfiguration().buildSessionFactory();
                if (previousValue != null) {
                    persistenceUnitInfo.getProperties().setProperty("hibernate.hbm2ddl.auto", previousValue);
                }
            }
        }
        return configuration;
    }

    public PersistenceUnitInfo getPersistenceUnitInfo() {
        return persistenceUnitInfo;
    }

    public void setPersistenceUnitInfo(PersistenceUnitInfo persistenceUnitInfo) {
        this.persistenceUnitInfo = persistenceUnitInfo;
    }

}

我对 ejb3 的调用如下所示。由于 ejb3 已从 4.3.10.Final hibernate 版本中删除,我如何将上述代码重构为 entitymanager。

public PersistentClass getPersistentClass(final String targetClassName) {
        return ejb3Configuration.getConfiguration().getClassMapping(targetClassName);
    }

我的电话

【问题讨论】:

    标签: java hibernate jpa orm entitymanager


    【解决方案1】:

    要以编程方式创建EntityManagerFactory,您可以这样做:

    Properties properties = getProperties();
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit", properties);
    

    【讨论】:

      猜你喜欢
      • 2017-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 2020-03-09
      • 2011-04-22
      • 2012-06-19
      相关资源
      最近更新 更多