首页 文章

实体不持久 - Spring Hibernate JPA

提问于
浏览
8

我正在使用Spring Hibernate JPA,我遇到的情况是我无法让我的实体持久化到数据库 . 我已经设置了一个使用@Transactional注释的服务类 . 它使用包含注入的EntityManager的DAO . 当我在服务对象上调用该函数时,我看到DAO正在执行的读取的一堆选择,但是由于我的DAO发出的合并和删除没有更新/删除 . 当然我的设置有问题,但我看不到它 .

persistence.xml中

<persistence 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"
    version="1.0">
    <persistence-unit name="pu">
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.InformixDialect" />
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />
            <property name="hibernate.showsql" value="true" />
            <property name="hibernate.cache.use_second_level_cache"
                value="false" />
        </properties>
    </persistence-unit>

config.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                http://www.springframework.org/schema/security
                http://www.springframework.org/schema/security/spring-security-3.0.3.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/testdb" />  
        <property name="username" value="username" />
        <property name="password" value="password" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="pu" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
            </bean>
        </property>
        <property name="loadTimeWeaver">
            <bean
                class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
        p:entityManagerFactory-ref="entityManagerFactory" />

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

    <context:annotation-config/>
    </beans>

AccountService.java

@Service("accountService")
@Transactional(propagation=Propagation.REQUIRED)
public class AccountService {

    private static final Logger log = Logger.getLogger(AccountService.class);

    @Autowired
    private UserDAO userDAO;

    public void activateUser(String username, String activationCode) {
        PendingActivation pendingActivation = userDAO.getPendingActivation(
                username, activationCode);

            Client client = pendingActivation.getClient();

            if (!userDAO.removePendingActivation(pendingActivation)) {
                log.warn("Unable to remove pending activation");
            }

            if (!userDAO.enableUser(client)) {
                log.error("Unable to enable client");
                return;
            }

            return;
        }
    }

UserDAOImpl.java

@Repository("userDAO")
public class UserDAOImpl implements UserDAO, Serializable {

    private static final long serialVersionUID = 1L;

    private static Logger log = Logger.getLogger(UserDAOImpl.class);

    @PersistenceContext
    EntityManager em;

    @Override 
    public PendingActivation getPendingActivation(String username, String activationCode) {
        Query q = em.createNamedQuery("getActivationCode")
            .setParameter("activationCode", activationCode);

        PendingActivation pendingActivation = null;
        try {
            pendingActivation = (PendingActivation)q.getSingleResult();
            return pendingActivation;
        }
        catch (Exception e) {
            log.warn("Could not retrieve activation code " + activationCode + " for user " + username, e);
            return null;
        }
    }

    @Override
    public boolean enableUser(Client client) {

        try {
            client.setEnabled(true);
            client = em.merge(client);   // this never generates an update
        }
        catch(Exception e) {
            log.error("Unable to enable client: " + client.getUsername(), e);
            return false;
        }

        return true;
    }

    @Override
    public boolean removePendingActivation(PendingActivation pendingActivation) {
        try {
            pendingActivation = (PendingActivation)em.getReference(PendingActivation.class, pendingActivation.getPendingActivationId());
            em.remove(pendingActivation);  // this never generates a delete
        }
        catch(Exception e) {
            log.warn("Unable to remove activation: " + pendingActivation.getActivationCode(), e);
            return false;
        }

        return true;
    }
}

AccountActivationController.java

@Controller
public class AccountActivationController {

    @Autowired
    @Qualifier("accountService")
    AccountService accountService;

    @RequestMapping("activate.do")
    public String doActivate(
            @RequestParam("activationCode") String activationCode,
            @RequestParam("username") String username,
            ModelMap model) {

        UnitCriteria unitCriteria = accountService.activateUser(username, activationCode);

        if (unitCriteria == null) {
            return "account/activationError";
        }

        model.addAttribute("fromActivation", true);
        return "forward:search.do?" + unitCriteria.toUrlParams(true);
    }

}

2 回答

  • 1

    好的,我发现了问题 . 我花了很长时间才弄清楚并且与我的数据库配置无关,所以我想帮助那些有类似问题的人 .

    Spring文档声明如下:

    <tx:annotation-driven />仅查找在其定义的同一应用程序上下文中的bean上的@Transactional . 这意味着,如果将<tx:annotation-driven />放在WebApplicationContext中以用于DispatcherServlet,则仅检查控制器中的@Transactional bean,而不是您的服务 . 有关更多信息,请参见第15.2节“DispatcherServlet” .

    我的原始帖子中没有发布的是我的servlet定义,其中包含以下几行配置代码:

    myServlet.xml

    <context:annotation-config /> 
    <context:component-scan base-package="com.myDomain.*" />
    

    这会将所有带注释的bean(包括控制器,服务和存储库)放入servlet上下文而不是应用程序上下文 . 其中存在问题 . 当Spring查找使用@Transactional注释的bean时(由于我的config.xml文件中存在 <tx:annotation-driven/> ),它正在应用程序上下文中查找它们 . 而且,根据我在上一个帖子中发布的配置,没有bean被加载到我的应用程序上下文中...它们都在servlet上下文中 . 因此,当我的servlet调用使用@Service和@Transactional注释的bean时,它是 using beans that were not wrapped by transaction proxies . 因此,没有交易 . 技巧(而不是正确的方法)是以下列方式更改我的配置文件:

    myServlet.xml

    <context:annotation-config /> 
    <context:component-scan base-package="com.myDomain.servlets" />
    

    config.xml中

    <context:annotation-config /> 
    <context:component-scan base-package="com.myDomain.dao" /> 
    <context:component-scan base-package="com.myDomain.services" />
    

    此配置确保所有控制器都存在于servlet上下文中,并且事务服务和存储库存在于应用程序上下文中,这是它们所属的位置 . 最后,经过许多不眠之夜,我的数据库写入仍然存在 .

  • 18

    我们可以在servlet-context.xml中提供Controller,如下所示

    <context:component-scan base-package="com.myDomain" use-default-filters="false" >
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    

    这将确保只有Controller存在于servlet-context中 . 在root-context.xml中使用以下内容

    <context:component-scan base-package="com.myDomain">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    

    这将确保应用程序上下文中存在除Controller之外的组件 . 我搜索了这个解决方案很多,因为没有这个JPA没有更新数据库,希望这将有助于某人

相关问题