数据库从耳内罐子里访问

时间:2017-04-24 14:51:58

标签: jpa java-ee eclipselink

在一个ear-file中,我有一个持久性单元,用于管理那个耳内的实体,它工作正常。此外,APP-INF / lib中还有一个commons jar文件,它也是其他耳朵的依赖(同一级别的其他耳模块)。在这个commons-jar中,我想拥有一个单独的实体,并为该实体创建CRUD的可能性。现在它只是一个带有额外持久性单元的jar(但是与耳朵相同的数据库)并且我无法设置它。这个持久性单元是否也应该由容器管理?我也可以在该jar文件中使用EJB吗?是否有可能做我想做的事?

我按如下方式创建了实体

@Entity
public class ConfigEntity {

  @Id
  private Long id;
  @Version
  private Long version;

  [further fields, getters and setters omitted]

}

以下" Dao"我尝试从耳内的bean调用isEntityManagerNull。

public class ConfigBEDao {

  @PersistenceContext(unitName = "configurationPU")
  EntityManager entityManager;

  public boolean isEntityManagerNull() {
    return entityManager == null;
  }

}

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">

    <persistence-unit name="configurationPU" transaction-type="RESOURCE_LOCAL">
        <description/>
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>jdbc/db</jta-data-source>
        <properties>
            <property name="eclipselink.target-database" value="${database.dialect}"/>
            <property name="eclipselink.logging.level" value="FINE"/>
            <property name="eclipselink.logging.parameters" value="true"/>
            <property name="eclipselink.cache.shared.default" value="false"/>
        </properties>
    </persistence-unit>
</persistence>

我从ear文件中的bean调用

ConfigBEDao dao = new ConfigBEDao();
dao.isEntityManagerNull();

在尝试部署服务器(payara)时已经给出了以下异常:

java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [ConfigBEDao/entityManager] in the scope of the module called [main_ear-1.0-SNAPSHOT#main_ejb-1.0-SNAPSHOT.jar]. Please verify your application.
    at com.sun.enterprise.deployment.BundleDescriptor.findReferencedPUViaEMRef(BundleDescriptor.java:733)
    at org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl.findReferencedPUs(EjbBundleDescriptorImpl.java:889)
    at org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:186)
    at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:168)

然后我按如下方式更改了persistence.xml,但是虽然可以部署应用程序,但它没有工作:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">

    <persistence-unit name="configurationPU" transaction-type="RESOURCE_LOCAL">
        <description/>
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/db"/>
            <property name="javax.persistence.jdbc.user" value="db"/>
            <property name="javax.persistence.jdbc.password" value="secret"/>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
        </properties>
    </persistence-unit>
</persistence>

给了我

javax.persistence.PersistenceException: No Persistence provider for EntityManager named configurationPU
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)

0 个答案:

没有答案
相关问题