如何为本地EJB bean

时间:2016-04-05 18:43:17

标签: jpa java-ee ejb jndi

我正在尝试使用Maven,JPA和EJB学习一些Java EE。我现在尝试使用InitialContext进行基本的EJB查找,只是为了熟悉ejb和东西。我对此非常陌生,我甚至无法在网上提供教程,因为注意对我来说很有意义。我花了大约4个小时阅读EJB,JNDI和InitialContext,但注意到我读了告诉我需要知道的事情。

我正在尝试查找从JPA实体执行基本读取操作的本地ejb。我尝试过“java:comp / env / beanName”JNDI名称,但它仍然无效。我尝试了在this帖子上找到的解决方案,但它也不起作用。这就是我所做的。

我有一个简单的ejb界面

package ejb;

import javax.ejb.Local;
import entity.Person;

@Local
public interface PersonService {
    public Person getPersonByID(int id);
}

由ejb

实现
package ejb;

import javax.ejb.Stateless;
import javax.persistence.*;
import entity.*;

@Stateless(mappedName="PersonServiceBean")
public class PersonServiceBean implements PersonService {
    //Pass persistence unit to entity manager
    @PersistenceContext(unitName = "reporting")
    private EntityManager entityManager;

    public Person getPersonByID(int id) {
        return entityManager.find(Person.class, id);
    }
}

上面的ejb从这个实体读取

package entity;

import java.io.Serializable;
import javax.persistence.*;


/**
* The persistent class for the person database table.
* 
*/
@Entity
@NamedQuery(name="Person.findAll", query="SELECT p FROM Person p")
public class Person implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private int personID;

    private String emails;

    private String firstNames;

    private int organizationID;

    private int researchCategoryID;

    private int researchGroupID;

    private String surname;

    public Person() {
    }

    public int getPersonID() {
        return this.personID;
    }

    public void setPersonID(int personID) {
        this.personID = personID;
    }

    public String getEmails() {
        return this.emails;
    }

    public void setEmails(String emails) {
        this.emails = emails;
    }

    public String getFirstNames() {
        return this.firstNames;
    }

    public void setFirstNames(String firstNames) {
        this.firstNames = firstNames;
    }

    public int getOrganizationID() {
        return this.organizationID;
    }

    public void setOrganizationID(int organizationID) {
        this.organizationID = organizationID;
    }

    public int getResearchCategoryID() {
        return this.researchCategoryID;
    }

    public void setResearchCategoryID(int researchCategoryID) {
        this.researchCategoryID = researchCategoryID;
    }

    public int getResearchGroupID() {
        return this.researchGroupID;
    }

    public void setResearchGroupID(int researchGroupID) {
        this.researchGroupID = researchGroupID;
    }

    public String getSurname() {
        return this.surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

}

JPA etity连接到mysql数据库,这是persistence.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="reporting" transaction-type="RESOURCE_LOCAL">
        <class>entity.Person</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/people"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        </properties>
    </persistence-unit>
</persistence>

我有一个Main类充当客户端

import javax.naming.InitialContext;
import javax.naming.NamingException;
import ejb.*;

public class Main {
    public static void main(String[] args) throws NamingException {
        InitialContext ic = new InitialContext();
        PersonService service = (PersonService)ic.lookup("PersonServiceBean");
        entity.Person p = service.getPersonByID(1);
        System.out.println(p.getFirstNames());
    }
}

我正在使用EclipseLink amd我不知道我正在使用哪个应用服务器,如果我正在使用它。如果它也有帮助,我正在使用Eclipse。如果我尝试运行应用程序,我会得到javax.naming.NoInitialContextException异常

Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at javax.naming.spi.NamingManager.getInitialContext(Unknown Source) at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source) at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source) at javax.naming.InitialContext.lookup(Unknown Source) at Main.main(Main.java:18)

我还能从Main访问EJB吗?

2 个答案:

答案 0 :(得分:0)

如果没有EJB容器(由JBoss或Websphere等应用程序服务器提供),则无法运行EJB。如果你需要一个好的框架来使用JPA为什么不使用Spring(我在Web应用程序和java批处理中都使用它)?设置并提供从EJB获得的每个优势都很容易。

答案 1 :(得分:0)

JavaBeans that are running within the container pass their credentials within the InitialContext, which is created to look up the remote EJBs. 

To pass JNDI properties within the Hashtable environment, set them as shown below: 
Hashtable env = new Hashtable(); 
env.put("java.naming.provider.url", "ormi://localhost/ejbexample"); 
env.put("java.naming.factory.initial", 
      "com.evermind.server.ApplicationClientInitialContextFactory"); 
env.put(Context.SECURITY_PRINCIPAL, "guest"); 
env.put(Context.SECURITY_CREDENTIALS, "welcome"); 
Context ic = new InitialContext (env); 
Object homeObject = ic.lookup("java:comp/env/employeeBean");

EmployeeHome empHome =
     (EmployeeHome) PortableRemoteObject.narrow(homeObject,
                                                   EmployeeHome.class);