How to inject persistence context to different data source programmatically

前端 未结 4 1086
时光说笑
时光说笑 2020-12-03 06:01

In standard EJB 3, when injecting entity manager, persistence unit (which refers to datasource) is hardcoded into annotation: (or alternatively xml file)

@Pe         


        
4条回答
  •  情话喂你
    2020-12-03 06:47

    Using EclipseLink, You can set a DataSource configured in your app server.

    import org.eclipse.persistence.config.PersistenceUnitProperties;
    ...
    
    
    ....
    Map props = new HashMap();  
    props.put(PersistenceUnitProperties.JTA_DATASOURCE, "dataSource");  
    EntityManagerFactory  emf = Persistence.createEntityManagerFactory("UNIT_NAME", props);
    EntityManager em = emf.createEntityManager();
    

    PU_NAME refers to the name used in the file persistence.xml
    dataSource refers name used in the app server for the jdbc Resource as "jdbc/sample"

提交回复
热议问题