从 Jboss 4.2.2 Spring 2 到 Jboss 6 Spring 3 配置 JPA 实体管理器时出现问题

发布于 2024-11-08 14:57:34 字数 5750 浏览 4 评论 0原文

当 jboss6 spring 3 JPA2 休眠时 EntityManager 为 null(未注入)

尝试将在 Jboss4.2.2 Spring 2 上运行的现有应用程序迁移到 Jboss 6 Spring 3

经过多次尝试,应用程序已正确部署,但在执行(调试)时,entityManager 为空)。我是否错过了注入 EM 的地点和时间,因为 Spring 2 下的旧 Application.xml 有很多更改

我有一个 persistence.xml 文件(Spring 3 jboss 6 的新文件)< /strong>

<persistence version="2.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_2_0.xsd">`
<persistence-unit name="boxPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/BoxMysqlDS</jta-data-source>
<class>box.business.Customer</class>
<properties>
<property name="jboss.entity.manager.factory.jndi.name" value="java:/BoxPersistenceUnitFactory"/>
<property name="jboss.entity.manager.jndi.name" value="java:/boxEM"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit> 
</persistence>

*Spring 3 Jboss 6 的 Apllication.xml *

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"`
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
    <!-- Container managed data source. -->
<jee:jndi-lookup id="dataSource" jndi-name="java:/BoxMysqlDS"
    expose-access-context="false" />
    <!-- Spring will inject this container managed persistence unit anywhere 
    you use @PersistenceContext. -->
<jee:jndi-lookup id="entityManagerFactory" jndi-name="java:/BoxPersistenceUnitFactory"
    expected-type="javax.persistence.EntityManagerFactory" />
    <context:annotation-config transaction-manager="transactionManager" />
    <bean id="customerDaoBean" class="box.dao.CustomerDaoImpl"></bean>
    <!-- Manager beans -->
<bean id="boxManagerBean" class="box.service.BoxManagerImpl">
    <property name="customerDao" ref="customerDaoBean" />
</bean>
</beans>

服务类

package box.service;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import box.dao.CustomerDao;
public class BoxManagerImpl implements BoxManager {
@PersistenceContext(unitName = "boxPU")
private EntityManager entityManager;
private CustomerDao customerDao;


public void setcustomerDao(CustomerDao customerDao) {
    this.customerDao = customerDao;
}

public String test() {


    boolean retour = customerDao.isEmailAlreadyUsed("[email protected]");

    return "valeur retour :"+ retour;

}}

DAO类

package box.dao;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import org.springframework.transaction.annotation.Transactional;
import box.business.Customer;
@Transactional
public class CustomerDaoImpl implements CustomerDao {
@PersistenceContext(unitName = "boxPU")
private EntityManager entityManager;

public boolean isEmailAlreadyUsed(String email) {

        String SQL_SELECT_CUSTOMER_EXISTS = " select c from " + " "
            + Constants.SQL_CUSTOMER_TABLE + " c, " + " "
            + Constants.SQL_STATUS_TABLE + " s "
            + " where c.status.statusid=s.statusid " + " and c.email='"
            + email.toLowerCase() + "'";

        boolean customerexists = false;

        if (email == null || email.equals(""))
        throw new DaoException("email null in isCustomerExists ", 10);

    Customer customer = null;
    try {
        customer = (Customer) entityManager.createQuery(
                SQL_SELECT_CUSTOMER_EXISTS).getSingleResult();
        customerexists = (customer == null) ? false : true;
    } catch (NoResultException exception) {
        customerexists = (customer == null) ? false : true;
    }

catch (PersistenceException exception) {
        throw new DaoException(exception.getMessage(),
                Error.CUSTOMER_IS_EMAIL_ALREADY_EXISTS_DAO_ERROR);
    }

    if (customerexists)
        throw new DaoException(
                Error.CUSTOMER_EMAIL_ALREADY_EXISTS_ERROR_MSG,
                Error.CUSTOMER_EMAIL_ALREADY_EXISTS_ERROR);

    return customerexists;
}
}

