简体   繁体   中英

Spring and persistence unit with jboss

My project initially develop using with out persistence unit and project is spring,Struts2 hibernate intergration one. Now I need to use jboss connection pool and persistence unit. Can any one guild me to shortest way to convert project in that requirement. current 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

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. 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:

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

3: Add annotation support for spring transactions. Add this to your config:

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

4: Finally decorate your Dao classes with @Transactional

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM