JPA EntityManagerFactory不会覆盖persistence.xml文件

时间:2017-09-22 16:45:11

标签: jpa java-ee

当我在应用程序中吃午餐时,不会使用这些属性,而是使用glassfish-ressource.xml中的默认属性。我使用JPA与nebeans自动生成的bean和实体。我想在运行时使用switch数据库。

这是他的会话课

package facade;
import entities.Tpe;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Emmanuel
 */
@Stateless
public class TpeFacade extends AbstractFacade<Tpe> {
    private EntityManager em;
     @Override
    public EntityManager getEntityManager() {

    EntityManagerFactory emf=null;
    Map properties = new HashMap();
    properties.put("javax.persistence.transactionType", "JTA");
    properties.put("javax.persistence.jdbc.driver", "jdbc:mysql://192.20.3.81:3306/piv?zeroDateTimeBehavior=convertToNull");
    properties.put("javax.persistence.jdbc.url", "com.mysql.jdbc.Driver");
    properties.put("javax.persistence.jdbc.database", "piv");
    properties.put("javax.persistence.jdbc.user", "username");
    properties.put("javax.persistence.jdbc.password", "password");

    try {
         emf = Persistence.createEntityManagerFactory("ConfigurationTPEPU", properties);
         System.out.println("emfznezizzhzz "+emf.getProperties() );
    } catch (Exception e) {
    }
    em = (EntityManager) emf.createEntityManager();
    return em ;
}


    public TpeFacade() {
        super(Tpe.class);
    }

}

和我的持久性文件内容

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="ConfigurationTPEPU" transaction-type="JTA">
    <jta-data-source>java:app/connexion81</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>

  </persistence-unit>

</persistence>

glassfish-ressources.xml内容

<resources>
    <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="mysql_piv_rootPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
        <property name="serverName" value="localhost"/>
        <property name="portNumber" value="3306"/>
        <property name="databaseName" value="db1"/>
        <property name="User" value="root"/>
        <property name="Password" value="gsmcom"/>
                <property name="URL" value="jdbc:mysql://localhost:3306/petroivoire?zeroDateTimeBehavior=convertToNull"/>

        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    </jdbc-connection-pool>
    <jdbc-resource enabled="true" jndi-name="java:app/connexion81" object-type="user" pool-name="mysql_piv_rootPool"/>
</resources>

1 个答案:

答案 0 :(得分:0)

你应该理解,对于 @EJB ,应用程序服务器在这里负责@EJB的注释以及注入持久性。

使用的配置是Glassfish域(domain / config / domain.xml)中已有的配置,而不是glassfish-ressource.xml文件的这些属性。

这意味着,要覆盖第一个配置,您将使用两个持久性并且:

  1. 以编程方式在它们之间切换(当两者都被注入时)。

  2. 或者使用 Persistence.createEntityManagerFactory(jndiDataSource2NdUnitName).createEntityManager(); 其中jndiDataSource2NdUnitName是第二个单位名称。