部署正确,没有错误。但是执行App时,DAO中的entityManger为null。 该应用程序与 Jboss 4.2.2 和 Spring 2 配合良好。 我认为注入机制有问题,我肯定错过了一些东西。 谢谢

EntityManager is null (not injected) whith jboss6 spring 3 JPA2 hibernate

trying to migrate an existing App running on Jboss4.2.2 Spring 2 to
Jboss 6 Spring 3

Afer many try the App is correctly deploy, but when executing (debug) the entityManager is null). Do i miss something, where and when the EM is injected, as there is many changes with the old Application.xml under Spring 2

I have a persistence.xml file (new one for Spring 3 jboss 6)

<persistence version="2.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_2_0.xsd">`
<persistence-unit name="boxPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/BoxMysqlDS</jta-data-source>
<class>box.business.Customer</class>
<properties>
<property name="jboss.entity.manager.factory.jndi.name" value="java:/BoxPersistenceUnitFactory"/>
<property name="jboss.entity.manager.jndi.name" value="java:/boxEM"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit> 
</persistence>

*An Apllication.xml for Spring 3 Jboss 6 *

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"`
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
    <!-- Container managed data source. -->
<jee:jndi-lookup id="dataSource" jndi-name="java:/BoxMysqlDS"
    expose-access-context="false" />
    <!-- Spring will inject this container managed persistence unit anywhere 
    you use @PersistenceContext. -->
<jee:jndi-lookup id="entityManagerFactory" jndi-name="java:/BoxPersistenceUnitFactory"
    expected-type="javax.persistence.EntityManagerFactory" />
    <context:annotation-config transaction-manager="transactionManager" />
    <bean id="customerDaoBean" class="box.dao.CustomerDaoImpl"></bean>
    <!-- Manager beans -->
<bean id="boxManagerBean" class="box.service.BoxManagerImpl">
    <property name="customerDao" ref="customerDaoBean" />
</bean>
</beans>

The service Class

package box.service;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import box.dao.CustomerDao;
public class BoxManagerImpl implements BoxManager {
@PersistenceContext(unitName = "boxPU")
private EntityManager entityManager;
private CustomerDao customerDao;


public void setcustomerDao(CustomerDao customerDao) {
    this.customerDao = customerDao;
}

public String test() {


    boolean retour = customerDao.isEmailAlreadyUsed("[email protected]");

    return "valeur retour :"+ retour;

}}

The DAO Class

package box.dao;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import org.springframework.transaction.annotation.Transactional;
import box.business.Customer;
@Transactional
public class CustomerDaoImpl implements CustomerDao {
@PersistenceContext(unitName = "boxPU")
private EntityManager entityManager;

public boolean isEmailAlreadyUsed(String email) {

        String SQL_SELECT_CUSTOMER_EXISTS = " select c from " + " "
            + Constants.SQL_CUSTOMER_TABLE + " c, " + " "
            + Constants.SQL_STATUS_TABLE + " s "
            + " where c.status.statusid=s.statusid " + " and c.email='"
            + email.toLowerCase() + "'";

        boolean customerexists = false;

        if (email == null || email.equals(""))
        throw new DaoException("email null in isCustomerExists ", 10);

    Customer customer = null;
    try {
        customer = (Customer) entityManager.createQuery(
                SQL_SELECT_CUSTOMER_EXISTS).getSingleResult();
        customerexists = (customer == null) ? false : true;
    } catch (NoResultException exception) {
        customerexists = (customer == null) ? false : true;
    }

catch (PersistenceException exception) {
        throw new DaoException(exception.getMessage(),
                Error.CUSTOMER_IS_EMAIL_ALREADY_EXISTS_DAO_ERROR);
    }

    if (customerexists)
        throw new DaoException(
                Error.CUSTOMER_EMAIL_ALREADY_EXISTS_ERROR_MSG,
                Error.CUSTOMER_EMAIL_ALREADY_EXISTS_ERROR);

    return customerexists;
}
}

The deploy is correct, no errors. But when executing the App, the entityManger in the DAO is null.
This App work fine with Jboss 4.2.2 and Spring 2.
I think there is a probleme is the injection mechanism, i certainly miss something.
thanks

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文