第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

沒有 Spring 和 XML 的最小 JPA 示例

沒有 Spring 和 XML 的最小 JPA 示例

喵喵時光機(jī) 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'當(dāng)我運(yùn)行它時,我得到Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named localDB我想在沒有 Spring 和 persistence.xml/hibernate.cfg.xml 的情況下進(jìn)行。我對注釋配置很滿意。我不知道如何在帶有 @Entity 注釋(沒有 XML)的類所在的屬性中聲明。我在哪里可以找到?jīng)]有 XML 或其他框架(Spring)的 JPA(Hibernate 或任何其他實(shí)現(xiàn))的最小工作示例?
查看完整描述

1 回答

?
動漫人物

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個贊

如果您只添加一個META-INF/persistence.xml. 但是,如果您堅持不這樣做,則persistence.xml必須自己實(shí)施PersistenceUnitInfo,這與僅添加一個persistence.xml. 可以在此處找到一個最小的實(shí)現(xiàn):


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;

    }

}

然后用它來引導(dǎo)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


查看完整回答
反對 回復(fù) 2022-12-28
  • 1 回答
  • 0 關(guān)注
  • 107 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號