JBoss 5.1 上的 Java EE bean 中的空 EntityManager

发布于 2024-10-05 02:51:04 字数 2514 浏览 3 评论 0原文

我是 Java EE 的新手。这是我尝试做的第一个项目,但我有一个无法解决的问题。

我创建了三个项目:slowka-beans (EJB)、slowka-persistance (JPA) 和 Slowka-web(JSF)。部署它们后,我无法访问持久性单元 - EntityManager 为空。一切工作正常 - 我可以创建 bean,在它们内部实例化实体类并将它们显示在 JSF 页面上。但如何将它们存储在数据库中呢?我在 JBoss 站点上配置了 MySQL 数据库。

我的代码如下所示: LanguagesManager.java(在slowka-beans中)

@Stateless
public class LanguagesManager implements LanguagesManagerLocal {

    @PersistenceContext(unitName="slowka-persistance")
    private EntityManager em;

    public LanguagesManager() {
        System.out.println("LanguagesManagerBean constructor");
    }

    public String getWorking() {
        if(em == null) {
            System.out.println("Not working...");
            return "Not working...";
        } else {
            System.out.println("It's ALIVE!");
            return "It's ALIVE!";
        }
    }
}

persistence.xml(slowka-persistance):

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="slowka-persistance">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/PraktykiDS</jta-data-source>
        <class>pl.edu.uj.sobczak.szymon.Language</class>
    </persistence-unit>
</persistence>

在服务器上部署它不会导致任何异常。但我在服务器的输出中发现了以下警告:

23:02:23,801 INFO  [PersistenceUnitDeployment] Starting persistence unit persistence.unit:unitName=slowka.ear/slowka-persistance.jar#slowka-persistance
23:02:23,803 INFO  [Ejb3Configuration] Processing PersistenceUnitInfo [
    name: slowka-persistance
    ...]
23:02:23,804 WARN  [Ejb3Configuration] Persistence provider caller does not implement the EJB3 spec correctly. PersistenceUnitInfo.getNewTempClassLoader() is null.
(... trimmed ...)
23:02:23,868 INFO  [SessionFactoryObjectFactory] Bound factory to JNDI name: persistence.unit:unitName=slowka.ear/slowka-persistance.jar#slowka-persistance
23:02:23,868 WARN  [SessionFactoryObjectFactory] InitialContext did not implement EventContext

每次我从 JSP 访问 LanguagesManager::getWorking() 时,我都会收到“Notworking...”输出。

我已经在 Eclipse 中创建了该项目,JPA 使用 EclipseLink。我尝试过 EclipseLink 1.1.4 和 2.1.0,结果相同。

你能帮我吗?

I'm a newbie in Java EE. This is the first project I'm trying to do and I have a problem that I just can't solve.

I've created three projects: slowka-beans (EJB), slowka-persistance (JPA) and slowka-web(JSF). After deploying them I can't get access to persistence unit - the EntityManager is null. Everything works fine - I can create beans, inside them instantiate entity classes and show them on JSF page. But how can I store them in the DB? I have MySQL database configured on JBoss site.

The code that I have looks following:
LanguagesManager.java (in slowka-beans)

@Stateless
public class LanguagesManager implements LanguagesManagerLocal {

    @PersistenceContext(unitName="slowka-persistance")
    private EntityManager em;

    public LanguagesManager() {
        System.out.println("LanguagesManagerBean constructor");
    }

    public String getWorking() {
        if(em == null) {
            System.out.println("Not working...");
            return "Not working...";
        } else {
            System.out.println("It's ALIVE!");
            return "It's ALIVE!";
        }
    }
}

persistence.xml (slowka-persistance):

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="slowka-persistance">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/PraktykiDS</jta-data-source>
        <class>pl.edu.uj.sobczak.szymon.Language</class>
    </persistence-unit>
</persistence>

Deploying this on server doesn't cause any exceptions. but I've spotted the following warnings in server's output:

23:02:23,801 INFO  [PersistenceUnitDeployment] Starting persistence unit persistence.unit:unitName=slowka.ear/slowka-persistance.jar#slowka-persistance
23:02:23,803 INFO  [Ejb3Configuration] Processing PersistenceUnitInfo [
    name: slowka-persistance
    ...]
23:02:23,804 WARN  [Ejb3Configuration] Persistence provider caller does not implement the EJB3 spec correctly. PersistenceUnitInfo.getNewTempClassLoader() is null.
(... trimmed ...)
23:02:23,868 INFO  [SessionFactoryObjectFactory] Bound factory to JNDI name: persistence.unit:unitName=slowka.ear/slowka-persistance.jar#slowka-persistance
23:02:23,868 WARN  [SessionFactoryObjectFactory] InitialContext did not implement EventContext

Every time I'm accessing LanguagesManager::getWorking() from JSP i'm getting "Not working..." output.

I've created the project in Eclipse, JPA is using EclipseLink. I've tried both - EclipseLink 1.1.4 and 2.1.0 with the same result.

Can please you help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤 2024-10-12 02:51:04

不要从 JSP 调用 EJB。从 servlet 调用它们,并使用 @EJB 注入它们。

如果您手动实例化对象,则不会发生注入。该对象(ejb)必须由 EJB 容器实例化。

Don't call EJBs from JSPs. Call them from servlets, where you inject them with @EJB.

If you instantiate the object manually, then injection does not happen. The object (ejb) must be instantiated by the EJB container.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文