简体   繁体   English

jboss的Spring和Persistence单元

[英]Spring and persistence unit with jboss

My project initially develop using with out persistence unit and project is spring,Struts2 hibernate intergration one. 我的项目最初是用没有持久性单元开发的,而项目是spring,Struts2休眠集成。 Now I need to use jboss connection pool and persistence unit. 现在,我需要使用jboss连接池和持久性单元。 Can any one guild me to shortest way to convert project in that requirement. 可以要求我以最简单的方式在该要求中转换项目。 current spring.xml 当前spring.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>properties/database.properties</value>
    </property>
</bean>
<bean id="springdatasource"

    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource">
        <ref bean="springdatasource" />
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>

    <property name="annotatedClasses">
        <list>

            <value>model.AtOrganisation</value>

            <value>model.AtDivision</value>



        </list>
    </property>

</bean>



<bean id="orgdao" class="dao.OrganisationDaoImp">
    <property name="sessionfactory" ref="sessionFactory" />
</bean>
<bean id="divdao" class="dao.DevisionDaoImpl">
    <property name="sessionfactory" ref="sessionFactory" />
</bean>
<bean id="empAction" class="action.OraganisationAction">
    <property name="orgdao" ref="orgdao" />
</bean>
<bean id="empAction2" class="action.DevisionAction">
    <property name="orgdao" ref="orgdao" />
</bean>
<bean id="divAction" class="action.DevisionAction">
    <property name="divdao" ref="divdao" />
</bean>

sample DAO class DAO类样本

public class DevisionDaoImpl implements DevisionDao {


private SessionFactory sessionfactory;

public void setSessionfactory(SessionFactory sessionfactory) {
    this.sessionfactory = sessionfactory;
}
@Override
public List showDiv() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void addDiv(AtDivision div) {
    Session session = sessionfactory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        session.saveOrUpdate(div);
        tx.commit();
        //System.out.println("after save : " + org.getAoId());
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    } finally {
        session.close();
    }
}

} }

1: change your datasource config to use the jboss datasource. 1:更改数据源配置以使用jboss数据源。 This can be done like this : 可以这样做:

 <jee:jndi-lookup id="springdatasource" jndi-name="your-de-jndi-name" /> 

2: Add a transaction manager to control the transactions using spring transaction manager: 2:添加事务管理器以使用spring事务管理器控制事务:

 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> 

3: Add annotation support for spring transactions. 3:为春季交易添加注释支持。 Add this to your config: 将此添加到您的配置:

<tx:annotation-driven transaction-manager="transactionManager" />

4: Finally decorate your Dao classes with @Transactional 4:最后使用@Transactional装饰您的Dao类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JBoss Weld中在持久性单元服务之前启动服务 - Start Service Ahead of Persistence Unit Service in JBoss Weld 如何在JBoss7.1.1.Final中创建多个持久性单元名称 - How to create multiple persistence unit Names in JBoss7.1.1.Final Spring Data CDI多个持久性单元错误 - Spring data cdi multiple persistence unit error JBoss,Spring:persistence.xml-为什么我应该避免使用该文件名? - JBoss, Spring: persistence.xml - why should i avoid this filename? Spring JPA Config IllegalArgumentException:没有找到名称的持久性单元 - Spring JPA Config IllegalArgumentException: No persistence unit with name found 即使启动了持久性单元实体,JPA Spring注入EntityManager也为null - JPA Spring injection EntityManager is null even if persistence unit entity is started 具有多个数据源的 Camel + Spring Boot + JPA 找不到正确的持久性单元 - Camel + Spring Boot + JPA with mutiple datasources not finding correct persistence unit Wicket + Spring + JPA + Hibernate:未找到持久性单元 - Wicket+Spring+JPA+Hibernate: No Persistence Unit Found JBoss AS 7 未找到持久性提供程序 - No persistence provider found with JBoss AS 7 无法在JBoss AS 7中使用持久性(“无法找到以子部署命名的部署单元”) - Can't get persistence to work in JBoss AS 7 (“Can't find a deployment unit named at subdeployment”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM