WAR和EAR的EJB模块的职责是什么,配置文件放在哪里?

What is the responsibity of WAR and EJB modules of EAR, where to place configuration files?

我使用netbean 创建java EE 应用程序,自动创建了两个项目,一个是XXXXX-war,另一个是XXXXX-ejb。这两个项目的职责是什么?我在 ejb 项目中创建了一些 java 实体 classes 并将持久性单元和 glassfish-resoruces.xml 放在那里。然后在 war 项目中创建一些托管 bean classes, *.xhtml。然后我创建一个德比数据库。这是正确的方法吗?

我在 glassfish 服务器上部署 war 项目时遇到以下异常。

Invalid resource : jdbc/<databasename>__pm

我在 google 中搜索了一些相关问题,但其中 none 提供了解决方案。然后我将持久性单元和 glashfish-resources.xml 文件移动到 war 项目。完成此操作后,我可以部署 war 项目而不会出现此类错误。我想知道放置这些资源文件的正确方法是什么?为什么我们在 Java EE 应用程序中需要两个项目(ejb 和 war)。

下面是 glassfish-resources.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<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="org.apache.derby.jdbc.ClientDataSource" 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="derby_net_GovernmentDB_governmentPool" 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="1527"/>
        <property name="databaseName" value="GovernmentDB"/>
        <property name="User" value="government"/>
        <property name="Password" value="government"/>
        <property name="URL" value="jdbc:derby://localhost:1527/GovernmentDB"/>
        <property name="driverClass" value="org.apache.derby.jdbc.ClientDriver"/>
    </jdbc-connection-pool>
    <jdbc-resource enabled="true" jndi-name="java:app/warjndi" object-type="user" pool-name="derby_net_GovernmentDB_governmentPool"/>
</resources>

下面是坚持-unit.xml:

<?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="GovernmentDepartment-warPU" transaction-type="JTA">
    <jta-data-source>java:app/warjndi</jta-data-source>
    <class>homesoft.govDept.entity.SystemUserEntity</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
    </properties>
  </persistence-unit>
</persistence>

补充问题,通过上述配置,在部署我的war项目后,我可以看到在数据库GovernmentDB下自动创建了一个table(SEQUENCE)。但是我没有任何名为 SEQUENCE 的实体 class。这个 table 来自哪里?我的实体 class 在 ejb 项目中定义如下:

package homesoft.govDept.entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 *
 * @author Kaley
 */
@Entity
@Table(name="system_user")
public class SystemUserEntity implements Serializable {

    private long id;
    private String lastName;
    private String firstName;
    private String email;
    private String password;
    private String type;
    private String address;
    private String phoneNumber;

    @GeneratedValue
    @Id
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Column
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Column
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Column
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Column
    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

}

在 Java EE 6 之前,无法将 ejb 部署到 war 文件中,因此必须有一个包含 .ear 中包含的所有 ejb 的单独 .jar。

很可能 netbeans 仍在使用这种 "old" 方法。尝试查看项目创建时间,netbeans 可能会问您需要什么类型的项目。

关于额外的 SEQUENCE table,您已经为您的 @Id 指定了 @GeneratedValue,这意味着该 ID 是使用默认策略 AUTO 生成的,该策略又将其委托给底层数据库。

这对我来说听起来很奇怪,但是如果您还没有自己创建 table,那么 derbyDB 很可能使用 table 来管理身份,其中包含实体的所有增量值,实际上在您的持久性-unit.xml 您已声明生成架构,这导致在启动时创建所有 table:

<properties>
   <property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>

如果您反省 SEQUENCE table,您应该找到对所有使用 @GeneratedValue 声明的实体的引用。